我与许多其他应用进行了比较。但我找不到我错了。
这就是我所做的
[[我更改了gradle版本28.0.0-beta01> 27.1.1,检查了所有xml文件,清理并重建了项目,并且我看到了许多带有TabLayout的有关Viewpager的视频。]
activity_main(TabLayout + ViewPager), 帮派列表(ListView,id / list), list_item(1 ImageView,2 Textview)
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the view pager that will allow the user to swipe between fragments
ViewPager viewPager = findViewById(R.id.viewpager);
// Create an adapter that knows which fragment should be shown on each page
CategoryAdapter adapter = new CategoryAdapter(this,getSupportFragmentManager());
// Set the adapter onto the view pager
viewPager.setAdapter(adapter);
TabLayout tabLayout= findViewById(R.id.tabs);
// Connect the tab layout with the view pager. This will
// 1. Update the tab layout when the view pager is swiped
// 2. Update the view pager when a tab is selected
// 3. Set the tab layout's tab names with the view pager's adapter's titles
// by calling onPageTitle()
tabLayout.setupWithViewPager(viewPager);
}
}
public class CategoryAdapter extends FragmentPagerAdapter {
private Context mContext;
public CategoryAdapter(Context context, FragmentManager fm) {
super(fm);
mContext = context;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new YeoidoFragment();
case 1:
return new YeoidoFragment();
case 2:
return new YeoidoFragment();
default:
return null;
}
}
public int getCount() {
return 1;
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return mContext.getString(R.string.yeouido_hangang_park);
case 1:
return mContext.getString(R.string.banpo_hangang_park);
case 2:
return mContext.getString(R.string.banpo_hangang_park);
default:
return null;
}
}
}
我正在使用相同的片段进行测试
public class Gang {
private int ImageResourceId;
private String Feature;
private String Explanation;
public Gang(int ImageResourceId, String Feature, String Explanation){
this.ImageResourceId = ImageResourceId;
this.Feature = Feature;
this.Explanation = Explanation;
}
public int getImageResourceId(){return ImageResourceId;}
public void setImageResourceId(int ImageResourceId){
this.ImageResourceId= ImageResourceId;
}
public String getFeature(){return Feature;}
public void setFeature(String Feature){
this.Feature = Feature;
}
public String getExplanation(){return Explanation;}
public void setExplanation(String Explanation){
this.Explanation = Explanation;
}
}
public class GangAdapter extends ArrayAdapter<Gang> {
public GangAdapter(Context context, ArrayList<Gang>gangs){
super(context, 0, gangs);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View listItemView = convertView;
if(listItemView==null){
listItemView= LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
Gang currentFeature = getItem(position);
ImageView spotImageView = listItemView.findViewById(R.id.Image);
spotImageView.setImageResource(currentFeature.getImageResourceId());
TextView featureTextView = listItemView.findViewById(R.id.where);
featureTextView.setText(currentFeature.getFeature());
TextView detailTextView = listItemView.findViewById(R.id.about);
detailTextView.setText(currentFeature.getExplanation());
return listItemView;
}
}
public class YeoidoFragment extends Fragment {
public YeoidoFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView= inflater.inflate(R.layout.ganglist, container, false);
final ArrayList<Gang> gangs = new ArrayList<>();
gangs.add(new Gang(R.drawable.yeouido1, getString(R.string.yeouido),getString(R.string.app_name)));
GangAdapter adapter = new GangAdapter(getActivity(), gangs);
ListView listView= rootView.findViewById(R.id.list);
listView.setAdapter(adapter);
return rootView;
}
}
来自logcat
08-06 21:32:41.805 8882-8882 / com.example.android.hangangabout E / Zygote:isWhitelistProcess-进程被列入白名单08-06 21:32:41.806 8882-8882 / com.example.android.hangang关于W / SELinux: SELinux selinux_android_compute_policy_index:策略索引[2], Con:u:r:zygote:s0 RAM:SEPF_SM-G950N_8.0.0_0005,[-1 -1 -1 -1 0 1] 08-06 21:32:41.807 8882-8882 / com.example.android.hangangabout I / SELinux:SELinux:seapp_context_lookup:seinfo =不受信任, 等级= s0:c512,c768,pkgname = com.example.android.hangangabout 08-06 21:32:41.810 8882-8882 / com.example.android.hangang关于I / zygote64: 后期启用-Xcheck:jni 08-06 21:32:41.878 8882-8882 / com.example.android.hangangabout D / ActivityThread:已添加 TimaKeyStore提供程序08-06 21:32:42.026 8882-8924 / com.example.android.hangang关于I / vndksupport:sphal 未为此过程配置名称空间。载入中 /vendor/lib64/egl/libGLES_mali.so从当前名称空间获取。 08-06 21:32:42.036 8882-8882 / com.example.android.hangangabout I / InstantRun:启动即时运行服务器:是主进程08-06 21:32:42.045 8882-8924 / com.example.android.hangangabout D / libEGL: 加载的/vendor/lib64/egl/libGLES_mali.so 08-06 21:32:42.990 8882-9058 / com.example.android.hangang关于D / OpenGLRenderer:HWUI GL 管道08-06 21:32:43.001 8882-8882 / com.example.android.hangangabout D / ViewRootImpl @ c5bf86a [MainActivity]:setView = DecorView @ 4d5415b [MainActivity] TM = true MM = false 08-06 21:32:43.011 8882-8882 / com.example.android.hangangabout D / ViewRootImpl @ c5bf86a [MainActivity]:dispatchAttachedToWindow 08-06 21:32:43.067 8882-8882 / com.example.android.hangangabout V / Surface: sf_framedrop debug:0x4f4c,游戏:false,日志记录:0 08-06 21:32:43.068 8882-8882 / com.example.android.hangangabout D / ViewRootImpl @ c5bf86a [MainActivity]:中继返回: 旧= [0,0] [0,0]新= [0,0] [1440,2960]结果= 0x7曲面= {有效= true 539904405504}更改为true 08-06 21:32:43.091 关于I / OpenGLRenderer的8882-9058 / com.example.android.hangang: 初始化的EGL版本1.4 08-06 21:32:43.091 8882-9058 / com.example.android.hangang关于D / OpenGLRenderer:交换 行为2 08-06 21:32:43.100 8882-9058 / com.example.android.hangangabout D / libGLESv1:STS_GLApi: 套餐中不允许使用DTS,ODTC: com.example.android.hangangabout 08-06 21:32:43.101 8882-9058 / com.example.android.hangang关于D / mali_winsys:EGLint new_window_surface(egl_winsys_display *,void *,EGLSurface, EGLConfig,egl_winsys_surface **,egl_color_buffer_format *, EGLBoolean)返回0x3000,[1440x2960]-格式:1 08-06 21:32:43.101 8882-9058 / com.example.android.hangang关于D / OpenGLRenderer: eglCreateWindowSurface = 0x7dbb41daf0 08-06 21:32:43.458 8882-8882 / com.example.android.hangangabout D / AbsListView:位于onLayout中 更改为08-06 21:32:43.497 8882-8882 / com.example.android.hangangabout D / AndroidRuntime:关闭VM 08-06 21:32:43.516 8882-8882 / com.example.android.hangangabout E / AndroidRuntime:FATAL 例外:主要 流程:com.example.android.hangangabout,PID:8882 java.lang.RuntimeException:画布:试图绘制太大(132710400bytes)的位图。 在android.view.DisplayListCanvas.throwIfCannotDraw(DisplayListCanvas.java:229) 在android.view.RecordingCanvas.drawBitmap(RecordingCanvas.java:97) 在android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:529) 在android.view.View.getDrawableRenderNode(View.java:20627) 在android.view.View.drawBackground(View.java:20563) 在android.view.View.draw(View.java:20343) 在android.view.View.updateDisplayListIfDirty(View.java:19297) 在android.view.View.draw(View.java:20075) 在android.view.ViewGroup.drawChild(ViewGroup.java:4421) 在android.view.ViewGroup.dispatchDraw(ViewGroup.java:4207) 在android.view.View.updateDisplayListIfDirty(View.java:19288) 在android.view.View.draw(View.java:20075) 在android.view.ViewGroup.drawChild(ViewGroup.java:4421) 在android.view.ViewGroup.dispatchDraw(ViewGroup.java:4207) 在android.view.View.updateDisplayListIfDirty(View.java:19288) 在android.view.View.draw(View.java:20075) 在android.view.ViewGroup.drawChild(ViewGroup.java:4421) 在android.view.ViewGroup.dispatchDraw(ViewGroup.java:4207) 在android.view.View.updateDisplayListIfDirty(View.java:19288) 在android.view.View.draw(View.java:20075) 在android.view.ViewGroup.drawChild(ViewGroup.java:4421) 在android.view.ViewGroup.dispatchDraw(ViewGroup.java:4207) 在android.view.View.updateDisplayListIfDirty(View.java:19288) 在android.view.View.draw(View.java:20075) 在android.view.ViewGroup.drawChild(ViewGroup.java:4421) 在android.view.ViewGroup.dispatchDraw(ViewGroup.java:4207) 在android.view.View.draw(View.java:20355) 在com.android.internal.policy.DecorView.draw(DecorView.java:979) 在android.view.View.updateDisplayListIfDirty(View.java:19297) 在android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:686) 在android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:692) 在android.view.ThreadedRenderer.draw(ThreadedRenderer.java:800) 在android.view.ViewRootImpl.draw(ViewRootImpl.java:3451) 在android.view.ViewRootImpl.performDraw(ViewRootImpl.java:3238) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2773) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1742) 在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:7763) 在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:911) 在android.view.Choreographer.doCallbacks(Choreographer.java:723) 在android.view.Choreographer.doFrame(Choreographer.java:658) 在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:897) 在android.os.Handler.handleCallback(Handler.java:789) 在android.os.Handler.dispatchMessage(Handler.java:98) 在android.os.Looper.loop(Looper.java:164) 在android.app.ActivityThread.main(ActivityThread.java:6938) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:327) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
答案 0 :(得分:1)
我认为int getCount()应该返回3
您应在
上定义或创建阵列列表适配器public void onViewCreated(视图,@ Nullable Bundle saveInstanceState){
}
答案 1 :(得分:0)
在logcat正在报告时,您正在尝试绘制一种将大位图插入画布的方法:
FATAL EXCEPTION: main Process: com.example.android.hangangabout, PID: 8882 java.lang.RuntimeException: Canvas: trying to draw too large(132710400bytes) bitmap.
这超过132兆字节!在绘制之前,应先缩放图像。通常,不需要这么大的位图,因为您的设备没有足够的像素来显示它。
此外,拥有如此大的对象也很危险,因为您应用的RAM限制受设备限制(该限制取决于设备)。通常,您的应用程序只能使用32mb的内存。