andriod 错误:Only the original thread that created a view hierarchy can touch its views——Handler的使用(代码
Posted gisoracle
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了andriod 错误:Only the original thread that created a view hierarchy can touch its views——Handler的使用(代码相关的知识,希望对你有一定的参考价值。
package com.example.yanlei.myapplication; import android.media.MediaMetadataRetriever; import android.media.MediaPlayer; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.content.pm.ActivityInfo; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.SeekBar; import android.widget.TextView; import android.widget.Toast; import android.widget.VideoView; import android.app.Activity; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.media.MediaPlayer.OnErrorListener; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.SeekBar; import android.widget.Toast; import android.widget.VideoView; import android.widget.SeekBar.OnSeekBarChangeListener; import java.io.File; import java.util.HashMap; import android.os.Handler;
private Handler handler = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); seekBar = (SeekBar) findViewById(R.id.seekBar); // 为进度条添加进度更改事件 seekBar.setOnSeekBarChangeListener(change); //创建属于主线程的handler handler = new Handler(); }
protected void play(int msec) { Log.i(TAG, " 获取视频文件地址"); String path = et_path.getText().toString().trim(); File file = new File(path); if (!file.exists()) { Toast.makeText(this, "视频文件路径错误", 0).show(); return; } Log.i(TAG, "指定视频源路径"); vv_video.setVideoPath(file.getAbsolutePath()); Log.i(TAG, "开始播放"); vv_video.start(); // 按照初始位置播放 vv_video.seekTo(msec); // 设置进度条的最大进度为视频流的最大播放时长 final int time = getTimeLong(file.getAbsolutePath()) / 100; seekBar.setMax(time); Toast.makeText(this, "长度:" + time, Toast.LENGTH_LONG).show(); // 开始线程,更新进度条的刻度 new Thread() { @Override public void run() { try { isPlaying = true; while (isPlaying) { // 如果正在播放,0.1.毫秒更新一次进度条 final int current = vv_video.getCurrentPosition() / 100; seekBar.setProgress(current); /* try { myText = "总时间:" + time / 10 + "秒,目前:" + current / 10 + "秒"; // pTextView.setText("总时间:" + time / 10 + "秒,目前:" + current / 10 + "秒"); new WorkThread().start(); } catch (Exception ex) { Log.d("错误:", ex.getMessage().toString()); }*/ new Thread() { public void run() { myText = "总时间:" + time / 10 + "秒,目前:" + current / 10 + "秒"; handler.post(updateUIRunnable); } }.start(); //Toast.makeText(this, "当前:"+current,Toast.LENGTH_LONG).show(); sleep(100); } } catch (Exception e) { e.printStackTrace(); } } }.start(); // 播放之后设置播放按钮不可用 btn_play.setEnabled(false); vv_video.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { // 在播放完毕被回调 btn_play.setEnabled(true); } }); vv_video.setOnErrorListener(new OnErrorListener() { @Override public boolean onError(MediaPlayer mp, int what, int extra) { // 发生错误重新播放 play(0); isPlaying = false; return false; } }); } //工作线程 private class WorkThread extends Thread { @Override public void run() { try { pTextView.setText(myText); } catch (Exception ex) { Log.e("严重错误:", ex.getMessage().toString()); } } } // 构建Runnable对象,并在runnable中更新UI Runnable updateUIRunnable = new Runnable() { @Override public void run() { try { pTextView.setText(myText); } catch (Exception ex) { Log.e("严重错误:", ex.getMessage().toString()); } } };
以上是关于andriod 错误:Only the original thread that created a view hierarchy can touch its views——Handler的使用(代码的主要内容,如果未能解决你的问题,请参考以下文章
Andriod Studio 出现An error has occurred.See the log file错误解决
错误记录手机中调试程序报错 ( The application could not be installed: INSTALL_FAILED_TEST_ONLY )
错误记录手机中调试程序报错 ( The application could not be installed: INSTALL_FAILED_TEST_ONLY )
浏览器报406 错误:The resource identified by this request is only capable of generating responses with char
VirtualBox Host-only Adapter,Failed to create the host-only adapter 转
the condition has length > 1 and only the first element will be used