在设置适配器上接收“空对象引用”

时间:2019-09-08 15:10:52

标签: java android android-studio android-emulator

这是代码,结尾是错误。

MainActivity

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate: Started");
        ListView theList = (ListView) findViewById(R.id.listViewTest);

        Person Mike = new Person("Mike", "23");
        Person Ivan = new Person("Ivan", "54");
        Person Petkan = new Person("Petkan", "26");
        Person Viki = new Person("Viki", "26");
        Person Miki = new Person("Miki", "26");
        Person Ani = new Person("Ani", "26");


        ArrayList<Person> peopleList = new ArrayList<>();

        peopleList.add(Mike);
        peopleList.add(Ivan);
        peopleList.add(Petkan);
        peopleList.add(Viki);
        peopleList.add(Miki);
        peopleList.add(Ani);
        peopleList.add(Ani);
        peopleList.add(Ani);
        peopleList.add(Ani);
        peopleList.add(Ani);
        peopleList.add(Ani);
        peopleList.add(Mike);
        peopleList.add(Ivan);
        peopleList.add(Petkan);
        peopleList.add(Viki);
        peopleList.add(Miki);
        peopleList.add(Ani);
        peopleList.add(Ani);
        peopleList.add(Ani);
        peopleList.add(Ani);
        peopleList.add(Ani);
        peopleList.add(Ani);
        peopleList.add(Mike);
        peopleList.add(Ivan);
        peopleList.add(Petkan);
        peopleList.add(Viki);
        peopleList.add(Miki);
        peopleList.add(Ani);
        peopleList.add(Ani);
        peopleList.add(Ani);
        peopleList.add(Ani);
        peopleList.add(Ani);
        peopleList.add(Ani);

        PersonListAdapter adapter = new PersonListAdapter(this, R.layout.new_list_layout, peopleList);
        theList.setAdapter(adapter);

    }
}

PersonListAdapter

public class PersonListAdapter extends ArrayAdapter<Person> {

    private static final String TAG = "PersonListAdapter";

    private Context mContext;
    private int mResource;

    public PersonListAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Person> objects) {
        super(context, resource, objects);
        this.mContext = mContext;
        this.mResource = resource;

    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        String name = getItem(position).getName();
        String points = getItem(position).getPoints();

        Person person = new Person(name, points);

        LayoutInflater inflater = LayoutInflater.from(mContext);
        convertView = inflater.inflate(mResource, parent, false);

        TextView tvName = (TextView) convertView.findViewById(R.id.textView1);
        TextView tvPoints = (TextView) convertView.findViewById(R.id.textView2);

        tvName.setText(name);
        tvPoints.setText(points);

        return convertView;
    }
}

public class Person {

    private String name;
    private String points;

    public Person(String name, String points) {
        this.name = name;
        this.points = points;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPoints() {
        return points;
    }

    public void setPoints(String points) {
        this.points = points;
    }
}

activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:id="@+id/listViewTest"
       />
</RelativeLayout>

new_list_layout

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



    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="TextView1"
        android:gravity="center"
        android:id="@+id/textView1"
        android:layout_weight="30"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="TextView2"
        android:gravity="center"
        android:id="@+id/textView2"
        android:layout_weight="70"
        />

</LinearLayout>

这是错误

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.test, PID: 17498
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
        at com.example.test.MainActivity.onCreate(MainActivity.java:68)
        at android.app.Activity.performCreate(Activity.java:7802)
        at android.app.Activity.performCreate(Activity.java:7791)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

我正在处理一项任务,我认为可以完成所有任务,但是无法启动。我收到消息:

  

仿真器:尝试擦除句柄为0的不存在的颜色缓冲区。

0 个答案:

没有答案