片段数组列表nullpointerexception

时间:2016-10-03 16:17:01

标签: android nullpointerexception fragment listadapter

我仍然是android的初学者,我使用listAdapter在UI中添加一个简单的数组列表,但是我的代码仍然存在此错误 你能救我吗?

异常堆栈跟踪:

android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) 
at android.app.ActivityThread.access$900(ActivityThread.java:177) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5942) 
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:1400) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference 
at test.ahlem.tn.sunshine.PlaceholderFragment.onCreateView(PlaceholderFragment.java:61) 
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290) 
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801) 
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677) 
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388) 
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:604) 
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178) 
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1234) 
at android.app.Activity.performStart(Activity.java:6329) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2656) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)  
at android.app.ActivityThread.access$900(ActivityThread.java:177) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
at android.os.Handler.dispatchMessage(Handler.java:102)  
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:5942) 
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:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) <p>

PlaceholderFragment.java代码:

public class PlaceholderFragment extends Fragment {

    public PlaceholderFragment () {
        // Required empty public constructor
    }

    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
    }

    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        String[] forcastArray = {
            "Today _ sunny _ 88/63",
            "Tomorrow _ Foggy _ 70/40",
            "wed _ cloudy _ 72/63",
            "thus _ Astroids _ 75/65",
            "Fri _ heavy Rain _ 65/56",
            "SAT _ Help trapped in weatheration _ 60/51",
            "sun _ sunny _ 80/68"
        };
        // Inflate the layout for this fragment
        View rootView= inflater.inflate(R.layout.fragment_placeholder, container, false);

        List<String> weekForcast = new ArrayList<String> (Arrays.asList (forcastArray));
        Log.v ("the weekForcast :", String.valueOf (weekForcast));
        ArrayAdapter<String> mForcastAdapter = new ArrayAdapter<String> (getContext(), R.layout.list_item_forcast, weekForcast);
        Log.v ("the mForcastAdpter content :", String.valueOf (mForcastAdapter));
        ListView listView = (ListView) rootView.findViewById (R.id.listview_forcast);
        listView.setAdapter (mForcastAdapter);

        return rootView;
    }
}

    

<android.support.v7.widget.ListViewCompat
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listview_forcast">
</android.support.v7.widget.ListViewCompat>

1 个答案:

答案 0 :(得分:0)

异常堆栈跟踪说:Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

这表示您的listview_forcast布局代码中没有fragment_placeholder.xml个ID。

您可能在那里有一个类型或使用了错误的布局。