MyFragment.java
public class MyFragment extends Fragment {
private onItemSelectedListener listener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container,
false);
btn_myButton = (Button) view.findViewById(R.id.btn_new_client);
btn_myButton .setOnClickListener(new OnClickListener() {
public void onClick(View v) {
updateDetail("New Layout");
}
});
return view;
}
Button btn_myButton;
public interface onItemSelectedListener {
public void onItemSelected(String link);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof onItemSelectedListener) {
listener = (onItemSelectedListener) activity;
} else {
throw new ClassCastException(activity.toString()
+ " must implemenet OnItemSelectedListener");
}
}
public void updateDetail(String s) {
listener.onItemSelected(s);
}
}
MainActivity.java
public class MainActivity extends Activity implements
onItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.main_activity);
super.onCreate(savedInstanceState);
}
public void onItemSelected(String link) {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Fragment fragment;
if ("New Layout".equals(link)) {
fragment = new Fragment1();
transaction.replace(R.id.detailFragment, fragment);
transaction.commit();
}
}
}
Fragment1.java
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.newfragmentt, container,
false);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_new_client"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="8"
android:gravity="center|center_vertical"
android:padding="2dp"
android:text="@string/new_client"
android:textColor="#FFFFFF" >
</Button>
</LinearLayout>
main_activity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#4B6C9E"
android:baselineAligned="false"
android:orientation="horizontal" >
<fragment
android:id="@+id/FragmentId"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
class="com.examples.MyProject.MyFragment" >
</fragment>
<FrameLayout
android:id="@+id/detailFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" >
</FrameLayout>
</LinearLayout>
LogCat error:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.elintsys.iCaseDiary/com.examples.MyProject.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
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:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
at android.app.Activity.setContentView(Activity.java:1862)
at com.examples.MyProject.MainActivity.onCreate(CaseEntryActivity.java:15)
at android.app.Activity.performCreate(Activity.java:4492)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
... 11 more
Caused by: java.lang.IllegalStateException: Fragment com.elintsys.iCaseDiary.CaseEntryFragment did not create a view.
at android.app.Activity.onCreateView(Activity.java:4293)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673)
... 21 more
Sending signal. PID: 16275 SIG: 9
Please help me friends.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…