Reputation: 87
I am making an app that uses AsyncTask and I want it to use two thread that one of them is asyncTask class and another one is thread class (this is in asyncTask),and the second thread works when I press start button. I want them works all together. I ran the app and asyncTask thread works fine but when I press button to start second thread that is in synckTask thread , unfortunately the app stopped . here is the code :
public class MainActivity extends Activity {
private Thread thread;
private Button btnStart;
private TextView textView;
private Async a;
int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.textEdit);
btnStart=(Button)findViewById(R.id.btnStart);
btnStart.setOnClickListener(onStart);
a=new Async();
a.execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class Async extends AsyncTask< String, Long , Void >{
@Override
protected void onPreExecute() {
textView.setText("has started ... ");
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Long... values) {
thread=new Thread(){
@Override
public void run() {
while(i<6) {
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(String.valueOf(i));
Log.w(" onPostExecute ", String.valueOf(i));
i++;
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
super.run();
}
};
super.onProgressUpdate(values);
}
@Override
protected Void doInBackground(String... params) {
for (int i = 0; i < 12 ; i++) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("count index in background ", String.valueOf(i));
}
return null;
}
@Override
protected void onPostExecute(Void result) {
}
}
private View.OnClickListener onStart=new View.OnClickListener(){
@Override
public void onClick(View v) {
thread.start();
}
};
}
please guide me how can I fix it for working fine...
thanks alot...
and here is its log:
03-17 07:35:35.913: D/libEGL(1113): loaded /system/lib/egl/libEGL_emulation.so
03-17 07:35:35.943: D/(1113): HostConnection::get() New Host Connection established 0x2a154d20, tid 1113
03-17 07:35:35.963: D/libEGL(1113): loaded /system/lib/egl/libGLESv1_CM_emulation.so
03-17 07:35:35.963: D/libEGL(1113): loaded /system/lib/egl/libGLESv2_emulation.so
03-17 07:35:36.133: W/EGL_emulation(1113): eglSurfaceAttrib not implemented
03-17 07:35:36.133: I/count index in background(1113): 0
03-17 07:35:36.144: D/OpenGLRenderer(1113): Enabling debug mode 0
03-17 07:35:36.404: I/count index in background(1113): 1
03-17 07:35:36.661: I/count index in background(1113): 2
03-17 07:35:36.865: I/count index in background(1113): 3
03-17 07:35:37.068: I/count index in background(1113): 4
03-17 07:35:37.272: I/count index in background(1113): 5
03-17 07:35:37.480: I/count index in background(1113): 6
03-17 07:35:37.683: I/count index in background(1113): 7
03-17 07:35:37.891: I/count index in background(1113): 8
03-17 07:35:38.223: I/count index in background(1113): 9
03-17 07:35:38.631: I/count index in background(1113): 10
03-17 07:35:38.837: I/count index in background(1113): 11
03-17 07:35:41.403: D/AndroidRuntime(1113): Shutting down VM
03-17 07:35:41.403: W/dalvikvm(1113): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-17 07:35:41.443: E/AndroidRuntime(1113): FATAL EXCEPTION: main
03-17 07:35:41.443: E/AndroidRuntime(1113): java.lang.NullPointerException
03-17 07:35:41.443: E/AndroidRuntime(1113): at com.example.asynctasckdemo.MainActivity$1.onClick(MainActivity.java:104)
03-17 07:35:41.443: E/AndroidRuntime(1113): at android.view.View.performClick(View.java:4204)
03-17 07:35:41.443: E/AndroidRuntime(1113): at android.view.View$PerformClick.run(View.java:17355)
03-17 07:35:41.443: E/AndroidRuntime(1113): at android.os.Handler.handleCallback(Handler.java:725)
03-17 07:35:41.443: E/AndroidRuntime(1113): at android.os.Handler.dispatchMessage(Handler.java:92)
03-17 07:35:41.443: E/AndroidRuntime(1113): at android.os.Looper.loop(Looper.java:137)
03-17 07:35:41.443: E/AndroidRuntime(1113): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-17 07:35:41.443: E/AndroidRuntime(1113): at java.lang.reflect.Method.invokeNative(Native Method)
03-17 07:35:41.443: E/AndroidRuntime(1113): at java.lang.reflect.Method.invoke(Method.java:511)
03-17 07:35:41.443: E/AndroidRuntime(1113): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-17 07:35:41.443: E/AndroidRuntime(1113): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-17 07:35:41.443: E/AndroidRuntime(1113): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 69
Reputation: 17401
@Override
protected Void doInBackground(String... params) {
for (int i = 0; i < 12 ; i++) {
try {
Thread.sleep(200);
publishProgress();//try calling this<------------------------------
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("count index in background ", String.valueOf(i));
}
return null;
}
Upvotes: 1