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)。。。。