0

当我打电话时:

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

我得到一个 NullPointerException。我认为我的问题在于该代码行:

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

但我就是找不到错误。我也使用了搜索,但我仍然不明白。我希望有人可以在这里帮助我。

在这里你可以观看我的完整代码:

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>

谢谢

4

1 回答 1

0

您在 findView 上缺少标识符,我通过将弹出视图添加到 findViewById() 来修复类似的 NPE。如果我的观点正确,这应该可以解决问题 final EditText textinput = (EditText)layout.findViewById(R.id.popup_plaintext);

于 2015-02-26T20:29:27.483 回答