作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Main Activity Oncreate 方法每当我尝试运行这段代码时它都会给我这个错误,尽管我已经添加了 runonui Thread 。但它仍然无法正常工作,我们将不胜感激。我已经搜索了很多,但真的没有找到任何东西
@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)
最佳答案
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();
}
}
};
performClick
必须在 ui 线程上调用,这就是您获得异常的原因。如果你想在调用 performClick
之前模拟等待 5 秒,你可以使用按钮的处理程序及其 postDelayed方法:
final Button buttonTakePicture = (Button)findViewById(R.id.takepicture);
buttonTakePicture.postDelayed(new Runnable() {
public void run() {
buttonTakePicture.performClick();
}
}, 5000);
关于android - 只有创建 View 层次结构的原始线程才能触及它的 View 。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29576772/
我正在尝试创建一个 React 组件来抽象化为我的表单创建一个输入组。所有输入都具有相同的布局 - 一个标签,下面是输入,如果存在错误/信息文本,它们将显示在输入下。 以前我处理自己的表单状态/处理程
我正在尝试将子 shell 的输出行读取到数组中,但我不愿意设置 IFS,因为它是全局的。我不希望脚本的一部分影响后面的部分,因为那是不好的做法,我拒绝这样做。在命令后恢复 IFS 不是一个选项,因为
我是一名优秀的程序员,十分优秀!