为什么我的代码提供了NULLPOINTEREXCEPTION?

时间:2017-05-28 14:50:08

标签: java android xml nullpointerexception

我是android的新手,所以我无法弄清楚为什么这段代码会给出NullPointerException。 Plz帮助

应用程序不断崩溃。

MainActivity类

package com.example.yash.listview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        ArrayList<wordClass> arrayList = new ArrayList<wordClass>();

        arrayList.add(new wordClass("Yash","Infosys"));
        arrayList.add(new wordClass("Shivam","HCL"));
        arrayList.add(new wordClass("Mohit","HCL"));
        arrayList.add(new wordClass("Deepak","HCL"));
        arrayList.add(new wordClass("Yash","HCL"));

        wordAdapter Adapt = new wordAdapter(this,arrayList);
        ListView listView = (ListView) findViewById(R.id.mera);

        listView.setAdapter(Adapt);



    }
}

wordClass类

package com.example.yash.listview;

/**
 * Created by Yash on 5/28/2017.
 */

public class wordClass {

    private String name;
    private String company;

    public wordClass(String name, String company) {
        this.name = name;
        this.company = company;
    }

    public String getName() {
        return name;
    }

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

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }
}

wordAdapter类

package com.example.yash.listview;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Yash on 5/28/2017.
 */

public class wordAdapter extends ArrayAdapter<wordClass> {

    private static final String LOG_TAG = wordAdapter.class.getSimpleName();

    public wordAdapter(Activity context, ArrayList<wordClass> arrayList) {
        super(context,0, arrayList);
    }

    @NonNull
    @Override
    public View getView(int position,View convertView,ViewGroup parent) {
        View v = convertView;

        if(v==null)
        {
            v = LayoutInflater.from(getContext()).inflate(R.layout.list_layout,parent,false);
        }

        wordClass i= getItem(position);



            TextView textView = (TextView) v.findViewById(R.id.textView);
            TextView textView1 = (TextView) v.findViewById(R.id.textView1);


                textView.setText(i.getName());



                textView1.setText(i.getCompany());



        return v;



    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:layout_margin="10dp"
    android:orientation="vertical"
    tools:context="com.example.yash.listview.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30dp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="15dp"
        />

</LinearLayout>

listview.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">

    <ListView
        android:id="@+id/mera"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

    </ListView>

</LinearLayout>

list_layout.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:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="25dp"/>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="15dp"/>

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

不看你的Logcat以及发生崩溃的地方,你似乎把Listview放在了错误的xml中,所以findviewbyid找不到它

你需要做类似的事情:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    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"
    android:layout_margin="10dp"
    android:orientation="vertical"
    tools:context="com.example.yash.listview.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30dp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="15dp"
        />
    <ListView
        android:id="@+id/mera"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

    </ListView>
</LinearLayout>

这应该是您的主要活动,这样它就会找到列表视图。