二进制XML文件行#0:错误膨胀类android.widget.imageview

时间:2018-01-15 12:04:40

标签: android android-layout

在没有找到解决方案的情况下搜索了3个小时的解决方案后,我决定找问问题。所以我使用BaseAdapter用图像填充gridview,错误就在我在BaseAdapter的getView()方法中扩展布局的行上

adapter.java

public class adapter extends BaseAdapter {
Context context;
int images[];
LayoutInflater inflater;
public adapter(final Context applicationcontext, final int[] images)
{
    this.context=applicationcontext;
    this.images=images;
    inflater=(LayoutInflater.from(applicationcontext));
}

@Override
public int getCount() {
    return images.length;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View view, ViewGroup parent) {
    try{
        view =inflater.inflate(R.layout.images_list, null);// error line
        ImageView imageView=(ImageView) view.findViewById(R.id.icon);
        imageView.setImageResource(images[position]);

    }
    catch (Exception ex)
    {
        ex.printStackTrace();
    }
    return view;
}

}

这是我的xml

images_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="1dp"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/icon"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:scaleType="fitXY"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/ic_launcher_foreground" />

</LinearLayout>

堆栈跟踪:

   01-15 18:38:37.729 19417-19417/com.example.zain.testingwallpaper E/AndroidRuntime: FATAL EXCEPTION: main
   java.lang.NullPointerException
       at android.widget.AbsListView.obtainView(AbsListView.java:2474)
       at android.widget.GridView.onMeasure(GridView.java:1030)
       at android.view.View.measure(View.java:15524)
       at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
       at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
       at android.view.View.measure(View.java:15524)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
       at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
       at android.view.View.measure(View.java:15524)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
       at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:400)
       at android.view.View.measure(View.java:15524)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
       at android.view.View.measure(View.java:15524)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
       at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
       at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
       at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
       at android.view.View.measure(View.java:15524)
       at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5109)
       at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
       at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2397)
       at android.view.View.measure(View.java:15524)
       at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1986)
       at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1227)
       at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1400)
       at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1120)
       at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4604)
       at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
       at android.view.Choreographer.doCallbacks(Choreographer.java:555)
       at android.view.Choreographer.doFrame(Choreographer.java:525)
       at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
       at android.os.Handler.handleCallback(Handler.java:615)
       at android.os.Handler.dispatchMessage(Handler.java:92)
       at android.os.Looper.loop(Looper.java:137)
       at android.app.ActivityThread.main(ActivityThread.java:4921)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:511)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
       at dalvik.system.NativeStart.main(Native Method)
   01-15 18:38:37.819 19417-19417/com.example.zain.testingwallpaper I/Process: Sending signal. PID: 19417 SIG: 9

1 个答案:

答案 0 :(得分:-2)

替换下面的行

view =inflater.inflate(R.layout.images_list, null);

view =inflater.inflate(R.layout.images_list, parent, false);