我試圖通過將對象從一個片段傳遞到另一個片段來在兩個片段之間進行通信。在GradeListFragment中,我有一個listView,而在GradeInfoFragment中,顯示的細節取決於在listView上進行的選擇。我向日誌顯示消息,以確保可以訪問所需的方法並確保可以檢索碎片之間的信息,但我仍可以獲取NullPointerException,因爲某些原因,當我嘗試調用將處理碎片的方法時在我GradeInfoFragment數據片段通信,由於某種原因無法正常工作
GradeListActivity.java
package bcs421.jorgeramirez.hwk.gradeapp.presentation;
import bcs421.jorgeramirez.hwk.gradeapp.businesslogic.GradeData;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Surface;
import android.app.FragmentTransaction;
public class GradeListActivity extends FragmentActivity implements SendGrades {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gradelist);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.frag_container1, new GradeListFragment());
int rotation = getWindowManager().getDefaultDisplay().getRotation();
if((rotation == Surface.ROTATION_90) || (rotation == Surface.ROTATION_270))
{
ft.add(R.id.frag_container2, new GradeInfoFragment());
}
ft.commit();
}
@Override
public void sendGradeInfo(GradeData thisGrade) {
Log.d("Hello!", "In sendGradeInfo function!");
Log.d("HEy!", "This category is: " + thisGrade.getCategory());
GradeInfoFragment f2 = (GradeInfoFragment) getFragmentManager().findFragmentById(R.id.frag_container2);
f2.getData(thisGrade);
}
}
GradeListFragment.java
package bcs421.jorgeramirez.hwk.gradeapp.presentation;
import java.io.Serializable;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import bcs421.jorgeramirez.hwk.gradeapp.businesslogic.GradeCollection;
import bcs421.jorgeramirez.hwk.gradeapp.businesslogic.GradeData;
public class GradeListFragment extends Fragment implements Serializable, SendGrades {
SendGrades SG;
public GradeCollection collectedGrades;
@Override
public View onCreateView( LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_gradelist, container, false);
ListView lv = (ListView) v.findViewById(R.id.listViewGrades);
ArrayList<String> namesArrayList;
ArrayAdapter<String> namesArrayAdapter;
Activity a = getActivity();
Intent i = a.getIntent();
namesArrayList = i.getExtras().getStringArrayList("grade_list");
collectedGrades = (GradeCollection) i.getSerializableExtra("grade_collection");
namesArrayAdapter = new ArrayAdapter<String>(a, R.layout.item_gradelist, namesArrayList);
lv.setAdapter(namesArrayAdapter);
lv.setClickable(false);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
GradeData thisGrade = collectedGrades.GetItem(position);
Log.d("Hell!", "This category is: " + thisGrade.getCategory());
SG.sendGradeInfo(thisGrade);
}
});
return v;
}
@Override
public void onAttach (Activity activity)
{
super.onAttach(activity);
try
{
SG = (SendGrades)activity;
}
catch (ClassCastException e)
{
throw new ClassCastException("You need to implement the sendGradeInfo method");
}
}
@Override
public void sendGradeInfo(GradeData thisGrade) {
GradeInfoFragment f2 = (GradeInfoFragment)getFragmentManager().findFragmentById(R.id.frag_container2);
f2.getData(thisGrade);
}
}
GradeInfoFragment.java
package bcs421.jorgeramirez.hwk.gradeapp.presentation;
import java.util.ArrayList;
import bcs421.jorgeramirez.hwk.gradeapp.businesslogic.GradeData;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class GradeInfoFragment extends Fragment {
EditText categoryText, numberText, gradeText, descText;
@Override
public View onCreateView( LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_gradeinfo, container, false);
categoryText = (EditText)v.findViewById(R.id.editCat);
numberText = (EditText)v.findViewById(R.id.editNumber);
gradeText = (EditText)v.findViewById(R.id.editGrade);
descText = (EditText)v.findViewById(R.id.editDesc);
return v;
}
public void getData(GradeData receivedGrade)
{
double gd = receivedGrade.getGrade();
String gValue = Double.toString(gd);
Log.d("Hello!", "Filling in grade information!");
categoryText.setText(receivedGrade.getCategory());
numberText.setText(receivedGrade.getNumber());
gradeText.setText(gValue);
descText.setText(receivedGrade.getDescription());
}
}
logcat的
04-06 13:26:28.161: D/Hell!(2229): This category is: Lab
04-06 13:26:28.161: D/Hello!(2229): In sendGradeInfo function!
04-06 13:26:28.161: D/HEy!(2229): This category is: Lab
04-06 13:26:28.161: D/AndroidRuntime(2229): Shutting down VM
04-06 13:26:28.161: W/dalvikvm(2229): threadid=1: thread exiting with uncaught exception (group=0xb1d44b20)
04-06 13:26:28.161: E/AndroidRuntime(2229): FATAL EXCEPTION: main
04-06 13:26:28.161: E/AndroidRuntime(2229): Process: bcs421.jorgeramirez.hwk.gradeapp.presentation, PID: 2229
04-06 13:26:28.161: E/AndroidRuntime(2229): java.lang.NullPointerException
04-06 13:26:28.161: E/AndroidRuntime(2229): at bcs421.jorgeramirez.hwk.gradeapp.presentation.GradeListActivity.sendGradeInfo(GradeListActivity.java:45)
04-06 13:26:28.161: E/AndroidRuntime(2229): at bcs421.jorgeramirez.hwk.gradeapp.presentation.GradeListFragment$1.onItemClick(GradeListFragment.java:55)
04-06 13:26:28.161: E/AndroidRuntime(2229): at android.widget.AdapterView.performItemClick(AdapterView.java:299)
04-06 13:26:28.161: E/AndroidRuntime(2229): at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
04-06 13:26:28.161: E/AndroidRuntime(2229): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2904)
04-06 13:26:28.161: E/AndroidRuntime(2229): at android.widget.AbsListView$3.run(AbsListView.java:3638)
04-06 13:26:28.161: E/AndroidRuntime(2229): at android.os.Handler.handleCallback(Handler.java:733)
04-06 13:26:28.161: E/AndroidRuntime(2229): at android.os.Handler.dispatchMessage(Handler.java:95)
04-06 13:26:28.161: E/AndroidRuntime(2229): at android.os.Looper.loop(Looper.java:136)
04-06 13:26:28.161: E/AndroidRuntime(2229): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-06 13:26:28.161: E/AndroidRuntime(2229): at java.lang.reflect.Method.invokeNative(Native Method)
04-06 13:26:28.161: E/AndroidRuntime(2229): at java.lang.reflect.Method.invoke(Method.java:515)
04-06 13:26:28.161: E/AndroidRuntime(2229): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-06 13:26:28.161: E/AndroidRuntime(2229): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-06 13:26:28.161: E/AndroidRuntime(2229): at dalvik.system.NativeStart.main(Native Method)
請您在sendGradeInfo方法F2實例......這不應該是空 –
嘗試調試你的應用程序。 f2是空參數嗎? – natario
嘿傢伙!我運行了一條if語句,如果f2爲null,並且f2確實爲null,則會向Logcat發送消息。我不知道爲什麼! –