我想从一个活动向片段发送一个字符串。这是我在我的活动课中所做的。我从json对象获取字符串并将其发送到片段类。但我作为回应得到了无效。怎么解决这个问题?
public final static String EXTRA_MESSAGE = "LoginActivity.java";
JSONObject mainObject2 = new JSONObject(responseStr2);
String Name = mainObject2.getString("Name");
Intent i = new Intent(getApplicationContext(),HomeFragment.class);
Bundle b = new Bundle();
b.putString("Name", Name);
i.putExtras(b);
startActivity(i);
以下是我在片段类中所做的事情。我正在尝试接收字符串并在textview上显示它。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
String Name = getActivity().getIntent().getStringExtra(LoginActivity.EXTRA_MESSAGE);
name = (TextView) rootView.findViewById(R.id.name);
name.setText(Name);
更新1:
我的LogCat显示以下内容。
03-23 13:26:13.756: E/AndroidRuntime(2540): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
03-23 13:26:13.756: E/AndroidRuntime(2540): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
03-23 13:26:13.756: E/AndroidRuntime(2540): at android.app.ActivityThread.access$800(ActivityThread.java:144)
03-23 13:26:13.756: E/AndroidRuntime(2540): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
03-23 13:26:13.756: E/AndroidRuntime(2540): at android.os.Handler.dispatchMessage(Handler.java:102)
03-23 13:26:13.756: E/AndroidRuntime(2540): at android.os.Looper.loop(Looper.java:135)
03-23 13:26:13.756: E/AndroidRuntime(2540): at android.app.ActivityThread.main(ActivityThread.java:5221)
03-23 13:26:13.756: E/AndroidRuntime(2540): at java.lang.reflect.Method.invoke(Native Method)
03-23 13:26:13.756: E/AndroidRuntime(2540): at java.lang.reflect.Method.invoke(Method.java:372)
03-23 13:26:13.756: E/AndroidRuntime(2540): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
03-23 13:26:13.756: E/AndroidRuntime(2540): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-23 13:26:13.756: E/AndroidRuntime(2540): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
03-23 13:26:13.756: E/AndroidRuntime(2540): at com.webgentechnology.kidskula.HomeFragment.onCreateView(HomeFragment.java:81)
03-23 13:26:13.756: E/AndroidRuntime(2540): at android.app.Fragment.performCreateView(Fragment.java:2053)
03-23 13:26:13.756: E/AndroidRuntime(2540): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
03-23 13:26:13.756: E/AndroidRuntime(2540): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
03-23 13:26:13.756: E/AndroidRuntime(2540): at android.app.BackStackRecord.run(BackStackRecord.java:833)
03-23 13:26:13.756: E/AndroidRuntime(2540): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
03-23 13:26:13.756: E/AndroidRuntime(2540): at android.app.Activity.performStart(Activity.java:5948)
03-23 13:26:13.756: E/AndroidRuntime(2540): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
03-23 13:26:13.756: E/AndroidRuntime(2540): ... 10 more
03-23 13:26:19.086: I/Process(2540): Sending signal. PID: 2540 SIG: 9
03-23 13:26:25.097: I/art(2750): Background partial concurrent mark sweep GC freed 30(3KB) AllocSpace objects, 0(0B) LOS objects, 24% free, 3MB/4MB, paused 11.746ms total 39.506ms
03-23 13:26:26.204: I/art(2750): Background partial concurrent mark sweep GC freed 77(3KB) AllocSpace objects, 0(0B) LOS objects, 20% free, 7MB/9MB, paused 7.144ms total 58.002ms
03-23 13:26:26.726: D/gralloc_goldfish(2750): Emulator without GPU emulation detected.
更新2:
下面是我的XML文件。此布局中有两个选项卡。以下代码是其中一个标签。
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/notificationtab"
android:orientation="vertical"
android:paddingTop="0dp" >
<Button
android:id="@+id/btn_Delete"
android:layout_height="44dp"
android:layout_width="78dp"
android:text="@string/Archieve"
android:textColor="#ffffff"
android:layout_marginTop="30dp"
android:background="@drawable/custombutton5"
android:textSize="18sp"
android:layout_marginRight="15dp"
android:layout_alignParentRight="true"/>
<RelativeLayout
android:id="@+id/relative_layout2"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginTop="90dp">
<ListView
android:id="@+id/listView"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:choiceMode="multipleChoice">
</ListView>
</RelativeLayout>
<CheckBox
android:id="@+id/chkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btn_Retrieve_All_Notification"
android:layout_alignParentLeft="true"
android:layout_marginLeft="15dp"
android:text="@string/CHECK"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#f33347" />
<Button
android:id="@+id/btn_Retrieve_All_Notification"
android:layout_width="215dp"
android:layout_height="45dp"
android:layout_alignBaseline="@+id/btn_Delete"
android:layout_alignBottom="@+id/btn_Delete"
android:layout_marginRight="25dp"
android:layout_toLeftOf="@+id/btn_Delete"
android:background="@drawable/custombutton5"
android:text="@string/Retrive"
android:textColor="#ffffff"
android:textSize="18sp" />
</RelativeLayout>
java的引用行如下:
//This is line 81
lst_vw.setAdapter(new Adapterclass(getActivity().getBaseContext()));
解决:
我已使用共享首选项解决了该问题。
答案 0 :(得分:0)
您正在使用 startActivity(i); 来调用Fragment
这是错误的
你应该这样做:
Bundle b = new Bundle();
b.putString("Name", Name);
Fragment startingFragment = getFragmentManager().findFragmentById(R.id.yourfragment);
startingFragment.setArguments(b);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.yourfragment, startingFragment);
ft.commit();
这样你可以开始你的片段
然后在片段的onCrateView中,执行以下操作:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
String Name = getArguments().getString("Name", 0)
name = (TextView) rootView.findViewById(R.id.name);
name.setText(Name);
答案 1 :(得分:0)
从数据传递的活动中调用片段
Fragment frag;
Bundle b = new Bundle();
b.putString("Name", Name);
frag.setArguments(b);
FragmentManager fm1 = getApplicationContext()
.getSupportFragmentManager();
FragmentTransaction ft1 = fm1.beginTransaction();
frag = new Your_Fragment_Name();
ft1.replace(R.id.container, frag);
ft1.commit();
在片段类
中接收该字符串Bundle bundle = getArguments();
String name = bundle.getString("Name","deafalut_value");