fatesses 2018-04-23 05:55 采纳率: 50%
浏览 1332
已采纳

求大神们帮忙看看,安卓中的RadioButton.setChecked(boolean);

 public class MainActivity extends FragmentActivity implements OnCheckedChangeListener{

    @ViewInject(R.id.main_bottom_tabs)
    private RadioGroup group;
    @ViewInject(R.id.main_home)
    private RadioButton main_home;
    private FragmentManager fragmentManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ViewUtils.inject(this);
        fragmentManager = getSupportFragmentManager();
        main_home.setChecked(true);

        group.setOnCheckedChangeListener(this);
        changeFragment(new FragmentHome(), false);


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.main_home:
                changeFragment(new FragmentHome(),true);
                break;
            case R.id.main_one:
                changeFragment(new FragmentOne(),true);
                break;
            case R.id.main_two:
                changeFragment(new FragmentTwo(),true);
                break;


                default:
                    break;
        }
    }

    public void changeFragment(Fragment fragment, boolean isFirst){
      FragmentTransaction transaction = fragmentManager.beginTransaction();
      transaction.replace(R.id.main_content,fragment);
      transaction.commit();
    }




}
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fatesses.homepage/com.example.fatesses.homepage.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioButton.setChecked(boolean)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioButton.setChecked(boolean)' on a null object reference
    at com.example.fatesses.homepage.MainActivity.onCreate(MainActivity.java:58)
    at android.app.Activity.performCreate(Activity.java:6975)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
    at android.os.Handler.dispatchMessage(Handler.java:105) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6541) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

大佬能看看这是什么问题吗,而且我明明用的是setChecked(true)。。。。

  • 写回答

8条回答 默认 最新

  • nades 2018-04-23 11:50
    关注

    debug 走一下吧 错误是空指针 很明显的的是你的RadioButton控制帧 我估计是你的注解的问题 你直接findViewById 试一下如果可以 就是你的注解有问题

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