布尔值不上点击取消按钮改变 - 如何停止的AsyncTask - Android的? [英] Boolean value does NOT change on clicking cancel button - How to stop AsyncTask - Android?
问题描述
我是初学者,所以请清楚地解释你的答案。谢谢
编辑2 的
我现在有
公共类MainActivity延伸活动{ //其他声明
公共ReadTask readTask;
保护无效的onCreate(捆绑savedInstanceState){
readTask =新ReadTask(); 私人OnClickListener测试=新OnClickListener()
{
@覆盖
公共无效的onClick(视图V)
{
readTask.execute();
//新ReadTask()执行(); } };
私人OnClickListener取消=新OnClickListener()
{
@覆盖
公共无效的onClick(视图V)
{
readTask.cancel(真);
results1.setText();
}
检查EDIT 1本code以上(编辑2)引发错误;
谢谢
我在App取消按钮。在点击这个,这个是什么
发生;
块引用>公共无效的onClick(视图V)
{
InputMethodManager inputManager =(InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus()。getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS); 新ReadTask()onCancelled()。
新ReadTask()取消(真);
results1.setText(取消);
}ReadTask延伸AsyncTask的这样;
公共类ReadTask扩展的AsyncTask<无效,字符串,太虚>
{
读者的BufferedReader = NULL;
公共挥发性布尔isCancelled = FALSE;
//其他声明
保护无效onCancelled(){
super.onCancelled();
isCancelled = TRUE;
新ReadTask()取消(真);
} @覆盖
保护无效doInBackground(无效... PARAMS)
{
// isCancelled = FALSE;
尝试{
为(中间体K = X1 [3]; K&下; = X2 [3]; k ++)
{
//一堆code的
Log.w(5.newString,newString);
字符串ISC1 =将String.valueOf(isCancelled);
Log.w(5.Is取消了?,ISC1); //在这一点上,值始终是假的! 如果(isCancelled()|| isCancelled ==真){Log.d(输入,为什么);打破;} //一堆code的 } //一堆捕捞量有2种方法在这里试过,一个布尔变量isCancelled和其他ReadTask()取消(真)。 - 两个不工作。为什么呢?
我是一个新人,所以请清楚地解释你的答案。谢谢
**************编辑1 - logcat的输出*******
11-27 17:18:56.626:W / dalvikvm(28775):主题ID = 1:螺纹未捕获的异常退出(组= 0x41bcee48)
11-27 17:18:56.636:E / AndroidRuntime(28775):致命异常:主要
11-27 17:18:56.636:E / AndroidRuntime(28775):工艺:com.example.pingtest,PID:28775
11-27 17:18:56.636:E / AndroidRuntime(28775):了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.pingtest / com.example.pingtest.MainActivity}:显示java.lang.NullPointerException
11-27 17:18:56.636:E / AndroidRuntime(28775):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2202)
11-27 17:18:56.636:E / AndroidRuntime(28775):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252)
11-27 17:18:56.636:E / AndroidRuntime(28775):在android.app.ActivityThread.access $ 800(ActivityThread.java:139)
11-27 17:18:56.636:E / AndroidRuntime(28775):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1200)
11-27 17:18:56.636:E / AndroidRuntime(28775):在android.os.Handler.dispatchMessage(Handler.java:102)
11-27 17:18:56.636:E / AndroidRuntime(28775):在android.os.Looper.loop(Looper.java:136)
11-27 17:18:56.636:E / AndroidRuntime(28775):在android.app.ActivityThread.main(ActivityThread.java:5103)
11-27 17:18:56.636:E / AndroidRuntime(28775):在java.lang.reflect.Method.invokeNative(本机方法)
11-27 17:18:56.636:E / AndroidRuntime(28775):在java.lang.reflect.Method.invoke(Method.java:515)
11-27 17:18:56.636:E / AndroidRuntime(28775):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:790)
11-27 17:18:56.636:E / AndroidRuntime(28775):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
11-27 17:18:56.636:E / AndroidRuntime(28775):在dalvik.system.NativeStart.main(本机方法)
11-27 17:18:56.636:E / AndroidRuntime(28775):致:显示java.lang.NullPointerException
。11-27 17:18:56.636:E / AndroidRuntime(28775):在com.example.pingtest.MainActivity $ ReadTask<&初始化GT;(MainActivity.java:120)
11-27 17:18:56.636:E / AndroidRuntime(28775):在com.example.pingtest.MainActivity.onCreate(MainActivity.java:39)
11-27 17:18:56.636:E / AndroidRuntime(28775):在android.app.Activity.performCreate(Activity.java:5275)
11-27 17:18:56.636:E / AndroidRuntime(28775):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-27 17:18:56.636:E / AndroidRuntime(28775):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2166)
11-27 17:18:56.636:E / AndroidRuntime(28775):... 11个
11-27 17:19:05.665:I /流程(28775):发送信号。 PID:28775 SIG:9解决方案您好我做出了一些改变在code。与解释,请查看
公共类ASYN延伸活动{ 公共ReadTask readTask;
//保持全局的prevent上每一次点击按钮建立新的异步任务
布尔isCancelled = TRUE;
结果1 TextView的;
按钮启动,结束; 保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); 的setContentView(R.layout.qww);
启动=(按钮)findViewById(R.id.button1);
结束=(按钮)findViewById(R.id.button2);
start.setOnClickListener(试验);
end.setOnClickListener(取消);
结果1 =(的TextView)findViewById(R.id.textView1);
} 私人OnClickListener测试=新OnClickListener(){
@覆盖
公共无效的onClick(视图V){
如果(isCancelled){//检查prevent创造新的异步任务
//每次点击按钮
Toast.makeText(Asyn.this,新建任务已启动,Toast.LENGTH_LONG)
。显示();
readTask =新ReadTask();
readTask.execute();
isCancelled = FALSE;
}其他{
Toast.makeText(Asyn.this,任务已经乳宁
Toast.LENGTH_LONG).show();
} } };
私人OnClickListener取消=新OnClickListener(){
@覆盖
公共无效的onClick(视图V){
如果(NULL!= readTask){
Toast.makeText(Asyn.this,任务停,Toast.LENGTH_LONG)
。显示();
readTask.cancel(真);
isCancelled = TRUE;
}
results1.setText();
}
}; 私有类ReadTask扩展的AsyncTask<无效,字符串,太虚> {
保护无效onProgressUpdate(字符串值...){
super.onProgressUpdate(值);
results1.setText(值[0]的ToString());
}
@覆盖
保护无效doInBackground(虚空...... PARAMS){ 尝试{
对于(INT K = 0; K< = 10; k ++){
//一堆code的
Log.w(5.newString,K +);
字符串ISC1 =将String.valueOf(isCancelled);
Log.w(5.Is取消了?,ISC1); //在这点上,值是
//下面尽快线即时更新为值改变
publishProgress(+ K +); //使用线程看到更新,否则将其删除
视频下载(2000);
如果(isCancelled()|| isCancelled ==真){
Log.d(输入,为什么);
打破;
} //一堆code的 }
}赶上(例外五){
e.printStackTrace();
}
返回null; } @覆盖
保护无效onPostExecute(虚空结果){
// TODO自动生成方法存根
super.onPostExecute(结果);
//指定真实的isCancelled,这样用户可以重新开始任务
isCancelled = TRUE;
}
}
}I am a beginner, so please explain your answer clearly. Thanks
EDIT 2 I now have
public class MainActivity extends Activity { //other declarations public ReadTask readTask; protected void onCreate(Bundle savedInstanceState){ readTask = new ReadTask(); private OnClickListener test = new OnClickListener() { @Override public void onClick (View V) { readTask.execute(); //new ReadTask().execute(); } }; private OnClickListener cancel = new OnClickListener() { @Override public void onClick (View V) { readTask.cancel(true); results1.setText(""); }
Check EDIT 1 for the error this code above (Edit 2) throws; Thanks
I have a cancel button in the App. On clicking this, this is what happens;
public void onClick (View V) { InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); new ReadTask().onCancelled(); new ReadTask().cancel(true); results1.setText("Cancelled"); }
ReadTask extends AsyncTask like this;
public class ReadTask extends AsyncTask<Void, String, Void> { BufferedReader reader = null; public volatile boolean isCancelled=false; //other declarations protected void onCancelled() { super.onCancelled(); isCancelled = true; new ReadTask().cancel(true); } @Override protected Void doInBackground(Void... params) { // isCancelled=false; try { for(int k=x1[3];k<=x2[3];k++) { //bunch of code Log.w("5.newString", newString); String isC1 = String.valueOf(isCancelled); Log.w("5.Is cancelled?", isC1); // at this point, VALUE IS ALWAYS FALSE ! if (isCancelled() || isCancelled == true) {Log.d("Entered","WHY");break;} //bunch of code } //bunch of catches
There are 2 methods tried here, one a boolean variable isCancelled and the other ReadTask().cancel(true); - Both don't work. Why?
I am a newcomer, so please explain your answer clearly. Thanks
************** EDIT 1 - Logcat Output*******
11-27 17:18:56.626: W/dalvikvm(28775): threadid=1: thread exiting with uncaught exception (group=0x41bcee48) 11-27 17:18:56.636: E/AndroidRuntime(28775): FATAL EXCEPTION: main 11-27 17:18:56.636: E/AndroidRuntime(28775): Process: com.example.pingtest, PID: 28775 11-27 17:18:56.636: E/AndroidRuntime(28775): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pingtest/com.example.pingtest.MainActivity}: java.lang.NullPointerException 11-27 17:18:56.636: E/AndroidRuntime(28775): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2202) 11-27 17:18:56.636: E/AndroidRuntime(28775): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252) 11-27 17:18:56.636: E/AndroidRuntime(28775): at android.app.ActivityThread.access$800(ActivityThread.java:139) 11-27 17:18:56.636: E/AndroidRuntime(28775): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200) 11-27 17:18:56.636: E/AndroidRuntime(28775): at android.os.Handler.dispatchMessage(Handler.java:102) 11-27 17:18:56.636: E/AndroidRuntime(28775): at android.os.Looper.loop(Looper.java:136) 11-27 17:18:56.636: E/AndroidRuntime(28775): at android.app.ActivityThread.main(ActivityThread.java:5103) 11-27 17:18:56.636: E/AndroidRuntime(28775): at java.lang.reflect.Method.invokeNative(Native Method) 11-27 17:18:56.636: E/AndroidRuntime(28775): at java.lang.reflect.Method.invoke(Method.java:515) 11-27 17:18:56.636: E/AndroidRuntime(28775): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) 11-27 17:18:56.636: E/AndroidRuntime(28775): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606) 11-27 17:18:56.636: E/AndroidRuntime(28775): at dalvik.system.NativeStart.main(Native Method) 11-27 17:18:56.636: E/AndroidRuntime(28775): Caused by: java.lang.NullPointerException 11-27 17:18:56.636: E/AndroidRuntime(28775): at com.example.pingtest.MainActivity$ReadTask.<init>(MainActivity.java:120) 11-27 17:18:56.636: E/AndroidRuntime(28775): at com.example.pingtest.MainActivity.onCreate(MainActivity.java:39) 11-27 17:18:56.636: E/AndroidRuntime(28775): at android.app.Activity.performCreate(Activity.java:5275) 11-27 17:18:56.636: E/AndroidRuntime(28775): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 11-27 17:18:56.636: E/AndroidRuntime(28775): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2166) 11-27 17:18:56.636: E/AndroidRuntime(28775): ... 11 more 11-27 17:19:05.665: I/Process(28775): Sending signal. PID: 28775 SIG: 9
解决方案Hi i made some changes in your code with explanation please check.
public class Asyn extends Activity { public ReadTask readTask; // keep global to prevent creating new async task on every time button click boolean isCancelled = true; TextView results1; Button start, end; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.qww); start = (Button) findViewById(R.id.button1); end = (Button) findViewById(R.id.button2); start.setOnClickListener(test); end.setOnClickListener(cancel); results1 = (TextView) findViewById(R.id.textView1); } private OnClickListener test = new OnClickListener() { @Override public void onClick(View V) { if (isCancelled) { // checked to prevent creating new async task on // every time button click Toast.makeText(Asyn.this, "new task started", Toast.LENGTH_LONG) .show(); readTask = new ReadTask(); readTask.execute(); isCancelled = false; } else { Toast.makeText(Asyn.this, "Task already runing", Toast.LENGTH_LONG).show(); } } }; private OnClickListener cancel = new OnClickListener() { @Override public void onClick(View V) { if (null != readTask) { Toast.makeText(Asyn.this, "Task stop", Toast.LENGTH_LONG) .show(); readTask.cancel(true); isCancelled = true; } results1.setText(""); } }; private class ReadTask extends AsyncTask<Void, String, Void> { protected void onProgressUpdate(String... values) { super.onProgressUpdate(values); results1.setText(values[0].toString()); } @Override protected Void doInBackground(Void... params) { try { for (int k = 0; k <= 10; k++) { // bunch of code Log.w("5.newString", k + ""); String isC1 = String.valueOf(isCancelled); Log.w("5.Is cancelled?", isC1); // at this point, VALUE IS //below line for instant update as soon as the value is changed publishProgress(" "+k + ""); // use thread to see the update otherwise remove it Thread.sleep(2000); if (isCancelled() || isCancelled == true) { Log.d("Entered", "WHY"); break; } // bunch of code } } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void result) { // TODO Auto-generated method stub super.onPostExecute(result); // assign true to isCancelled so that user can start task again isCancelled = true; } } }
这篇关于布尔值不上点击取消按钮改变 - 如何停止的AsyncTask - Android的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!