2016-06-17 18 views
0

Dies treibt mich gerade die Wand hoch. Ich habe zwei Fragmente mit ListViews, die beide verwenden. Einer von ihnen funktioniert gut, aber der andere nicht.ListView wird nicht initialisiert

Hier ist der Arbeits Fragment:

//IRRELEVANT CODE ABOVE 
@BindView(R.id.exerciseListView1) 
ListView exerciseListView; 

/** 
* Mandatory empty constructor for the fragment manager to instantiate the 
* fragment (e.g. upon screen orientation changes). 
*/ 
public ExerciseFragment() { 
} 

@SuppressWarnings("unused") 
public static ExerciseFragment newInstance(int columnCount) { 
    ExerciseFragment fragment = new ExerciseFragment(); 
    Bundle args = new Bundle(); 
    args.putInt(ARG_COLUMN_COUNT, columnCount); 
    fragment.setArguments(args); 
    return fragment; 
} 

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

    if (getArguments() != null) { 
     mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT); 
    } 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_exercise_list, container, false); 
    unbinder = ButterKnife.bind(this, view); 

    ListItem[] exercises = { 
      new ListItem(" 1. Learning Names & Faces", "This exercise will teach you how to learn people's names."), 
      new ListItem(" 2. Paying Attention", "This exercise will test how well you pay attention."), 
      new ListItem(" 3. Memory Recall", "This exercise will help you improve your recall skills.") 
    }; 

    mAdapter = new ArrayAdapter<ListItem>(view.getContext(), R.layout.mylist, exercises); 
    exerciseListView.setAdapter(mAdapter); 
    exerciseListView.setOnItemClickListener(new ListView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, 
           long id) { 
      String item = ((TextView) view).getText().toString(); 
      Log.d("ExercisesActivity", item + " #" + Integer.toString(position) + " selected"); 
      //Switch statements won't take longs, thankfully there's the position variable 
      switch (position) { 
       case 0: 
        startActivity(new Intent(getContext(), LearningNamesActivity.class)); 
        break; 
       case 1: 
        startActivity(new Intent(getContext(), PayingAttentionActivity.class)); 
        break; 
       case 2: 
        startActivity(new Intent(getContext(), MemoryRecallActivity.class)); 
        break; 
      } 
     } 
    }); 

    return view; 
} 

Und hier ist das eine, die nicht funktionieren:

//IRRELEVANT CODE ABOVE 
@BindView(R.id.informationListView) 
ListView informationList; 
/** 
* Mandatory empty constructor for the fragment manager to instantiate the 
* fragment (e.g. upon screen orientation changes). 
*/ 
public InformationFragment() { 
} 

// TODO: Customize parameter initialization 
@SuppressWarnings("unused") 
public static InformationFragment newInstance(int columnCount) { 
    InformationFragment fragment = new InformationFragment(); 
    Bundle args = new Bundle(); 
    args.putInt(ARG_COLUMN_COUNT, columnCount); 
    fragment.setArguments(args); 
    return fragment; 
} 

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

    if (getArguments() != null) { 
     mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT); 
    } 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_information_list, container, false); 
    unbinder = ButterKnife.bind(getContext(), view); 

    ListItem[] exercises = { 
      new ListItem("1. Tell me about memory", ""), 
      new ListItem("2. What can go wrong with my memory", ""), 
      new ListItem("3. Helpful tips to improve my memory", "") 
    }; 
    mAdapter = new ArrayAdapter<>(view.getContext(), R.layout.mylist, exercises); 
informationList.setAdapter(mAdapter); 
informationList.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      switch (position) { 
       case 0: 
        startActivity(new Intent(getContext(), LearnAboutMemoryActivity.class)); 
        break; 
       case 1: 
        startActivity(new Intent(getContext(), WhatCanGoWrongActivity.class)); 
        break; 
       case 2: 
        //TODO: Use HelpfulTipsActivity as an "all-purpose" activity for holding tip Fragments 
        startActivity(new Intent(getContext(), HelpfulTipsActivity.class)); 
        break; 
      } 
     } 
}); 

return view; 

Hier ist das Fehlerprotokoll:

06-17 12:20:15.674 13182-13182/com.tommy.mastersproject E/AndroidRuntime: FATAL EXCEPTION: main                   Process: com.tommy.mastersproject, PID: 13182 
Theme: themes:{com.android.settings=overlay:com.cyngn.hexo, default=overlay:system, iconPack:system, fontPkg:system, com.android.systemui=overlay:system, com.android.systemui.navbar=overlay:system} 
                      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tommy.mastersproject/com.tommy.mastersproject.activities.InformationActivity}: 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:2450) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5461) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
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.tommy.mastersproject.fragments.InformationFragment.onCreateView(InformationFragment.java:90) 
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252) 
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742) 
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617) 
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:339) 
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:601) 
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1238) 
at android.app.Activity.performStart(Activity.java:6268) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)  
at android.app.ActivityThread.-wrap11(ActivityThread.java)  
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)  
at android.os.Handler.dispatchMessage(Handler.java:102)  
at android.os.Looper.loop(Looper.java:148)  
at android.app.ActivityThread.main(ActivityThread.java:5461)  
at java.lang.reflect.Method.invoke(Native Method)  
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  

Hier ist die Fragmente 'jeweilige Layout-Dateien: Arbeiten richtig

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
    <ListView 
     android:id="@+id/exerciseListView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
</RelativeLayout> 

nicht richtig

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
    <ListView 
     android:id="@+id/informationListView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
</RelativeLayout> 

arbeiten Gab es etwas in der Arbeits Fragment, das ich vergessen in den einen setzen, die nicht funktioniert?

+0

Können Sie versuchen, indem Sie Match_parent Höhe und Breite von ListView geben. –

+0

Sie verwendeten falsche 'ButterKnife.bind' im zweiten Fragment – Selvin

+0

sehen Sie meine Antwort @ CiaranC94 –

Antwort

1

Ich denke, das Problem ist in InformationFragment innen onCreateView():

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_information_list, container, false); 
    unbinder = ButterKnife.bind(this, view); 

    ListItem[] exercises = { 
     new ListItem("1. Tell me about memory", ""), 
     new ListItem("2. What can go wrong with my memory", ""), 
     new ListItem("3. Helpful tips to improve my memory", "") 
    }; 
    // rest of your code 

Problem:

Während Bindung Ansicht Sie getContext() .So Ihre informationList Listview verwendet wird, bleibt Null und Sie waren bekommen NullPointerException Entfernen getContext() instea d add this.

+0

Ich denke, ich hatte Recht, dass es eine dumme Unterlassung ist, die Probleme für mich verursacht. Vielen Dank! – CiaranC94

Verwandte Themen