2017-11-01 12 views
0

라디오 버튼을 클릭하면 응용 프로그램이 충돌합니다. 라디오 버튼이 동적으로 생성됩니다. 그러나 그 버튼을 참조 할 때 충돌합니다.내 응용 프로그램이 라디오 버튼으로 충돌 함

충돌이 발생합니다. 여기에 스낵바가 표시됩니다. 내가 믿을 때 내가 선택한 버튼에 대한 getText 메서드를 호출합니다.

어떻게 해결할 수 있습니까?

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); 
    } 
} 
} 

여기, 내 로그 캣 어쨌든

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) 

이다가 충돌 왜 내가 아는 '해달라고 :

여기 내 코드입니다. 제발 도와주세요

+0

스택 추적을 읽는이 있습니다 1. 해결하는 단계 (로그 캣은에 대한 자세한 내용은 https://developer.android.com/studio/debug/am-logcat.html#running 참조 설정 및 사용) 실제 예외가 오류를 일으킨 원인을 확인합니다. 디버그 모드에서 코드를 단계별로 실행하고 중단 점을 사용하여 충돌이 발생한 위치와 그 원인을 확인합니다. – WOUNDEDStevenJones

+0

표시 할 수 있습니까? 오류를 기록하십시오. – muazhud

+0

질문에 붙여 넣었습니다. 어쨌든, 나는 다시 확인했다. getText.to_String 메쏘드를 호출 할 때의 문제점 – ama989

답변

0
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); 
} 
} 
} 
0

여기에 가서, 당신의 요구 사항은 분명하지 않지만 이미 하나의 XML에 다른 라디오 그룹을 추가 할 생각을하지 않습니다. 수정 된 버전이 정상적으로 작동합니다. 더 라디오 버튼이 선택하지 않은 경우

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    final RadioGroup radioGroup = (RadioGroup) findViewById(R.id.file_radio_group); 
    String[] filename = getApplicationContext().fileList(); 
    addRadioButtons(radioGroup, filename.length, filename); 
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      int id_Btn = radioGroup.getCheckedRadioButtonId(); 
      for (int count = 0; count < radioGroup.getChildCount(); count++) { 
       if (radioGroup.getChildAt(count).getId() == id_Btn) { 
        RadioButton radioButton = (RadioButton) radioGroup.getChildAt(count); 
        Snackbar.make(view, "Replace with your own action " + radioButton.getText().toString(), Snackbar.LENGTH_LONG) 
          .setAction("Action", null).show(); 
       } 
      } 
     } 
    }); 
} 

public void addRadioButtons(RadioGroup radioGroup, int number, String[] filenames) { 
    radioGroup.setOrientation(LinearLayout.VERTICAL); 
    for (int i = 1; i <= number; i++) { 
     RadioButton rdbtn = new RadioButton(this); 
     rdbtn.setId((i * 2) + i); 
     rdbtn.setText(filenames[i - 1]); 
     rdbtn.setTextSize(25); 
     radioGroup.addView(rdbtn); 
    } 
} 
0

rd.getCheckedRadioButtonId() -1을 돌려줍니다 그 때문에 findViewById보기를 찾을 수있는 뷰의 ID 없을 것이다.

id_Btn을 사용하기 전에 -1을 확인하여보기를 찾으십시오.

public void onClick(View view) { 
    RadioGroup rd = (RadioGroup) findViewById(R.id.file_radio_group); 
    int id_Btn = rd.getCheckedRadioButtonId() ; 
    if (id_Btn != -1) { 
     RadioButton selectedRB = (RadioButton) findViewById(id_Btn) ; 
    } else { 
     //Handle case if no button is selected 
    } 

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