2013-10-15 53 views
0

當我打電話:NullPointerException異常的getText()/彈出/ EditText上的Android

channel_name = textinput.getText().toString(); 

我得到一個NullPointerException。 我想我的問題是在這行代碼:

final EditText textinput = (EditText)findViewById(R.id.popup_plaintext); 

但我就是不能發現其中的錯誤。 我也使用了搜索,但我仍然沒有得到它。 我希望有人能幫助我。

這裏u能看我的全碼:

package de.project.ibob; 

import android.app.Activity; 
import android.content.Context; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Gravity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.View.OnLongClickListener; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.PopupWindow; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.RadioGroup.OnCheckedChangeListener; 
import android.widget.Switch; 


public class ModuleActivity extends Activity{ 

//Data 
    int a = 16; 
    int b = 6; 
    byte[][] buffer = new byte[a][b]; 
    public int LongClickID; 
    public String channel_name; 

//Debugging 
    private static final String TAG = "ModuleActivity"; 
    private static final boolean D = true; 

//Layout 
    private PopupWindow pwindo; 
    private RadioGroup mradiogroup; 
    private RadioButton mradiobutton[] = new RadioButton[7]; 
    private Switch mswitch[] = new Switch[33]; 
    private Button buttonClosePopup; 
    public Switch textoutput; 

    private void SavePreferences() { 

     SharedPreferences settings = getSharedPreferences("Channels", 0); 
     SharedPreferences.Editor editor = settings.edit(); 

//Buttons 
     for(int i = 1; i < 7; i++) { 

      editor.putBoolean("radio" + i, mradiobutton[i].isChecked()); 

     }//for 

     for(int i = 1; i < 33; i++) { 

      editor.putBoolean("switch" + i, mswitch[i].isChecked()); 

     }//for 

     editor.commit(); 

    }//SafePreferences 

    private void LoadPreferences() { 

     SharedPreferences settings = getSharedPreferences("Channels", 0); 
//  
//Buttons 
     for(int i = 1; i < 7; i++) { 

      mradiobutton[i].setChecked(settings.getBoolean("radio" + i, false)); 

     }//for 

     for(int i = 1; i < 33; i++) { 

      mswitch[i].setChecked(settings.getBoolean("switch" + i, false)); 

     }//for 

    }//LoadPreferences 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     if(D) Log.e(TAG, "+++ ON CREATE +++"); 
     super.onCreate(savedInstanceState); 

//Layout 
     setContentView(R.layout.modules); 

//RadioGroup 
     mradiogroup = (RadioGroup)findViewById(R.id.radio_group_modules); 

//RadioButtons (i = 1 because of radiobutton_modules_module ID starts at 1! ==> i <7 NOT 6!) 
     for(int i = 1; i < 7; i++) { 

      String RadioID = "radiobutton_modules_module" + i; 
      int resID = getResources().getIdentifier(RadioID, "id", "de.project.ibob"); 
      mradiobutton[i] = ((RadioButton)findViewById(resID)); 
      if(D) Log.e(TAG, "+++ SetRadioButton +++" + i); 

     }//for 

//Switches (i = 1 because of Switch ID starts at 1! ==> i <33 NOT 32!)  
     for(int i = 1; i < 33; i++) { 

      String SwitchID = "Switch" + i; 
      int resID = getResources().getIdentifier(SwitchID, "id", "de.project.ibob"); 
      mswitch[i] = ((Switch)findViewById(resID)); 
      mswitch[i].setOnLongClickListener(new OnLongClickListener(){ 

       @Override 
        public boolean onLongClick(View v) { 
        if(D) Log.e(TAG, "+++ LongClick +++" + v.getId()); 

        LongClickID = v.getId(); 
        initiatePopupWindow(); 

        return true; 
        } 
      }); 
      if(D) Log.e(TAG, "+++ Switch +++" + i); 

     }//for 

//Buttons Listener 
     mradiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(RadioGroup group, int mradiogroup) { 

      for(int i = 1; i < 7; i++) { 

       if(mradiobutton[i].isChecked() == true) { 
        if(D) Log.e(TAG, "+++ RadioButton selected +++"); 


//Identifier (MODUL - Read/Write - Channel) all 16 Messages get the same ModulID 
        for(a = 0; a < 15; a++) { 

         if(i == 1)buffer[a][0] = 0x01; 
         if(i == 2)buffer[a][1] = 0x02; 
         if(i == 3)buffer[a][2] = 0x03; 
         if(i == 4)buffer[a][3] = 0x04; 
         if(i == 5)buffer[a][4] = 0x05; 
         if(i == 6)buffer[a][5] = 0x06; 



         }//for 

        if(D) Log.e(TAG, "ID Byte:" + i + " selected"); 

        }//if 
       }//for 
      }//onCheckedChanged 
     });//setOnCheckedChangedListener 

