Android 引起:java.lang.NullPointerException: Attempt to invoke virtual method 'void ... on a null object reference

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26326629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:15:48  来源:igfitidea点击:

caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void ... on a null object reference

android

提问by Red_X

I browse on some question here in stack overflow but it didn't solve my problem.

我在堆栈溢出中浏览了一些问题,但它没有解决我的问题。

here's my code.

这是我的代码。

   public class MenuListActivity extends ListActivity {
String MenuNames[] = {"Consultation", "Medicine", "Diseases", "Doctor"};
protected void onCreateView(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu_list);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, MenuNames);
    final ListView lv = (ListView) findViewById(R.id.listView1);

    lv.setAdapter(adapter);

    lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {

            if(position == 1) {
                //code specific to first list item    
                Intent myIntent = new Intent(v.getContext(), ConsultationActivity.class);
                startActivity(myIntent);
            }


        }

    }
     );

}

Logcat.

日志猫。

    10-12 10:53:37.572: E/AndroidRuntime(10605): FATAL EXCEPTION: main
    10-12 10:53:37.572: E/AndroidRuntime(10605): Process: com.capstone.medicinehealthguide, PID:     10605
    10-12 10:53:37.572: E/AndroidRuntime(10605): java.lang.RuntimeException: Unable to start activity      ComponentInfo{com.capstone.medicinehealthguide/com.capstone.medicinehealthguide.MenuListActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2317)
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at android.app.ActivityThread.access0(ActivityThread.java:143)
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at android.os.Handler.dispatchMessage(Handler.java:102)
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at android.os.Looper.loop(Looper.java:135)
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at android.app.ActivityThread.main(ActivityThread.java:5070)
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at java.lang.reflect.Method.invoke(Native Method)
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at java.lang.reflect.Method.invoke(Method.java:372)
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
    10-12 10:53:37.572: E/AndroidRuntime(10605): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at com.capstone.medicinehealthguide.MenuListActivity.onCreate(MenuListActivity.java:24)
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at android.app.Activity.performCreate(Activity.java:5720)
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102)
    10-12 10:53:37.572: E/AndroidRuntime(10605):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2208)
    10-12 10:53:37.572: E/AndroidRuntime(10605):    ... 10 more

XML file

XML文件

    <RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.capstone.medicinehealthguide.MenuListActivity" >

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

</ListView>

I'm trying to make an app that has a listview and when you click on it, another activities will be open

我正在尝试制作一个具有列表视图的应用程序,当您单击它时,将打开另一个活动

回答by Matt Clark

You are actually using this wrong:

你实际上是在使用这个错误:

final ListView lv = (ListView) getListView().findViewById(R.id.listView1);

Here:

这里:

final ListView lv = (ListView) findViewById(R.id.listView1);

The issue is that in yours, you are getting the list view, and then trying to get your list view from a list view.

问题是在您的情况下,您正在获取列表视图,然后尝试从列表视图中获取列表视图。

Try the option above to just get the list view from your content view.

尝试上面的选项以从您的内容视图中获取列表视图。

回答by Panther

Before you invoke findByViewIdthe view must be inflated. What you have to do is.. move this code from onCreatemethod to 'onCreateView' method. IN onCreateViewmethod place it below setContentView()line..

在调用findByViewId视图之前必须膨胀。您需要做的是.. 将此代码从onCreate方法移动到“onCreateView”方法。INonCreateView方法将它放setContentView()在线下..

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, MenuNames);

final ListView lv = (ListView) getListView().findViewById(R.id.listView1);

lv.setAdapter(adapter);

lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v,
            int position, long id) {

        if(position == 1) {
            //code specific to first list item    
            Intent myIntent = new Intent(v.getContext(), ConsultationActivity.class);
            startActivity(myIntent);
        }
}