【问题标题】:Null Pointer Exception in alert Dialog Builder警报对话框生成器中的空指针异常
【发布时间】:2014-01-06 09:52:55
【问题描述】:

在这个方法中,我曾经调用 AlertDialog Box 。但是当我调用警报对话框时。我总是得到胎儿异常。以下是我在警报 Dialog Builder 中遇到的错误。请帮我找。

    12-18 04:25:10.222: E/AndroidRuntime(4173): FATAL EXCEPTION: main
    12-18 04:25:10.222: E/AndroidRuntime(4173): Process: com.androidexample.customlistview, PID: 4173
    12-18 04:25:10.222: E/AndroidRuntime(4173): java.lang.NullPointerException
    12-18 04:25:10.222: E/AndroidRuntime(4173):     at android.app.AlertDialog.resolveDialogTheme          (AlertDialog.java:143)
    12-18 04:25:10.222: E/AndroidRuntime(4173):     at android.app.AlertDialog$Builder.<init>(AlertDialog.java:360)
    12-18 04:25:10.222: E/AndroidRuntime(4173):     at    com.androidexample.customlistview.CustomListViewAndroidExample.updateData  (CustomListViewAndroidExample.java:162)
    12-18 04:25:10.222: E/AndroidRuntime(4173):     at com.androidexample.customlistview.CustomAdapter$1.onClick(CustomAdapter.java:144)
    12-18 04:25:10.222: E/AndroidRuntime(4173):     at android.view.View.performClick(View.java:4424)
    12-18 04:25:10.222: E/AndroidRuntime(4173):     at android.view.View$PerformClick.run(View.java:18383)
    12-18 04:25:10.222: E/AndroidRuntime(4173):     at android.os.Handler.handleCallback(Handler.java:733)
    12-18 04:25:10.222: E/AndroidRuntime(4173):     at android.os.Handler.dispatchMessage(Handler.java:95)
    12-18 04:25:10.222: E/AndroidRuntime(4173):     at android.os.Looper.loop(Looper.java:137)         12-18 04:25:10.222: E/AndroidRuntime(4173):  at android.app.ActivityThread.main(ActivityThread.java:4998)
    12-18 04:25:10.222: E/AndroidRuntime(4173):     at java.lang.reflect.Method.invokeNative(Native Method)
    12-18 04:25:10.222: E/AndroidRuntime(4173):     at java.lang.reflect.Method.invoke(Method.java:515)
    12-18 04:25:10.222: E/AndroidRuntime(4173):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
    12-18 04:25:10.222: E/AndroidRuntime(4173):     at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:593)
    12-18 04:25:10.222: E/AndroidRuntime(4173):     at dalvik.system.NativeStart.main(Native Method)

这是代码:

 package com.androidexample.customlistview;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;


public class CustomListViewAndroidExample extends Activity {

    ListView list;
    CustomAdapter adapter;
    String response = null;
    TextView txt_Date ;
    TextView txt_Location;
    static String teamID;
    static String startTime ;

    public  CustomListViewAndroidExample CustomListView = null;
    public  ArrayList<String> userIDArr = new ArrayList<String>();
    public  ArrayList<ListModel> CustomListViewArr = new ArrayList<ListModel>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CustomListView = this;
        txt_Date = (TextView) findViewById(R.id.txt_date);
        txt_Location = (TextView) findViewById(R.id.txt_location);
        Intent intent= getIntent();
        startTime = intent.getStringExtra("startTime");
        txt_Location.setText(intent.getStringExtra("location"));
        teamID = intent.getStringExtra("teamID");
        txt_Date.setText(startTime);
        setListData();
        Resources res =getResources();
        list=(ListView)findViewById(R.id.participationList);

