警报Dialog Builder中的空指针异常

时间:2013-12-18 09:39:14

标签: android alertdialog

在这个方法中,我曾经调用过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);



    }

}

5 个答案:

答案 0 :(得分:14)

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

如果您的活动扩展为 活动 ,请使用

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

如果您的活动扩展为 FragmentActivity ,请使用

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

如果您使用的是 片段 ,请传递 getActivity()

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

编辑:

写到最后

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

答案 1 :(得分:2)

更改

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

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

答案 2 :(得分:0)

alert.show();

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

alert.create().show();

答案 3 :(得分:0)

将AlertDialog更改为

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

称之为

AlertDialog showIt =  alert.create()

showIt.show();

答案 4 :(得分:0)

您似乎在按钮上点击了updateData()方法。

如果我是对的,请按以下方式使用:

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