我试图从其他类访问Asynctask。我的问题是将mainActivity的上下文文件传递给Asynctask类以创建进度对话框。 dialog.show();在SendToWebService中抛出nullpointerException
MainActivity
public class MainActivity extends ActionBarActivity implements onTaskCompletion{
onTaskCompletion mCompletion;
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mCompletion = this;
setContentView(R.layout.activity_main);
try {
new SendToWebService(getApplicationContext(), mCompletion).execute();
}catch(Exception e){
String error=e.toString();
String error2=error;
}}
public void onTaskCompleted(String value) {
Toast.makeText(MainActivity.this, value,
Toast.LENGTH_LONG).show();
tv=(TextView)findViewById(R.id.textView1);
tv.setText(value);
}
}
SendToWebServices.java
public class SendToWebService extends AsyncTask<Void, String, String> {
String response;
private Context context;
ProgressDialog dialog;
private onTaskCompletion mCompletion;
public SendToWebService(Context cxt, onTaskCompletion mCompletion) {
context = cxt;
dialog = new ProgressDialog(context);
this.mCompletion=mCompletion;
}
@Override
protected void onPreExecute() {
dialog.setTitle("Please wait");
dialog.show();
}
@Override
protected String doInBackground(Void... unused) {
SystemClock.sleep(2000);
response="Success";
return response; }
@Override
protected void onPostExecute(String response)
{
dialog.dismiss();
try {
mCompletion.onTaskCompleted(response);
}catch (Exception e){
String error=e.toString();
String error2=error;
}
// return response;
}
}
onTaskCompletion.java
接口文件
package com.example.sampleprogressdialog;
public abstract interface onTaskCompletion {
void onTaskCompleted(String response);
// void onTaskCompleted2(String response);
}
xml文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sampleprogressdialog.MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</FrameLayout>
logcat的:
[2014-04-09 11:41:40 - SampleProgressDialog] Uploading SampleProgressDialog.apk onto device 'emulator-5554'
[2014-04-09 11:41:41 - SampleProgressDialog] Installing SampleProgressDialog.apk...
[2014-04-09 11:41:52 - SampleProgressDialog] Success!
[2014-04-09 11:41:52 - SampleProgressDialog] Starting activity com.example.sampleprogressdialog.MainActivity on device emulator-5554
[2014-04-09 11:42:14 - SampleProgressDialog] Success!
[2014-04-09 11:42:15 - SampleProgressDialog] Starting activity com.example.sampleprogressdialog.MainActivity on device emulator-5554
[2014-04-09 11:42:18 - SampleProgressDialog] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.sampleprogressdialog/.MainActivity }
答案 0 :(得分:2)
试试这个
替换此行:
发件人强>
new SendToWebService(getApplicationContext(), mCompletion).execute();
要强>
new SendToWebService(this, mCompletion).execute();
答案 1 :(得分:2)
只需从
更改new SendToWebService(getApplicationContext(), mCompletion).execute();
到
new SendToWebService(MainActivity.this, mCompletion).execute();
因为Dialog
需要activity
上下文而不是application
上下文。