在这种方法中,我用来调用AlertDialog框。但当我调用警报对话框时。我总是有胎儿异常。下面是我在警报对话框生成器中得到的错误。请帮我找一下。
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);
}
}
最佳答案
此问题发生在不同的上下文参数上。
如果您的活动随活动而扩展,则使用
AlertDialog.Builder alert = new AlertDialog.Builder(YourActivity.this);
如果活动扩展为碎片活动,则使用
AlertDialog.Builder alert = new AlertDialog.Builder(YourActivityName.this);
如果使用的是fragment,则传递getActivity()。
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
编辑:
终于写出来了
AlertDialog alertdialog = alert.create();
alertdialog .show();