您好我是Android的新手,我得到Null指针Exception.can任何身体告诉我这段代码的问题 代码如下。
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(mContext);
//pd.setTitle("Sending Request !");
pd.setMessage(ActivityStrings.PROGRESS_MESSAGE_PD);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setCancelable(false);
pd.setCanceledOnTouchOutside(false);
if (!((Activity) mContext).isFinishing()) {
pd.show();
}
}
提前致谢
请参阅此处的log cat
05-31 01:57:12.568: E/AndroidRuntime(8329): FATAL EXCEPTION: main
05-31 01:57:12.568: E/AndroidRuntime(8329): java.lang.NullPointerException
05-31 01:57:12.568: E/AndroidRuntime(8329): at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:142)
05-31 01:57:12.568: E/AndroidRuntime(8329): at android.app.AlertDialog.<init>(AlertDialog.java:98)
05-31 01:57:12.568: E/AndroidRuntime(8329): at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
05-31 01:57:12.568: E/AndroidRuntime(8329): at com.eeshana.android_29042014.AsynkTask.GuestLoginSenderAskTask.onPreExecute(GuestLoginSenderAskTask.java:72)
05-31 01:57:12.568: E/AndroidRuntime(8329): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
05-31 01:57:12.568: E/AndroidRuntime(8329): at android.os.AsyncTask.execute(AsyncTask.java:534)
05-31 01:57:12.568: E/AndroidRuntime(8329): at com.eeshana.android_04012014.checkthisapp.LoginScreen.callGuestLoginThread(LoginScreen.java:138)
05-31 01:57:12.568: E/AndroidRuntime(8329): at com.eeshana.android_04012014.checkthisapp.LoginScreen.onClick(LoginScreen.java:104)
05-31 01:57:12.568: E/AndroidRuntime(8329): at android.view.View.performClick(View.java:4084)
05-31 01:57:12.568: E/AndroidRuntime(8329): at android.os.Handler.handleCallback(Handler.java:615)
05-31 01:57:12.568: E/AndroidRuntime(8329): at android.os.Handler.dispatchMessage(Handler.java:92)
05-31 01:57:12.568: E/AndroidRuntime(8329): at android.os.Looper.loop(Looper.java:137)
05-31 01:57:12.568: E/AndroidRuntime(8329): at android.app.ActivityThread.main(ActivityThread.java:4745)
05-31 01:57:12.568: E/AndroidRuntime(8329): at java.lang.reflect.Method.invokeNative(Native Method)
05-31 01:57:12.568: E/AndroidRuntime(8329): at java.lang.reflect.Method.invoke(Method.java:511)
05-31 01:57:12.568: E/AndroidRuntime(8329): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-31 01:57:12.568: E/AndroidRuntime(8329): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
这是一个简单的逻辑只需使用this.mContext来初始化Context。我会完成......
答案 1 :(得分:0)
鉴于您发布的堆栈跟踪,问题似乎出现在警告对话框的init中,如此行所示:
E/AndroidRuntime(8329): at android.app.AlertDialog.<init>(AlertDialog.java:98)
这可能意味着当执行代码的以下行时,变量mContext为null:
pd = new ProgressDialog(mContext);
解决问题的最合理方法是定义AsyncTask(我假设是异步任务,因为它定义了onPreExecute方法),如下所示:
public class MyAsyncTask extends AsyncTask<String, Void, String> {
private Context mContext;
public MyAsyncTask(Context ctx) {
this.mContext = ctx;
}
@Override
protected String doInBackground(String... params) {
String returnedValue;
//your long-time-taking code goes here.
return returnedValue;
}
@Override
protected void onPostExecute(String result) {
//do something with the returned value and eventually hide the progress bar
pb.setVisibility(View.GONE);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(mContext);
pd.setMessage(ActivityStrings.PROGRESS_MESSAGE_PD);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setCancelable(false);
pd.setCanceledOnTouchOutside(false);
if (!((Activity) mContext).isFinishing()) {
pd.show();
}
}
@Override
protected void onProgressUpdate(Void... values) {
// do something while the task is running, such updating your progress bar..
}
}
并将其初始化(在您的活动中),如下所示:
new MyAsyncTask(this).execute(STRING_PARAMETER);
这样,您将在异步任务中拥有适当的上下文来初始化进度对话框。
这里有几点:
希望这会有所帮助。
答案 2 :(得分:0)
你可以通过检查crush line修复NullPointException使用对象ref是否为null。
然后尝试在调用之前初始化该对象。
这种情况就像mContext&lt; - 这个对象可能是null,你应该在新的ProgressDialog之前初始化mContext。
如果在MainActivity中这样
mContext = MainActivity.this; // <- your context instance.
pd = new ProgressDialog(mContext);
答案 3 :(得分:-1)
嗨,通过查看你的代码我猜你的代码中的mContext问题是由@Wezelkrozum声明的。作为一种解决方案,我可以说你的mContext指向Null。如果您正在使用mContext在各处初始化Context,那么只需执行此操作.mContext = yourContext(Obj)。 我认为这会奏效......