-1

美好的一天!不能將Arraylist <Object>的值傳遞給片段

我很難得到這個問題。我嘗試實現接口,parcelable對象和bundles,以便與片段進行通信。但是在我運行我的代碼之後,它每次都會在Fragment類中發生NullPointerException。這裏的錯誤:

07-31 18:09:42.052 4776-4776/bwrt.monsters.android.com.blesswhoreadthis E/AndroidRuntime: FATAL EXCEPTION: main 
                         java.lang.RuntimeException: Unable to start activity ComponentInfo{bwrt.monsters.android.com.blesswhoreadthis/bwrt.monsters.android.com.blesswhoreadthis.Activity.MainActivity}: java.lang.NullPointerException 
    at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2059) 
    at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2084) 
    at android.app.ActivityThread.access$600(ActivityThread.java:130) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:4745) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
    at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.NullPointerException 
    at wrt.monsters.android.com.blesswhoreadthis.Fragment.GenesisFragment.onCreate(GenesisFragment.java:58) 
                          at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:796) 
                          at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035) 
                          at android.app.BackStackRecord.run(BackStackRecord.java:635) 
                          at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397) 
                          at android.app.Activity.performStart(Activity.java:5017) 
                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032) 
                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)  
                          at android.app.ActivityThread.access$600(ActivityThread.java:130)  
                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)  
                          at android.os.Handler.dispatchMessage(Handler.java:99)  
                          at android.os.Looper.loop(Looper.java:137)  
                          at android.app.ActivityThread.main(ActivityThread.java:4745)  
                          at java.lang.reflect.Method.invokeNative(Native Method)  
                          at java.lang.reflect.Method.invoke(Method.java:511)  
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)  
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)  
                          at dalvik.system.NativeStart.main(Native Method)  

這裏是我的片段類:

public class GenesisFragment extends Fragment { 

Context mContext; 
ListView lv; 
ArrayList<Bible> verseList=null; 
VerseAdapter verseAdapter; 
displayDataInterface listeners; 

public interface displayDataInterface { 
    Bundle displayData(); 

} 


@Override 
public void onAttach(Context context) { 
    super.onAttach(context); 
    if (context instanceof displayDataInterface) { 
     this.listeners= (displayDataInterface) context; 
    }else { 
     throw new ClassCastException(context.toString()+" must implement the NEEDED INTERFACE!"); 
    } 
} 

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

    Bundle bundle=listeners.displayData(); 

    if (bundle.isEmpty()) { 
     Log.e("BUNDLE","EMPTY"); 
    }else { 
     verseList= bundle.getParcelableArrayList("verseValues"); 
    } 

    verseAdapter= new VerseAdapter(mContext,verseList); 

} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View v= inflater.inflate(R.layout.fragment_genesis, container, false); 

    return v; 



} 

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 

    lv=(ListView)getActivity().findViewById(R.id.listview_genesis); 

    lv.setAdapter(verseAdapter); 
    verseAdapter.notifyDataSetChanged(); 


} 

}

我實現捆綁接口的方法內(_bibleValues-一個ArrayList)

我的MainActivity類別部分:

/** Interface Generated Methods*/ 
@Override 
public Bundle displayData() { 

    GenesisFragment gFragment= new GenesisFragment(); 
    Bundle args= new Bundle(); 
    args.putParcelableArrayList("verseValues",_bibleValues); //Make the Object class Parcelable 1st 
    gFragment.setArguments(args); 

    return args; 
} 

我的物品等級:

public class Bible implements Parcelable{ 
private int id; 
private int book; 
private int chapter; 
private int verseCount; 
private String verseContent; 
/** Constructors */ 
public Bible(int _id,int _book, int _chapter,int _verseCount, String _verseContent) { 
    this.id=_id; 
    this.book=_book; 
    this.chapter=_chapter; 
    this.verseCount=_verseCount; 
    this.verseContent=_verseContent; 
} 


protected Bible(Parcel in) { 
    id = in.readInt(); 
    book = in.readInt(); 
    chapter = in.readInt(); 
    verseCount = in.readInt(); 
    verseContent = in.readString(); 
} 

public static final Creator<Bible> CREATOR = new Creator<Bible>() { 
    @Override 
    public Bible createFromParcel(Parcel in) { 
     return new Bible(in); 
    } 

    @Override 
    public Bible[] newArray(int size) { 
     return new Bible[size]; 
    } 
}; 


/** Getters and Setters */ 
public int getId() { 
    return id; 
} 

public void setId(int id) { 
    this.id = id; 
} 

public int getBook() { 
    return book; 
} 

public void setBook(int book) { 
    this.book = book; 
} 

public int getChapter() { 
    return chapter; 
} 

public void setChapter(int chapter) { 
    this.chapter = chapter; 
} 

public int getVerseCount() { 
    return verseCount; 
} 

public void setVerseCount(int verseCount) { 
    this.verseCount = verseCount; 
} 

public String getVerseContent() { 
    return verseContent; 
} 

public void setVerseContent(String verseContent) { 
    this.verseContent = verseContent; 
} 



@Override 
public int describeContents() { 
    return 0; 
} 

@Override 
public void writeToParcel(Parcel dest, int flags) { 
    dest.writeInt(id); 
    dest.writeInt(book); 
    dest.writeInt(chapter); 
    dest.writeInt(verseCount); 
    dest.writeString(verseContent); 
} 

}

每次我運行它始終指向的onCreate錯誤的片段CLAS程序。任何人都可以向我解釋這個錯誤的根源,並建議我的想法,以防止這種情況發生?

在此先感謝!

+0

'_bibleValues'的類型是什麼? – skywall

+0

@ skywall-一個對象先生。因爲我有一個可以分類的對象,只是爲了實現Bundle方法。 – PhilipJ

+0

我的對象由4個整數和1個字符串組成。 – PhilipJ

回答

0

你的錯誤是在這裏

lv=(ListView)getActivity().findViewById(R.id.listview_genesis); 

其中,findViewById又回到你空,因爲我承擔活動XML佈局不包含ListView的,而是分片XML佈局一樣。


你可以做最裏面onCreateView所有這些操作,

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View v= inflater.inflate(R.layout.fragment_genesis, container, false); 
    lv = v.findViewById(R.id.listview_genesis); 

    if (listeners != null) { 
     Bundle bundle=listeners.displayData(); 

     if (bundle.isEmpty()) { 
      Log.e("BUNDLE","EMPTY"); 
     } else { 
      verseList= bundle.getParcelableArrayList("verseValues"); 
      verseAdapter= new VerseAdapter(getActivity(), verseList); 
      lv.setAdapter(verseAdapter); 
     } 
    } 

    return v; 

} 

實際上,displayData()既是該方法的可憐的名字,而且大多是錯誤的方式做你想做的因爲你做了一個new GenesisFragment,設置參數,然後不做任何事情。

它應該返回Fragment,而不是Bundle,它不顯示數據,它只是返回一個值。

看起來像你試圖實現類似instantiating a Fragment的方法,其中newInstance方法不在Activity類中,而是在Fragment類中,並且將該實例從Activity中加載到FragmentManager中。

+0

感謝您的幫助。我可以知道如何附加上下文的代碼嗎?對不起,我的困惑先生。 – PhilipJ

+0

你不應該在Fragment類中需要'mContext',當你需要上下文時,你可以在那些實例中訪問'getActivity()'方法。 –

+0

謝謝,我更改onAttach()中的代碼並且它工作正常。但我現在的問題是我的arraylist裏面的片段是平等的,但我調用Bundle方法來檢索接口方法中的參數。 – PhilipJ