在这里获取nullpointerexception以获取我不了解的内容

时间:2015-01-17 19:16:28

标签: java android list nullpointerexception fragment

编辑:我把所有内容都移到了OnCreateView上,但是现在我得到的错误是“我的listView是非静态字段不能引用静态(getApplication()相同”)并且“ArrayAdapter无法解析为构造函数“

所以我想用片段在我的活动中创建一个listView,我从教程中实现了以下代码。它没有告诉我错误在哪里,我不确切地知道哪个对象或东西是null。

package com.example.aland.intrigueme;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;


public class listActivity extends ActionBarActivity {


    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }

        //haal listView van XML
        listView = (ListView) findViewById(R.id.list);

        //waarden in Array definiëren om lijst te tonen

        String[] values = new String[] {"Park","Café","Nachtclub","Straat","School/Opleiding"};

        // Definieert nieuwe Adapter
        // Eerste paramater = context
        // Tweede parameter = layout voor rijen
        // Derde parameter = ID voor de textView
        // Vierde = data array

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

        //Adapter instellen aan lijst
        listView.setAdapter(adapter);

        //onClick event voor lijst
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                //listview clicked item index
                int itemPosition = position;

                //listview clicked item value
                String itemValue = (String) listView.getItemAtPosition(position);

                //show alert
                Toast.makeText(getApplicationContext(),
                        "Position: "+itemPosition+" Listitem: " +itemValue, Toast.LENGTH_LONG).show();


            }
        });


    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_list, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_list, container, false);
            return rootView;
        }
    }
}

这是我的XML文件

<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.aland.intrigueme.listActivity$PlaceholderFragment">

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


</LinearLayout>

这是我得到的错误

01-17 19:02:43.080    2830-2830/com.example.aland.intrigueme E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.aland.intrigueme, PID: 2830
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aland.intrigueme/com.example.aland.intrigueme.listActivity}: 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:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     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.aland.intrigueme.listActivity.onCreate(listActivity.java:49)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
01-17 19:03:50.460    2830-2830/com.example.aland.intrigueme I/Process﹕ Sending signal. PID: 2830 SIG: 9
01-17 19:04:16.630    2870-2870/com.example.aland.intrigueme D/AndroidRuntime﹕ Shutting down VM
01-17 19:04:16.630    2870-2870/com.example.aland.intrigueme E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.aland.intrigueme, PID: 2870
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aland.intrigueme/com.example.aland.intrigueme.listActivity}: 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:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     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.aland.intrigueme.listActivity.onCreate(listActivity.java:49)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
01-17 19:04:19.400    2870-2870/com.example.aland.intrigueme I/Process﹕ Sending signal. PID: 2870 SIG: 9

1 个答案:

答案 0 :(得分:1)

好吧,空指针表示您正在尝试访问尚未初始化的对象(可能)。我遇到了同样的问题,我的建议是在代码的每一行基本上使用Log.i(String a,String b)方法,看看你的对象何时被初始化以及何时被查询。

字符串a是日志文件中的“标记”,保持相同,以便更轻松地找到您的面包屑。 字符串b应该用于描述特定代码行的内容。

祝你好运。