ViewPager2 自动轮播线程发生异常
老师,我是跟着课程 综合案例:慕淘旅游--》指示器:自动滑动版 做的,和课程里没区别呀,课程里的线程就能运行修改UI?
我的代码:
package com.example.mydemo.fragment;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleAdapter;
import com.example.mydemo.R;
import com.example.mydemo.util.DataUtil;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class IndexFragment extends Fragment {
private ViewPager2 pagers;
private LinearLayout pointers;
private int index = 1;//用于保存一个数字,控制ViewPager的显示页数
private GridView grids;
private List<Map<String,Object>> data=new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_index, container, false);
pagers = view.findViewById(R.id.pagers);
pointers = view.findViewById(R.id.pointers);
grids=view.findViewById(R.id.grids);
setViewPager();
setGridView();
return view;
}
private void setGridView() {
for(int i=0;i<DataUtil.index_menu_imgs.length;i++){
Map<String,Object> map=new HashMap<>();
map.put("img",DataUtil.index_menu_imgs[i]);
map.put("txt",DataUtil.index_menu_txts[i]);
data.add(map);
}
String[] from={"img","txt"};
int[] to={R.id.img,R.id.txt};
SimpleAdapter adapter=new SimpleAdapter(getContext(),data,R.layout.index_menu_item,from,to);
grids.setAdapter(adapter);
}
private void setViewPager() {
RecyclerView.Adapter adapter = new RecyclerView.Adapter() {
//用于获取盛放图片的控件的ViewHolder对象
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(getContext()).inflate(R.layout.adv_item, parent, false);
return new ViewHolder(v);
}
//为ViewHolder对象中的控件设置显示效果
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
ViewHolder vh = (ViewHolder) holder;
vh.v.setBackgroundResource(DataUtil.adv_imgs[position % 4]);
}
//设置pager的数量
@Override
public int getItemCount() {
return Integer.MAX_VALUE;
}
};
pagers.setAdapter(adapter);
pagers.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
for (int i = 0; i < pointers.getChildCount(); i++) {
ImageView img = (ImageView) pointers.getChildAt(i);
img.setImageResource(R.drawable.dot_unselected);
}
((ImageView) pointers.getChildAt(position % 4)).setImageResource(R.drawable.dot_selected);
}
}) ;
//开启一个线程,控制每3秒切换一个广告
new Thread() {
@Override
public void run() {
super.run();
while (true) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
pagers.setCurrentItem(index);
index++;
}
}
}.start();
}
class ViewHolder extends RecyclerView.ViewHolder {
public View v;
public ViewHolder(@NonNull View itemView) {
super(itemView);
v = itemView;
}
}
}
log异常报告:
2020-01-30 13:00:55.546 3551-3657/com.example.mydemo E/AndroidRuntime: FATAL EXCEPTION: Thread-3
Process: com.example.mydemo, PID: 3551
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6855)
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1075)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:5242)
at android.view.View.invalidateInternal(View.java:13574)
at android.view.View.invalidate(View.java:13538)
at android.view.View.invalidate(View.java:13522)
at android.widget.ImageView.setImageDrawable(ImageView.java:514)
at androidx.appcompat.widget.AppCompatImageView.setImageDrawable(AppCompatImageView.java:99)
at androidx.appcompat.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:90)
at androidx.appcompat.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:93)
at com.example.mydemo.fragment.IndexFragment$2.onPageSelected(IndexFragment.java:100)
at androidx.viewpager2.widget.CompositeOnPageChangeCallback.onPageSelected(CompositeOnPageChangeCallback.java:73)
at androidx.viewpager2.widget.CompositeOnPageChangeCallback.onPageSelected(CompositeOnPageChangeCallback.java:73)
at androidx.viewpager2.widget.ScrollEventAdapter.dispatchSelected(ScrollEventAdapter.java:331)
at androidx.viewpager2.widget.ScrollEventAdapter.notifyProgrammaticScroll(ScrollEventAdapter.java:266)
at androidx.viewpager2.widget.ViewPager2.setCurrentItem(ViewPager2.java:449)
at androidx.viewpager2.widget.ViewPager2.setCurrentItem(ViewPager2.java:412)
at com.example.mydemo.fragment.IndexFragment$3.run(IndexFragment.java:118)
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 1789 份
- 解答问题 2907 个
Android大楼Java起,本阶段是Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始入门Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星