Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的代码会给出NULLPOINTEREXCEPTION?_Java_Android_Xml_Nullpointerexception - Fatal编程技术网

Java 为什么我的代码会给出NULLPOINTEREXCEPTION?

Java 为什么我的代码会给出NULLPOINTEREXCEPTION?,java,android,xml,nullpointerexception,Java,Android,Xml,Nullpointerexception,我是android新手,所以我无法理解为什么这段代码会给出NullPointerException。Plz帮助 应用程序不断崩溃 main活动类 package com.example.yash.listview; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView

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

应用程序不断崩溃

main活动类

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);



    }
}
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;
    }
}
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;



    }
}
wordAdapter类

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);



    }
}
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;
    }
}
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;



    }
}
package com.example.yash.listview;
导入android.app.Activity;
导入android.content.Context;
导入android.support.annotation.LayoutRes;
导入android.support.annotation.NonNull;
导入android.support.annotation.Nullable;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.TextView;
导入java.util.ArrayList;
导入java.util.List;
/**
*由Yash于2017年5月28日创建。
*/
公共类wordAdapter扩展了ArrayAdapter{
私有静态最终字符串LOG_TAG=wordAdapter.class.getSimpleName();
公共字适配器(活动上下文,ArrayList ArrayList){
super(context,0,arrayList);
}
@非空
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
如果(v==null)
{
v=LayoutInflater.from(getContext()).flate(R.layout.list\u布局,父级,false);
}
wordClass i=getItem(位置);
TextView TextView=(TextView)v.findViewById(R.id.TextView);
TextView textView1=(TextView)v.findViewById(R.id.textView1);
setText(i.getName());
textView1.setText(i.getCompany());
返回v;
}
}
活动\u 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>
<?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>
<?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>

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

list_layout.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>
<?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>
<?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>

如果不查看日志猫和崩溃发生的位置,似乎您将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>


这应该是您的主要活动,这样它将找到listview。

添加logcat以便我们可以看到崩溃发生的位置,包括代码抛出npe的那一行。。阅读目前,我们对您的问题的了解比您在activity_main.xml中所做的要少。您没有ListView,并且您正试图使用findViewById()方法在MainActivity中访问它,这就是为什么会出现异常。谢谢!!这很有效。。。看到这个错误后,我觉得自己很愚蠢:)