[英]Dialogbox not displaying on MainActivity called from AsyncTask class
I have MainActivity class and separate AsyncTask class, which i am executing from my main activity class. 我有MainActivity类和单独的AsyncTask类,它们是从我的主要活动类执行的。 I am trying to display dialog box on Main Activity layout which should be called from AsyncTask class onPreExecute method.
我正在尝试在Main Activity布局上显示对话框,该对话框应从AsyncTask类的onPreExecute方法调用。
I am trying to implement this code, no dialog box is showing on main layout and app stops. 我正在尝试实现此代码,主布局和应用程序停止处均未显示任何对话框。 plz help :)
请帮助:)
here is what code looks like 这是代码的样子
MainActivity class 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();
}
.
.
.
}
and LoginThread Class 和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();
}
.
.
.
}
this is logcat msgs 这是logcat消息
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
This line causing the crash...here, context_main
is null
. 这行导致崩溃...此处,
context_main
为null
。
LoginThread login = new LoginThread(context_main);
Now, initialize the LoginThread
object inside the onCreate()
method as below...The crash is happening because, the Context
of MainActivity
isn't properly assigned. 现在,按如下所示在
onCreate()
方法中初始化LoginThread
对象...发生崩溃是因为MainActivity
的Context
未正确分配。
LoginThread login;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = new LoginThread(this);
}
Replace following 替换以下
pDialog = new ProgressDialog(context);
with following line of code 与下面的代码行
pDialog = new ProgressDialog(MainActivity.this);
Works for me 为我工作
You need to move this inside onCreate 您需要在onCreate中移动它
LoginThread login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = new LoginThread(this);
// can use this as it refers to Activity context
... //rest of the code
login.execute();
Context
is available once Activity is created 创建活动后即可使用
Context
在oncreate方法中初始化context_main = this并将值传递给构造函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.