【问题标题】:Android ListAdapter :NullPointerException exceptionAndroid ListAdapter :NullPointerException 异常
【发布时间】:2016-04-16 12:56:16
【问题描述】:

此代码在基本 Activity 中有效,但在选项卡式 Activity 中无效。 我想将 ListView 与自定义视图一起使用。 常用代码:

list_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView2" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textView3" />
</LinearLayout>

这是我的简单数据类,我想展示一下:

String nev;
String szakma;

public String getNev() {
    return nev;
}

public void setNev(String nev) {
    this.nev = nev;
}

public String getSzakma() {
    return szakma;
}

public void setSzakma(String szakma) {
    this.szakma = szakma;
}

public Adat(String nev, String szakma) {
    this.nev = nev;
    this.szakma = szakma;
}

还有我的自定义 ListAdapter:

    public class EgyeniAdapter extends BaseAdapter {

        Context context;
        ArrayList<Adat> adatok;

        public EgyeniAdapter(Context context, ArrayList<Adat> adatok) {
            this.context = context;
            this.adatok = adatok;
        }

        @Override
        public int getCount() {
            return adatok.size();
        }

        @Override
        public Object getItem(int position) {
            return adatok.get(position);
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_row,null); // itt rendelem hozza a saját viewet

            if (convertView != null)
            {
                TextView Cim = (TextView) convertView.findViewById(R.id.textView2);
                TextView Alcim = (TextView) convertView.findViewById(R.id.textView3);

                Adat adatom = adatok.get(position);
                Cim.setText(adatom.getNev());
                Alcim.setText(adatom.getSzakma());

            }
            return convertView;
        }
    }

And it is my MainActivity:

@Override
    protected void onCreate(Bundle savedInstanceState) {
    ...
    betolt();
}

private void betolt()
    {
        ArrayList<Adat> adataim = new ArrayList<>();
        adataim.add(new Adat("Title","Subtitle"));
        adataim.add(new Adat("Title2","Subtitle2"));
        final ListView myList=  (ListView) findViewById(R.id.listView);
        EgyeniAdapter egyeniAdapter = new EgyeniAdapter(this,adataim);

        myList.setAdapter(egyeniAdapter);

    }

这可行,但是当我想在选项卡式活动中的另一个应用程序中使用它时,它会抛出:

NullPointerException: 尝试调用虚方法 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' 空对象引用

有什么问题?

我的片段代码:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.mpet8.englishtest.Tesztek">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment"
        android:id="@+id/tesztek_label"
        />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tesztek_listView"
        android:layout_gravity="left|top" />

</FrameLayout>

我在 onStart 方法中调用我的方法:

@Override
    protected void onStart() {

        super.onStart();
        betolt();
    }

【问题讨论】:

标签: android exception listadapter


【解决方案1】:

betolt() 中,您正在寻找ID 为R.id.listView (final ListView myList= (ListView) findViewById(R.id.listView);) 的ListView,但在您的fragmentlayout 中,它的ID 为android:id="@+id/tesztek_listView"

【讨论】:

  • 对不起,我复制了正常的活动 betolt() 方法。我在选项卡式活动中使用了这个:final ListView myList= (ListView) findViewById(R.id.tesztek_listView);
  • 在我看来:EgyeniAdapter 类是可以的。问题是:myList 为空。 (我在选项卡视图中使用碎片。) private void betolt() { .. ListView myList= (ListView) findViewById(R.id.tesztek_listView);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-05
  • 1970-01-01
  • 2011-02-10
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 2013-11-28
相关资源
最近更新 更多