我正在尝试将一个片段的 from 传递给与另一个片段data
相关联edittext
textview
我正在遵循在片段之间传递数据的静态方式
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/fragment2"
android:name="com.example.usingtwofragmentsfornavigatingbetweentwoactivities.Fragment2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<fragment
android:id="@+id/fragment1"
android:name="com.example.usingtwofragmentsfornavigatingbetweentwoactivities.Fragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
片段1.xml
<?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" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="Click to send data to next Activity" />
</RelativeLayout>
片段2.xml
<?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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="TextView"
android:textSize="40dp" />
</RelativeLayout>
片段1.java
package com.example.usingtwofragmentsfornavigatingbetweentwoactivities;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class Fragment1 extends Fragment{
Button button;
EditText textedit;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view=inflater.inflate(R.layout.fragment_1, container, false);
button=(Button) view.findViewById(R.id.button1);
textedit=(EditText) view.findViewById(R.id.editText1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentManager FM = getFragmentManager();
Fragment2 f2=(Fragment2) FM.findFragmentById(R.id.fragment2);
f2.setName(textedit.getText().toString());
}
});
return view;
}
}
片段2.java
package com.example.usingtwofragmentsfornavigatingbetweentwoactivities;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class Fragment2 extends Fragment{
View view;
TextView txtview;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
view=inflater.inflate(R.layout.fragment_2, container, false);
return view;
}
public void setName(String Name){
txtview=(TextView) view.findViewById(R.id.textView1);
txtview.setText("Hello:: " +Name);
}
}
[编辑]
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/fragment2"
class="com.example.usingtwofragmentsfornavigatingbetweentwoactivities.Fragment2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<fragment
android:id="@+id/fragment1"
class="com.example.usingtwofragmentsfornavigatingbetweentwoactivities.Fragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
片段1.java
package com.example.usingtwofragmentsfornavigatingbetweentwoactivities;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class Fragment1 extends Fragment{
Button button;
EditText textedit;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view=inflater.inflate(R.layout.fragment_1, container, false);
button=(Button) view.findViewById(R.id.button1);
textedit=(EditText) view.findViewById(R.id.editText1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentManager FM = getActivity().getSupportFragmentManager();
Fragment2 f2=(Fragment2) FM.findFragmentById(R.id.fragment2);
f2.setName(textedit.getText().toString());
}
});
return view;
}
}
日志::
10-30 14:37:32.900: D/AndroidRuntime(460): Shutting down VM
10-30 14:37:32.900: W/dalvikvm(460): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-30 14:37:32.940: E/AndroidRuntime(460): FATAL EXCEPTION: main
10-30 14:37:32.940: E/AndroidRuntime(460): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.usingtwofragmentsfornavigatingbetweentwoactivities/com.example.usingtwofragmentsfornavigatingbetweentwoactivities.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
10-30 14:37:32.940: E/AndroidRuntime(460): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.os.Handler.dispatchMessage(Handler.java:99)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.os.Looper.loop(Looper.java:123)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-30 14:37:32.940: E/AndroidRuntime(460): at java.lang.reflect.Method.invokeNative(Native Method)
10-30 14:37:32.940: E/AndroidRuntime(460): at java.lang.reflect.Method.invoke(Method.java:521)
10-30 14:37:32.940: E/AndroidRuntime(460): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-30 14:37:32.940: E/AndroidRuntime(460): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-30 14:37:32.940: E/AndroidRuntime(460): at dalvik.system.NativeStart.main(Native Method)
10-30 14:37:32.940: E/AndroidRuntime(460): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
10-30 14:37:32.940: E/AndroidRuntime(460): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
10-30 14:37:32.940: E/AndroidRuntime(460): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.app.Activity.setContentView(Activity.java:1647)
10-30 14:37:32.940: E/AndroidRuntime(460): at com.example.usingtwofragmentsfornavigatingbetweentwoactivities.MainActivity.onCreate(MainActivity.java:12)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-30 14:37:32.940: E/AndroidRuntime(460): ... 11 more
10-30 14:37:32.940: E/AndroidRuntime(460): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.usingtwofragmentsfornavigatingbetweentwoactivities-2.apk]
10-30 14:37:32.940: E/AndroidRuntime(460): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
10-30 14:37:32.940: E/AndroidRuntime(460): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
10-30 14:37:32.940: E/AndroidRuntime(460): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.view.LayoutInflater.createView(LayoutInflater.java:466)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
10-30 14:37:32.940: E/AndroidRuntime(460): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
10-30 14:37:32.940: E/AndroidRuntime(460): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
10-30 14:37:32.940: E/AndroidRuntime(460): ... 20 more
MainActivity.java
package com.example.usingtwofragmentsfornavigatingbetweentwoactivities;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
如何解决这个问题?