我有MainActivity类和单独的AsyncTask类,我正在从我的主活动类执行。我试图在Main Activity布局上显示对话框,该对话框应该从AsyncTask类onPreExecute方法调用。
我正在尝试实现此代码,主要布局和应用程序停止时没有显示任何对话框。 PLZ帮助:)
这是什么代码看起来像
MainActivity类
public class MainActivity extends Activity implements LoginResponse {
public static Context context_main;
LoginThread login = new LoginThread(context_main);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login.execute();
}
.
.
.
}
和LoginThread类
public class LoginThread extends AsyncTask<Void, Void, RequestToken> {
.
.
.
public LoginThread(Context context)
{
this.context= context;
}
protected void onPreExecute()
{
super.onPreExecute();
pDialog = new ProgressDialog(context);
pDialog.setMessage(Html.fromHtml("<b>Logging In</b><br/>Please wait..."));
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
.
.
.
}
这是logcat msgs
03-12 15:02:10.838: E/Error(32517): I got error
03-12 15:02:10.838: E/Error(32517): java.lang.NullPointerException
03-12 15:02:10.838: E/Error(32517): at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:143)
03-12 15:02:10.838: E/Error(32517): at android.app.AlertDialog.<init>(AlertDialog.java:98)
03-12 15:02:10.838: E/Error(32517): at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
03-12 15:02:10.838: E/Error(32517): at pack.locationinfo.LoginThread.onPreExecute(LoginThread.java:87)
03-12 15:02:10.838: E/Error(32517): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
03-12 15:02:10.838: E/Error(32517): at android.os.AsyncTask.execute(AsyncTask.java:534)
03-12 15:02:10.838: E/Error(32517): at pack.locationinfo.MainActivity.TwitterLogin(MainActivity.java:53)
03-12 15:02:10.838: E/Error(32517): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 15:02:10.838: E/Error(32517): at java.lang.reflect.Method.invoke(Method.java:525)
03-12 15:02:10.838: E/Error(32517): at android.view.View$1.onClick(View.java:3833)
03-12 15:02:10.838: E/Error(32517): at android.view.View.performClick(View.java:4475)
03-12 15:02:10.838: E/Error(32517): at android.view.View$PerformClick.run(View.java:18784)
03-12 15:02:10.838: E/Error(32517): at android.os.Handler.handleCallback(Handler.java:730)
03-12 15:02:10.838: E/Error(32517): at android.os.Handler.dispatchMessage(Handler.java:92)
03-12 15:02:10.838: E/Error(32517): at android.os.Looper.loop(Looper.java:137)
03-12 15:02:10.838: E/Error(32517): at android.app.ActivityThread.main(ActivityThread.java:5414)
03-12 15:02:10.838: E/Error(32517): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 15:02:10.838: E/Error(32517): at java.lang.reflect.Method.invoke(Method.java:525)
03-12 15:02:10.838: E/Error(32517): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
03-12 15:02:10.838: E/Error(32517): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
03-12 15:02:10.838: E/Error(32517): at dalvik.system.NativeStart.main(Native Method)
03-12 15:02:16.108: D/mali_winsys(32517): new_window_surface returns 0x3000
答案 0 :(得分:1)
此行导致崩溃......此处context_main
为null
。
LoginThread login = new LoginThread(context_main);
现在,在LoginThread
方法中初始化onCreate()
对象,如下所示......崩溃正在发生,因为Context
的{{1}}不正确分配
MainActivity
答案 1 :(得分:0)
替换以下
pDialog = new ProgressDialog(context);
以下代码行
pDialog = new ProgressDialog(MainActivity.this);
为我工作