我有一个应用程序有1个主要活动,调用2个片段。 1个片段有一个列表视图,如果单击一个项目,它应该打开一个新片段,其中包含基于所单击项目索引的信息。当我尝试更改第二个片段调用中的文本时,我的应用程序崩溃了。
MainActivity:
public class MyActivity extends Activity implements Communicator {
FragmentManager manager;
FragmentTransaction transaction;
FriendsFragment friendsFrag;
FeedFragment feedFrag;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
friendsFrag = new FriendsFragment();
feedFrag = new FeedFragment();
manager = getFragmentManager();
transaction = manager.beginTransaction();
transaction.add(R.id.fragment_container, friendsFrag, "friendsFrag");
transaction.commit();
}
@Override
public void startFragment(int i) {
manager = getFragmentManager();
//FeedFragment frag = (FeedFragment) manager.findFragmentById(R.id.feedFrag);
transaction = manager.beginTransaction();
transaction.replace(R.id.fragment_container, feedFrag);
transaction.addToBackStack(null);
transaction.commit();
feedFrag.updateFeedDisplay(i);
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/fragment_container"
>
</LinearLayout>
第一个叫做的片段:
public class FriendsFragment extends ListFragment implements AdapterView.OnItemClickListener {
Communicator com;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.friendsfragment, container, false);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
com = (Communicator)activity;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//com = (Communicator)getActivity();
ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.test, android.R.layout.simple_list_item_1);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
com.startFragment(position);
// Toast.makeText(getActivity(), "ITem: " + position, Toast.LENGTH_LONG).show();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@android:id/list"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@android:id/empty"
android:layout_gravity="left|center_vertical"/>
</LinearLayout>
第二片段:
public class FeedFragment extends Fragment {
FeedFragmentData feed;
private static TextView fedTV;
//Communicator com;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// LayoutInflater lf = getActivity().getLayoutInflater();
//return inflater.inflate(R.layout.feedfragment, container, false);
View v = inflater.inflate(R.layout.feedfragment, container, false);
//fedTV = (TextView) v.findViewById(R.id.fedTV);
feed = new FeedFragmentData(getActivity());
//fedTV.setText(feed.getFeed(0));
return v;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
//com = (Communicator)activity;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// feed = new FeedFragmentData(getActivity());
fedTV = (TextView) getActivity().findViewById(R.id.fedTV);
}
public void updateFeedDisplay(int position) {
// Toast.makeText(this, "HIii", Toast.LENGTH_LONG).show();
// fedTV = (TextView) getActivity().findViewById(R.id.fedTV);
if (fedTV != null) {
fedTV.setText("Haa");
}
}
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="text"
android:id="@+id/fedTV"/>
</ScrollView>
当我删除if(fedTV!= null)和setText时,我产生了这个错误。如果我有if语句,那么textfield永远不会改变 Logcat错误:
java.lang.NullPointerException
at albert.kim.fragments.FeedFragment.updateFeedDisplay(FeedFragment.java:53)
at albert.kim.fragments.MyActivity.startFragment(MyActivity.java:41)
at albert.kim.fragments.FriendsFragment.onItemClick(FriendsFragment.java:47)
at android.widget.AdapterView.performItemClick(AdapterView.java:308)
at android.widget.AbsListView.performItemClick(AbsListView.java:1483)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3485)
at android.widget.AbsListView$3.run(AbsListView.java:4843)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
我在updateFeedDisplay()方法中实例化了fedTV,并将声明从private static更改为private。这次工作......