//Functions 
      LoadPreferences(); 

    }//onCreate 

    @Override 
    public void onBackPressed() { 

     SavePreferences(); 
     super.onBackPressed(); 

     if(D) Log.e(TAG, "+++ onBackPressed +++"); 



    }//onBackPressed 

    public void initiatePopupWindow() { 

    try{ 
     LayoutInflater inflater = (LayoutInflater) 
       ModuleActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View layout = inflater.inflate(R.layout.popup, 
       (ViewGroup)findViewById(R.id.popup_element)); 

     pwindo = new PopupWindow(layout,600,250,true); 
     pwindo.showAtLocation(layout, Gravity.CENTER,0,0); 

     buttonClosePopup = (Button)layout.findViewById(R.id.button_modules_popup_setText); 
     buttonClosePopup.setOnClickListener(cancel_button_click_listener); 

    }//try 

    catch(Exception e) { 
     e.printStackTrace(); 
    } 
    } 


    public OnClickListener cancel_button_click_listener = new OnClickListener(){ 
    @Override 
    public void onClick(View cancel_button_click_listener){ 

      final EditText textinput = (EditText)findViewById(R.id.popup_plaintext); 
      channel_name = textinput.getText().toString(); 
      textoutput = (Switch)findViewById(LongClickID); 
      textoutput.setText(channel_name); 
      pwindo.dismiss(); 
    } 
    }; 
    } 

堆棧跟蹤:

01-26 00:13:58.071: E/AndroidRuntime(15877): FATAL EXCEPTION: main 
01-26 00:13:58.071: E/AndroidRuntime(15877): java.lang.NullPointerException 
01-26 00:13:58.071: E/AndroidRuntime(15877): at de.project.ibob.ModuleActivity$1.onClick(ModuleActivity.java:205) 
01-26 00:13:58.071: E/AndroidRuntime(15877): at android.view.View.performClick(View.java:4232) 
01-26 00:13:58.071: E/AndroidRuntime(15877): at android.view.View$PerformClick.run(View.java:17318) 
01-26 00:13:58.071: E/AndroidRuntime(15877): at android.os.Handler.handleCallback(Handler.java:615) 
01-26 00:13:58.071: E/AndroidRuntime(15877): at android.os.Handler.dispatchMessage(Handler.java:92) 
01-26 00:13:58.071: E/AndroidRuntime(15877): at android.os.Looper.loop(Looper.java:137) 
01-26 00:13:58.071: E/AndroidRuntime(15877): at android.app.ActivityThread.main(ActivityThread.java:4921) 
01-26 00:13:58.071: E/AndroidRuntime(15877): at java.lang.reflect.Method.invokeNative(Native Method) 
01-26 00:13:58.071: E/AndroidRuntime(15877): at java.lang.reflect.Method.invoke(Method.java:511) 
01-26 00:13:58.071: E/AndroidRuntime(15877): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) 
01-26 00:13:58.071: E/AndroidRuntime(15877): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 
01-26 00:13:58.071: E/AndroidRuntime(15877): at dalvik.system.NativeStart.main(Native Method) 

在這裏,你可以看到我的佈局XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/popup_element" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#444444" > 

    <TextView 
     android:id="@+id/popup_text" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="32dp" 
     android:text="@string/set_name" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textColor="@android:color/white" /> 

    <Button 
     android:id="@+id/button_modules_popup_setText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/popup_plaintext" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="21dp" 
     android:text="@string/set_channelname" 
     android:textColor="@android:color/white" /> 

    <EditText 
     android:id="@+id/popup_plaintext" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/popup_text" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="21dp" 
     android:ems="10" 
     android:inputType="text" 
     android:textColor="@android:color/white" /> 

</RelativeLayout> 

謝謝

+0

是textinput初始化與任何內容,當你在調試器中看它? – Simon

+0

不,它說textinput = null,所以沒有內容。但是我在PlainText中寫了一個文本。那麼我的錯誤在哪裏呢? –

+0

所以這就是NPE的原因。由於我沒有意識到Android API,我只能猜測,但無論是findViewById返回null出於某種原因或轉換爲EditText不起作用。還請檢查是否有任何編譯器警告,這可能是可疑的。 – Simon

回答

0

你錯過了一個身份證ifier放在你的findView中,我通過向findViewById()添加彈出視圖來修復類似的NPE。這應該是訣竅,如果我有你的觀點正確 final EditText textinput = (EditText)layout.findViewById(R.id.popup_plaintext);

相關問題