安卓按钮单击android上的警告对话框后应用程序崩溃

时间:2014-11-11 10:59:08

标签: android alertdialog

请帮我解决这个问题,因为我在网上搜索了解决方案而没有获得解决方案。

我要做的是从警告对话框中获取数据,然后单击确定按钮。 单击TypeActivity上的项后会弹出警告对话框。此单击的结果将传递给HomeActivity,其中警告对话框将弹出一个文本字段。 但是每次我输入文本字段并按下" OK"按钮,应用程序崩溃。 logcat显示空指针异常。我也会显示logcat内容。谢谢你的帮助。

我的代码如下

TypeActivity:

    String[] typeTag = {"TypeA","TypeB","TypeC","TypeD"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setListAdapter(new MyAdapter(TypeActivity.this,R.layout.spinnerrow, typeTag));
    getListView().setAdapter(new MyAdapter(TypeActivity.this,R.layout.spinnerrow, typeTag));


    getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Intent returnIntent = new Intent();
            returnIntent.putExtra("selectedType", typeTag[position]);
            setResult(RESULT_OK, returnIntent);
            finish();
        }
    });

HomeActivity:

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 1 && resultCode == RESULT_OK){
            String temp = data.getStringExtra("selectedType");

            AlertDialog.Builder builder2 = new AlertDialog.Builder(HomeActivity.this); 
            LayoutInflater inflater = HomeActivity.this.getLayoutInflater();
            ViewGroup parent = null;
            builder2.setView(inflater.inflate(R.layout.inputtype, parent, false));

            final EditText tagname = (EditText)findViewById(R.id.uname);                        

            builder2.setPositiveButton("OK", new DialogInterface.OnClickListener()
            {                   

            @Override
            public void onClick(DialogInterface dialog, int id) {
                String tag = tagname.getText().toString();
                Toast.makeText(getApplicationContext(), "U entered "+tag, Toast.LENGTH_LONG).show();
                }
            })
            .setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                    }
            })
            .show();  
        }
    }

Logcat:

1-11 05:22:45.427: E/AndroidRuntime(2298): FATAL EXCEPTION: main
11-11 05:22:45.427: E/AndroidRuntime(2298): Process: com.example.myapp, PID: 2298
11-11 05:22:45.427: E/AndroidRuntime(2298): java.lang.NullPointerException
11-11 05:22:45.427: E/AndroidRuntime(2298):     at com.example.myapp.HomeActivity$3.onClick(HomeActivity.java:278)
11-11 05:22:45.427: E/AndroidRuntime(2298):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
11-11 05:22:45.427: E/AndroidRuntime(2298):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-11 05:22:45.427: E/AndroidRuntime(2298):     at android.os.Looper.loop(Looper.java:136)
11-11 05:22:45.427: E/AndroidRuntime(2298):     at android.app.ActivityThread.main(ActivityThread.java:5017)
11-11 05:22:45.427: E/AndroidRuntime(2298):     at java.lang.reflect.Method.invokeNative(Native Method)
11-11 05:22:45.427: E/AndroidRuntime(2298):     at java.lang.reflect.Method.invoke(Method.java:515)
11-11 05:22:45.427: E/AndroidRuntime(2298):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-11 05:22:45.427: E/AndroidRuntime(2298):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-11 05:22:45.427: E/AndroidRuntime(2298):     at dalvik.system.NativeStart.main(Native Method)

新的logcat

 11-11 06:06:35.917: E/AndroidRuntime(4530): FATAL EXCEPTION: main
 11-11 06:06:35.917: E/AndroidRuntime(4530): Process: com.example.myapp, PID: 4530
 11-11 06:06:35.917: E/AndroidRuntime(4530): java.lang.NullPointerException
 11-11 06:06:35.917: E/AndroidRuntime(4530):    at com.example.myapp.HomeActivity$3.onClick(HomeActivity.java:282)
 11-11 06:06:35.917: E/AndroidRuntime(4530):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
 11-11 06:06:35.917: E/AndroidRuntime(4530):    at android.os.Handler.dispatchMessage(Handler.java:102)
 11-11 06:06:35.917: E/AndroidRuntime(4530):    at android.os.Looper.loop(Looper.java:136)
 11-11 06:06:35.917: E/AndroidRuntime(4530):    at android.app.ActivityThread.main(ActivityThread.java:5017)
 11-11 06:06:35.917: E/AndroidRuntime(4530):    at java.lang.reflect.Method.invokeNative(Native Method)
 11-11 06:06:35.917: E/AndroidRuntime(4530):    at java.lang.reflect.Method.invoke(Method.java:515)
 11-11 06:06:35.917: E/AndroidRuntime(4530):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
 11-11 06:06:35.917: E/AndroidRuntime(4530):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
 11-11 06:06:35.917: E/AndroidRuntime(4530):    at dalvik.system.NativeStart.main(Native Method)

