android中出现Only the original thread that created a view hierarchy can touch its views.错误解决方案

/**
	 * kill the process onclick listener
	 * @param view
	 */
	public void killProcess(View view){
		int total = 0;
		long memorySize = 0;
		for (TaskInfo userTaskInfo : usertaskinfos) {
			if(userTaskInfo.isChecked()){
				manager.killBackgroundProcesses(userTaskInfo.getPackName());
				total ++;
				memorySize += userTaskInfo.getMemorySize();
			}
		}
		for (TaskInfo systemTaskInfo : systemtaskinfos) {
			if(systemTaskInfo.isChecked()){
				manager.killBackgroundProcesses(systemTaskInfo.getPackName());
				total++;
				memorySize += systemTaskInfo.getMemorySize();
			}
		}
		
		Toast.makeText(getApplicationContext(), "清理了"+total+"个进程"+"释放了" + TextForMater.getKBDataSize(memorySize)+"M内存", 0).show();
		
		fillData();
	}

在Activity中调用以上方法;

private void fillData() {
		
		setTitleData();
		
		new Thread(){
			public void run() {
				ll_task_manager_loading = (LinearLayout) TaskManagerActivity.this.findViewById(R.id.ll_task_manager_loading);
				ll_task_manager_loading.setVisibility(View.VISIBLE);
				provider = new TaskInfoProvider(TaskManagerActivity.this);
				taskInfos = provider.getAllTasks(runningProcess);
				
				Message msg = new Message();
				msg.what = GET_ALL_TASK_FINISH;
				
				handler.sendMessage(msg);
			};
		}.start();
	}

kill方法中调用了子线程的方法,出错原因是ll_task_manager_loading是主线程中的对象,不能再子线程里调用,因为没其生命周期已经结束。

参考


















你可能感兴趣的:(android中出现Only the original thread that created a view hierarchy can touch its views.错误解决方案)