主要活动Oncreate方法每当我尝试运行此代码时它会给我这个错误,虽然我已经添加了runonui线程。但它仍然没有工作任何帮助将不胜感激。我已经搜索了很多,但实际上没有找到任何东西
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
DevicePolicyAdmin = new ComponentName(this,
Dprcv.class);
// btntake =(Button)findViewById(R.id.takepicture);
truitonAdminEnabledCheckbox = (CheckBox) findViewById(R.id.checkBox);
//start
// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
controlInflater = LayoutInflater.from(getBaseContext());
View viewControl = controlInflater.inflate(R.layout.control, null);
ViewGroup.LayoutParams layoutParamsControl
= new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
this.addContentView(viewControl, layoutParamsControl);
final Button buttonTakePicture = (Button)findViewById(R.id.takepicture);
buttonTakePicture.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
camera.takePicture(myShutterCallback,
myPictureCallback_RAW, myPictureCallback_JPG);
}
});
runOnUiThread(new Runnable() {
@Override
public void run() {
final Button buttonTakePicture = (Button)findViewById(R.id.takepicture);
Thread tr = new Thread(){
public void run(){
try{
final Button buttonTakePicture = (Button)findViewById(R.id.takepicture);
sleep(5000);
buttonTakePicture.performClick();
}
catch (InterruptedException ex)
{
ex.printStackTrace();
}
}
};
tr.start();
}
});
这是logcat
04-11 15:19:49.629 6343-6360/com.ahmed.raja.wrongclick E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-4119
Process: com.ahmed.raja.wrongclick, PID: 6343
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6669)
at android.view.ViewRootImpl.playSoundEffect(ViewRootImpl.java:5642)
at android.view.View.playSoundEffect(View.java:17278)
at android.view.View.performClick(View.java:4462)
at com.ahmed.raja.wrongclick.MainActivity$2$1.run(MainActivity.java:107)
答案 0 :(得分:1)
Thread tr = new Thread(){
public void run(){
try{
final Button buttonTakePicture = (Button)findViewById(R.id.takepicture);
sleep(5000);
buttonTakePicture.performClick();
}
catch (InterruptedException ex) {
ex.printStackTrace();
}
}
};
必须在ui线程上调用 performClick
,这就是你获得异常的原因。如果您想在呼叫performClick
之前模拟等待5秒,您可以使用按钮的处理程序及其postDelayed方法:
final Button buttonTakePicture = (Button)findViewById(R.id.takepicture);
buttonTakePicture.postDelayed(new Runnable() {
public void run() {
buttonTakePicture.performClick();
}
}, 5000);
答案 1 :(得分:1)
这是因为你在调用tr.start()时在runOnUiThread方法中启动新线程;这意味着这段代码:
final Button buttonTakePicture = (Button)findViewById(R.id.takepicture);
sleep(5000);
buttonTakePicture.performClick();
不在ui线程中运行。据我所知,你想要执行的代码点击延迟。您可以通过以下方式完成此操作:
buttonTakePicture.postDelayed