布局:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" >

  <ImageView
   android:id="@+id/dispimg"        
   android:layout_width="match_parent"        
   android:layout_height="64dp"        
   android:background="#d1d1d1"        
   android:contentDescription="@string/app_name"        
   android:scaleType="center"        
   android:src="@drawable/addme" /> 

    <EditText        
     android:id="@+id/uname"        
     android:layout_width="match_parent"        
     android:layout_height="wrap_content"        
     android:layout_marginBottom="10dp"        
     android:layout_marginLeft="10dp"        
     android:layout_marginRight="10dp"        
     android:layout_marginTop="16dp"                
     android:inputType="text"
     android:background="@drawable/textfield" />   

     </LinearLayout>

新代码:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 1 && resultCode == RESULT_OK){
            String temp = data.getStringExtra("selectedType");            

            AlertDialog.Builder builder2 = new AlertDialog.Builder(HomeActivity.this);              
            LayoutInflater inflater = HomeActivity.this.getLayoutInflater();
            ViewGroup parent = null;
             View view = inflater.inflate(R.layout.inputtype, parent, true);
             final EditText tagname = (EditText)findViewById(R.id.uname);   
             builder2.setView(view);

            builder2.setPositiveButton("OK", new DialogInterface.OnClickListener()
            {                   

            @Override
            public void onClick(DialogInterface dialog, int id) {
                String tag = tagname.getText().toString();
                Toast.makeText(getApplicationContext(), "U entered "+tag, Toast.LENGTH_LONG).show();
                    }
                })
                 .setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {

                        }
                 })
                .show();  
            }
        }

new logcat:

    11-11 06:58:49.804: E/AndroidRuntime(7148): FATAL EXCEPTION: main
    11-11 06:58:49.804: E/AndroidRuntime(7148): Process: com.example.myapp, PID: 7148
    11-11 06:58:49.804: E/AndroidRuntime(7148): java.lang.NullPointerException
    11-11 06:58:49.804: E/AndroidRuntime(7148):     at com.example.myapp.HomeActivity$3.onClick(HomeActivity.java:288)
    11-11 06:58:49.804: E/AndroidRuntime(7148):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
    11-11 06:58:49.804: E/AndroidRuntime(7148):     at android.os.Handler.dispatchMessage(Handler.java:102)
    11-11 06:58:49.804: E/AndroidRuntime(7148):     at android.os.Looper.loop(Looper.java:136)
    11-11 06:58:49.804: E/AndroidRuntime(7148):     at android.app.ActivityThread.main(ActivityThread.java:5017)
    11-11 06:58:49.804: E/AndroidRuntime(7148):     at java.lang.reflect.Method.invokeNative(Native Method)
    11-11 06:58:49.804: E/AndroidRuntime(7148):     at java.lang.reflect.Method.invoke(Method.java:515)
    11-11 06:58:49.804: E/AndroidRuntime(7148):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    11-11 06:58:49.804: E/AndroidRuntime(7148):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    11-11 06:58:49.804: E/AndroidRuntime(7148):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:3)

您的editText属于AlertDialog,因此代码中的跟随行为空:

 final EditText tagname = (EditText)findViewById(R.id.uname);  

所以你得到NPE

String tag = tagname.getText().toString();

您需要以下代码:

 View view = inflater.inflate(R.layout.inputtype, parent, false)
 builder2.setView(view);

 final EditText tagname = (EditText)view.findViewById(R.id.uname); 

因此您的代码必须如下:

  AlertDialog.Builder builder2 = new AlertDialog.Builder(HomeActivity.this); 
            LayoutInflater inflater = HomeActivity.this.getLayoutInflater();
            ViewGroup parent = null;
             View view = inflater.inflate(R.layout.inputtype, parent, false);
             final EditText tagname = (EditText)view.findViewById(R.id.uname); 
             builder2.setView(view);




            builder2.setPositiveButton("OK", new DialogInterface.OnClickListener()
            {                   

            @Override
            public void onClick(DialogInterface dialog, int id) {
                String tag = tagname.getText().toString();
                Toast.makeText(getApplicationContext(), "U entered "+tag, Toast.LENGTH_LONG).show();
                }
            })
            .setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                    }
            })
            .show();