从活动中更改片段中的RadioButton图像

时间:2017-04-25 09:42:54

标签: android radio-button

我想在单击活动中的按钮时更改片段中的单选按钮图像,所以我试图在我的片段中调用一个方法来更改图像,但它不起作用,这是我的代码:< / p>

我的片段:

public class ChoicesFragment extends Fragment {
    RadioButton rd1;
    RadioButton rd2;
    RadioButton rd3;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_choices, container, false);
        rd1=(RadioButton) view.findViewById(R.id.choice1);
        rd2=(RadioButton) view.findViewById(R.id.choice2);
        rd3=(RadioButton) view.findViewById(R.id.choice3);
        return view;
    }
    public void setRadioButtonsImages(int choix1, int choix2, int choix3){
        rd1.setButtonDrawable(choix1);
        rd2.setButtonDrawable(choix2);
        rd3.setButtonDrawable(choix3);
    }
}

在我的活动中:

public void showImages(View view) {
    android.support.v4.app.FragmentTransaction transaction  = getSupportFragmentManager().beginTransaction();
    cf = new ChoicesFragment();
    transaction.replace(R.id.container2, cf,"choicesFragment").addToBackStack(null).commit();
    Resources res = getResources();
    int choix1 = res.getIdentifier(level.getChoix1(),"drawable",getPackageName());
    int choix2 = res.getIdentifier(level.getChoix2(),"drawable",getPackageName());
    int choix3 = res.getIdentifier(level.getChoix3(),"drawable",getPackageName());
    cf.setRadioButtonImage(choix1,choix2,choix3);
}

我收到此错误

04-25 12:39:28.734 29413-29413/com.example.mireille.test E/AndroidRuntime: FATAL EXCEPTION: main
         Process: com.example.mireille.test, PID: 29413
         java.lang.IllegalStateException: Could not execute method for android:onClick
             at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
             at android.view.View.performClick(View.java:5637)
             at android.view.View$PerformClick.run(View.java:22429)
             at android.os.Handler.handleCallback(Handler.java:751)
             at android.os.Handler.dispatchMessage(Handler.java:95)
             at android.os.Looper.loop(Looper.java:154)
             at android.app.ActivityThread.main(ActivityThread.java:6119)
             at java.lang.reflect.Method.invoke(Native Method)
             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
          Caused by: java.lang.reflect.InvocationTargetException
             at java.lang.reflect.Method.invoke(Native Method)
             at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
             at android.view.View.performClick(View.java:5637) 
             at android.view.View$PerformClick.run(View.java:22429) 
             at android.os.Handler.handleCallback(Handler.java:751) 
             at android.os.Handler.dispatchMessage(Handler.java:95) 
             at android.os.Looper.loop(Looper.java:154) 
             at android.app.ActivityThread.main(ActivityThread.java:6119) 
             at java.lang.reflect.Method.invoke(Native Method) 
             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
          Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RadioButton.setButtonDrawable(int)' on a null object reference
             at com.example.mireille.simplegame.ChoicesFragment.setRadioButtonImage(ChoicesFragment.java:37)
             at com.example.mireille.simplegame.MainActivity.changeLevel(MainActivity.java:60)
             at com.example.mireille.simplegame.MainActivity.startRotatingImage(MainActivity.java:48)
             at java.lang.reflect.Method.invoke(Native Method) 
             at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
             at android.view.View.performClick(View.java:5637) 
             at android.view.View$PerformClick.run(View.java:22429) 
             at android.os.Handler.handleCallback(Handler.java:751) 
             at android.os.Handler.dispatchMessage(Handler.java:95) 
             at android.os.Looper.loop(Looper.java:154) 
             at android.app.ActivityThread.main(ActivityThread.java:6119) 
             at java.lang.reflect.Method.invoke(Native Method) 
             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

由于

0 个答案:

没有答案