I have MainActivity class and separate AsyncTask class, which i am executing from my main activity class. I am trying to display dialog box on Main Activity layout which should be called from AsyncTask class onPreExecute method.
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
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
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
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
.
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.
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
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
在oncreate方法中初始化context_main = this并将值传递给构造函数。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.