我是Android新手。我的应用程序在真实设备上运行时正在关闭,但它在我的模拟器上工作正常。这是我在调试器上获得的错误代码。我正在尝试在pageViewer的帮助下添加图像滑块。在添加滑块之前,应用程序工作正常。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.windows81.travelspoc, PID: 17409
android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class ImageView
at android.view.LayoutInflater.inflate(LayoutInflater.java:545)
at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
at android.view.LayoutInflater.inflate(LayoutInflater.java:380)
at com.example.windows81.travelspoc.ViewPagerAdapter.instantiateItem(ViewPagerAdapter.java:39)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1002)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1150)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1084)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1614)
at android.view.View.measure(View.java:19141)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:973)
at android.view.View.measure(View.java:19141)
at android.support.v4.widget.NestedScrollView.measureChildWithMargins(NestedScrollView.java:1446)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:223)
at android.support.v4.widget.NestedScrollView.onMeasure(NestedScrollView.java:512)
at android.view.View.measure(View.java:19141)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6245)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:223)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:19141)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6245)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:400)
at android.view.View.measure(View.java:19141)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6245)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:223)
at android.view.View.measure(View.java:19141)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6245)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1723)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:788)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:648)
at android.view.View.measure(View.java:19141)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6245)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:223)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2790)
at android.view.View.measure(View.java:19141)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2476)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1440)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1694)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1321)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6748)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:713)
at android.view.Choreographer.doFrame(Choreographer.java:648)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5790)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1015)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:876)
Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class ImageView
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:827)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:749)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:880)
at
android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:843)
at andro
I/Process: Sending signal. PID: 17409 SIG: 9
Disconnected from the target VM, address: 'localhost:8600', transport:
'socket'
HomeActivity.java
package com.example.windows81.travelspoc;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class HomeActivity extends AppCompatActivity {
ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
viewPager = (ViewPager) findViewById(R.id.viewPager);
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(this);
viewPager.setAdapter(viewPagerAdapter);
}
public void exploreActivity(View view){
Intent intent = new Intent(this,ExploreActivity.class);
startActivity(intent);
}
public void packagesActivity(View view){
Intent intent = new Intent(this,PackagesActivity.class);
startActivity(intent);
}
public void dealsActivity(View view){
Intent intent = new Intent(this,DealsActivity.class);
startActivity(intent);
}
}
ViewPagerAdapter.java
package com.example.windows81.travelspoc;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
/**
* Created by Windows 8.1 on 11/6/2017.
*/
public class ViewPagerAdapter extends PagerAdapter{
private Context context;
private LayoutInflater layoutInflater;
private Integer [] images = {R.drawable.slide1,R.drawable.slide2};
public ViewPagerAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return images.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.custom_layout,null);
ImageView imageView = (ImageView) view.findViewById(R.id.slider);
imageView.setImageResource(images[position]);
ViewPager vp = (ViewPager) container;
vp.addView(view, 0);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
ViewPager vp = (ViewPager) container;
View view = (View) object;
vp.removeView(view);
}
}
答案 0 :(得分:0)
堆栈跟踪可能会产生误导。您的drawable可能有问题。尝试不同的@drawable,看看它是否有效。或者其他一个可能是你的图像文件大小有点大
您的错误是:
android.view.InflateException:二进制XML文件行#0:二进制XML文件行#0:错误导致类ImageView
选中此项并按此搜索。
答案 1 :(得分:0)
我得到了答案。问题是,我的自定义布局的imageView源是mipmap,我已将其更改为drawable,现在正在运行。谢谢你的贡献。