Search code examples
androidandroid-radiogroupandroid-radiobutton

My App Crashes With List Radio Button


My application crashes when I click on a radio button. The radio Buttons are generated Dynamically. But, when I refer to that button it crashes.

The crash occurs Here when the snackbar is shown. I believe when when I call the method getText for the selected Button.

How can I fix it ?

Here is my code:

public class ListActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    String[] filename = getApplicationContext().fileList();

    addRadioButtons(filename.length, filename);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            RadioGroup rd = (RadioGroup) findViewById(R.id.file_radio_group);
            int id_Btn = rd.getCheckedRadioButtonId() ;
            RadioButton selectedRB = (RadioButton) findViewById(id_Btn) ;

            Snackbar.make(view, "Replace with your own action "+selectedRB.getText().toString(), Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });



}
public void addRadioButtons(int number,String[] filenames) {

    for (int row = 0; row < 1; row++) {
        RadioGroup ll = new RadioGroup(this);
        ll.setOrientation(LinearLayout.VERTICAL);

        for (int i = 1; i <= number; i++) {
            RadioButton rdbtn = new RadioButton(this);
            rdbtn.setId((row * 2) + i);
            rdbtn.setText(filenames[i-1]);
            //rdbtn.setText("Radio " + rdbtn.getId());
            rdbtn.setTextSize(25);
            ll.addView(rdbtn);
        }
        ((ViewGroup) findViewById(R.id.file_radio_group)).addView(ll);
    }
}
}

Here is my Logcat

 11-01 19:04:26.460 6743-6743/alahdal.amjad.newloginproject E/AndroidRuntime: FATAL EXCEPTION: main
 Process: alahdal.amjad.newloginproject, PID: 6743
 java.lang.NullPointerExceptionat 
 alahdal.amjad.newloginproject.ListActivity$1.onClick(ListActivity.java:39)
 at android.view.View.performClick(View.java:4438)
 at android.view.View$PerformClick.run(View.java:18422)
 at android.os.Handler.handleCallback(Handler.java:733)
 at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:136)
 at android.app.ActivityThread.main(ActivityThread.java:5017)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:515)     atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
 at dalvik.system.NativeStart.main(Native Method)

Anyways, I dont' know why it crashes. Please Help


Solution

  • public class ListActivity extends AppCompatActivity {
    private RadioGroup ll; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    String[] filename = getApplicationContext().fileList();
    
    addRadioButtons(filename.length, filename);
    
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            RadioGroup rd = (RadioGroup) findViewById(R.id.file_radio_group);
            int id_Btn = rd.getCheckedRadioButtonId() ;
            RadioButton selectedRB = (RadioButton) findViewById(id_Btn) ;
    
            Snackbar.make(view, "Replace with your own action "+selectedRB.getText().toString(), Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    
    
    
    }
    
    
      public void addRadioButtons(int number,String[] filenames) {
    
    for (int row = 0; row < 1; row++) {
        ll = new RadioGroup(this);
        ll.setOrientation(LinearLayout.VERTICAL);
    
        for (int i = 1; i <= number; i++) {
            RadioButton rdbtn = new RadioButton(this);
            rdbtn.setId((row * 2) + i);
            rdbtn.setText(filenames[i-1]);
            //rdbtn.setText("Radio " + rdbtn.getId());
            rdbtn.setTextSize(25);
            ll.addView(rdbtn);
        }
        ((ViewGroup) findViewById(R.id.file_radio_group)).addView(ll);
    }
    }
    }