2013-04-11 4 views
0

RadioGroup에 4 RadioButton의 대화 상자가 있는데 클릭 한 ID를 반환하려고 시도했지만 NullPointerException이 계속 나타납니다. 몇 가지 예를 살펴보고 광산이 어떻게 다른지 보지 못했습니다.RadioButton이 NULL을 반환하는 이유는 무엇입니까?

<RadioGroup 
      android:id="@+id/radiojqmobdiv" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:checkedButton="0" 
      android:orientation="horizontal" > 

      <RadioButton 
       android:id="@+id/jqpage" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/jqpage" 
       android:checked="true" /> 

      <RadioButton 
       android:id="@+id/jqheader" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/jqheader" /> 

      <RadioButton 
       android:id="@+id/jqcontent" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/jqcontent" /> 

      <RadioButton 
       android:id="@+id/jqfooter" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/jqfooter" /> 

     </RadioGroup> 

코드 :

@Override 
    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     setContentView(R.layout.main); 
     preferences = getSharedPreferences("MYPREFS", 0); 

     et = (EditTextLineNumbers) findViewById(R.id.ide); 
     et.setTextColor(preferences.getInt("colourChoice", Color.GREEN)); 
     et.setMaxLines(5000); 
     divBtn = (Button) findViewById(R.id.divbutton); 
     divGroup = (RadioGroup) findViewById(R.id.radiojqmobdiv); 

     exists = false; 
     gestureDetector = new GestureDetector(this, new MyGestureDetector()); 
     gestureListener = new View.OnTouchListener() { 
      public boolean onTouch(View v, MotionEvent event) { 
       return gestureDetector.onTouchEvent(event); 
      } 
     }; 
     web = (WebView) findViewById(R.id.webpreview); 
     web.setOnClickListener(this); 
     web.setOnTouchListener(gestureListener); 
     getWindow().setSoftInputMode(
       WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); 
     File dir = new File(Environment.getExternalStorageDirectory() 
       + "/My Webs"); 
     currentDirectory = dir; 
     ListView lv = getListView(); 
     registerForContextMenu(lv); 
     lv.setOnItemLongClickListener(new OnItemLongClickListener() { 

      @Override 
      public boolean onItemLongClick(AdapterView<?> a, View v, 
        int position, long id) { 
       Show_Alert_box(v.getContext(), "Please select action.", 
         position); 
       return false; 
      } 
     }); 

     et.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, 
        int count) { 
      } 

      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, 
        int after) { 

      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       changed = true; 
       startPos = et.getSelectionStart(); 
       endPos = et.getSelectionEnd(); 
      } 
     }); 
     if (dir.isDirectory()) { 
      browseToRoot(); 
     } else { 
      dir.mkdir(); 
     } 

     divBtn.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       final Dialog dialog = new Dialog(MainActivity.this); 
       dialog.setContentView(R.layout.divdialog); 
       dialog.setTitle("Inserv Div"); 

       // set the custom dialog components - text, image and button 
       // TextView text = (TextView) dialog.findViewById(R.id.text); 
       // text.setText("Android custom dialog example!"); 
       // ImageView image = (ImageView) 
       // dialog.findViewById(R.id.image); 
       // image.setImageResource(R.drawable.ic_launcher); 

       **Button insertButton = (Button) dialog 
         .findViewById(R.id.insertBtn);** 
       // if button is clicked, close the custom dialog 
       insertButton.setOnClickListener(new OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         // int selectedID = divGroup.getCheckedRadioButtonId(); 

         // find the radiobutton by returned id 
         divRdoBtn = (RadioButton) findViewById(selectedID); 

         Toast.makeText(MainActivity.this, divRdoBtn.getText(), 
           Toast.LENGTH_LONG).show(); 
        } 
       }); 

       Button cancelButton = (Button) dialog 
         .findViewById(R.id.cancelBtn); 
       cancelButton.setOnClickListener(new OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         dialog.dismiss(); 
        } 
       }); 
       dialog.show(); 
      } 

     }); 

     gestureDetector = new GestureDetector(this, new MyGestureDetector()); 
     gestureListener = new View.OnTouchListener() { 
      public boolean onTouch(View v, MotionEvent event) { 
       return gestureDetector.onTouchEvent(event); 
      } 
     }; 
     loadPrefs(); 
    } 
+0

스택 추적은 무엇입니까? – Smit

+0

divGroup이 초기화 됨 onCreate(); NullException이 발생했습니다 selectedID ... – RapsFan1981

+1

그런 다음'divGroup'가'null'입니다 – codeMagic

답변

1

당신은 당신의 대화 레이아웃과 활동 레이아웃 모두의 전체 xml 파일을 게시하지 않았다, 그래서 여기에 몇 가지 가정을 만들고 있어요.

그러나 제목에 대화 상자에 divGroup이라는 이름의 RadioGroup이 포함되어 있다고 말했습니까?

divGroup = (RadioGroup) findViewById(R.id.radiojqmobdiv); 

당신은 ID R.id.radiojqmobdiv의 활동에서 검색, 당신이 암시 한 : 그게 사실이라면, 당신이이 줄을 실행할 때을 radioGroup을 찾을 수 없을 것이라는 의미가 있습니다 아직 만들지 않은 대화 상자에만 존재합니다. findViewById()는 뷰가 없으면 null을 반환하므로 divGroup에 null을 할당합니다.

대화 상자를 선언 한 후 해당 줄을 이동하고 작업 대신 dialog 개체에 findViewById을 호출해야합니다.

final Dialog dialog = new Dialog(MainActivity.this); 
dialog.setContentView(R.layout.divdialog); 
divGroup = (RadioGroup) dialog.findViewById(R.id.radiojqmobdiv); 
+0

Ahhhtesting now하지만 느낌이 좋았습니다. 이것이 의미가 있습니다. – RapsFan1981

+0

좋습니다! :) 그것이 작동하는지 또는 더 도움이 될 답변을 추가 할 수 있는지 알려주십시오. – mattgmg1990

관련 문제