        adapter=new CustomAdapter(CustomListView, CustomListViewArr,res,userIDArr);
        list.setAdapter(adapter);

    }


    public void setListData()
    {
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
        .detectDiskReads().detectDiskWrites().detectNetwork()
        .penaltyLog().build());

        String userName = null;
        int memberStatus = 0;
        JSONArray memberData=null;
        int entry = 0;
        JSONObject jArray = null;
        String userID = null;
        String photo = null;

        ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("teamID",teamID));
        postParameters.add(new BasicNameValuePair("startTime","2013-09-18 10:00:00"));


        try {

            response = CustomHttpClient.executeHttpPost(
                   "http://10.0.2.2/football365/sankashaList.php",
                   postParameters);

            jArray = new JSONObject(response.toString());
            memberData=jArray.getJSONArray("memberdata");

            for(int i = 0 ; i < memberData.length(); i++){

                userName = (memberData.getJSONObject(i).getString("name").toString()+"\t");
                memberStatus = Integer.parseInt(memberData.getJSONObject(i).getString("memberStatus").toString());
                entry = Integer.parseInt(memberData.getJSONObject(i).getString("entry").toString());
                photo = (memberData.getJSONObject(i).getString("photo").toString());
                userID = (memberData.getJSONObject(i).getString("userID").toString());
                Log.i("photo", photo);
                userIDArr.add(i, userID);


                ListModel sched = new ListModel();
                sched.setMemberName(userName);
                sched.setMemberStatus(memberStatus);
                  if(photo.equals("") || photo.equals(null))
                   {
                      sched.setImage("upload.png");
                   }
                   else{
                   sched.setImage(photo);
                   }
                   sched.setEntry(entry);
                   sched.setUserID(userID);


                CustomListViewArr.add(sched);
            }

        }


        catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
        }
        catch (Exception e) {

            Log.e("log_tag", "Error parsing data "+e.toString());
        }

           Log.i("AVCE", userName+"");
           Log.i("AVCE", memberStatus+"");



    }

    @SuppressWarnings("deprecation")
    public  void updateData(String userID,String entry) {


        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
        .detectDiskReads().detectDiskWrites().detectNetwork()
        .penaltyLog().build());

        String response = null;

        ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("teamID",teamID));
        postParameters.add(new BasicNameValuePair("startTime",startTime));
        postParameters.add(new BasicNameValuePair("entry", entry));
        postParameters.add(new BasicNameValuePair("userID", userID));

        try {
            response = CustomHttpClient.executeHttpPost("http://10.0.2.2/football365/scheduleupdateflagentry.php",
                       postParameters);
        }
        catch (Exception e) {

            e.printStackTrace();
        }
        if(CustomHttpClient.response.getStatusLine().getStatusCode() == 400){

            if(response.toString().trim().equals("Insert Fail")){
                AlertDialog.Builder alert = new AlertDialog.Builder(CustomListViewAndroidExample.this);
                alert.setTitle("Error Logging in");
                alert.setMessage("Insert Fail");
                alert.setNeutralButton("Ok",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub

                    }
                });


              alert.show();

            }
        }


        Log.i("response", response);



    }

}

【问题讨论】:

  • 哪一个是孤独的162?
  • AlertDialog.Builder alert = new AlertDialog.Builder(this); .这是第 162 行
  • 试试 CustomListViewAndroidExample.this 或 getparent()
  • @user3032822 活动片段中的代码在哪里??
  • @Raghunandan >> 我不明白你的意思:(

标签: android android-alertdialog


【解决方案1】:
alert.show();

像这样修改你的代码......

alert.create().show();

【讨论】:

  • 没有必要。 Show() 创建并立即显示对话框。
【解决方案2】:

改变

 AlertDialog.Builder alert = new AlertDialog.Builder(this);

进入

 AlertDialog.Builder alert = new AlertDialog.Builder(yourActivityName.this);
 AlertDialog dialog = alert.create();
 dialog.show();

【讨论】:

    【解决方案3】:

    您的不同上下文参数会出现此问题。

    如果您的 Activity 使用 Activity 扩展,则使用

     AlertDialog.Builder alert = new AlertDialog.Builder(YourActivity.this);
    

    如果您的 Activity 使用 FragmentActivity 扩展,则使用

     AlertDialog.Builder alert = new AlertDialog.Builder(YourActivityName.this);
    

    如果您使用的是 Fragment,则传递 getActivity()

     AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
    

    编辑:

    最后写

    AlertDialog alertdialog = alert.create();
    alertdialog .show();
    

    【讨论】:

    • 我的活动扩展了 Activity 并且我已经这样做了 myActivity.this 。没有区别:(
    • 它不知道生成器
    • 现在再次检查并获得您的解决方案
    • 您是在自定义适配器类还是自定义视图类中调用对话框?
    • 究竟什么是fragmentContext?片段没有自己的上下文。
    【解决方案4】:

    将 AlertDialog 更改为

    AlertDialog.Builder alert = new AlertDialog.Builder(yourActivityName.this);
    

    像这样称呼它

    AlertDialog showIt =  alert.create()
    
    showIt.show();
    

    【讨论】:

      【解决方案5】:

      您似乎在单击按钮时调用updateData() 方法。

      如果我是对的,那么使用如下:

      AlertDialog.Builder alert = new AlertDialog.Builder(ACTIVITY_CLASS_NAME.this);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-07
        • 1970-01-01
        • 1970-01-01
        • 2017-07-21
        • 1970-01-01
        • 2018-05-23
        • 1970-01-01
        • 2021-08-25
        相关资源
        最近更新 更多