レイアウト内の既存のフラグメントを置き換えるフラグメントを配置しようとしています
これは私の mainactivity.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="horizontal"
android:baselineAligned="false">
<LinearLayout
android:id="@+id/listviewFragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
></LinearLayout>
<LinearLayout
android:id="@+id/detailFragment"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
></LinearLayout>
</LinearLayout>
ここに私のメインアクティビティクラスがあります:
public class MainActivity extends FragmentActivity implements
FragmentListViewActivity.OnItemSelectedListener {
/* (non-Javadoc)
* @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
setContentView(R.layout.activity_main);
FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
FragmentListViewActivity toadd = new FragmentListViewActivity();
transaction.add(R.id.listviewFragment, toadd);
transaction.addToBackStack(null);
transaction.commit();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
DetailFragmentActivity detail = new DetailFragmentActivity();
ft.add(R.id.detailFragment, detail);
ft.addToBackStack(null);
ft.commit();
}
@Override
public void onItemSelected(String link) {
// TODO Auto-generated method stub
if(link != null && link.equalsIgnoreCase("Monday")){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
MondayActivity detail = new MondayActivity();
ft.replace(R.id.detailFragment, detail);
ft.addToBackStack(null);
ft.commit();
}
if(link != null && link.equalsIgnoreCase("Tuesday")){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
TuesdayActivity detail = new TuesdayActivity();
ft.replace(R.id.detailFragment, detail);
ft.addToBackStack(null);
ft.commit();
}
if(link != null && link.equalsIgnoreCase("Wednesday")){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
WednesdayActivity detail = new WednesdayActivity();
ft.replace(R.id.detailFragment, detail);
ft.addToBackStack(null);
ft.commit();
}
}
}
ここに私の FragmentlistViewActivity があります
public class FragmentListViewActivity extends Fragment {
private OnItemSelectedListener listener;
ListView listview;
String[] list = new String[] {"Monday","Tuesday","Wednesday"};
/* (non-Javadoc)
* @see android.app.ListFragment#onCreateView(android.view.LayoutInflater,
android.view.ViewGroup, android.os.Bundle)
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view =
inflater.inflate(R.layout.layout_fragment_listviewitems_screen,
container, false);
listview = (ListView)
view.findViewById(R.id.layout_frgment_listitems_listview);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
(getActivity(), android.R.layout.simple_list_item_1,list);
listview.setAdapter(arrayAdapter);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> av, View v, int position,long itemId) {
// TODO Auto-generated method stub
String link = (String) listview.getItemAtPosition(position);
listener.onItemSelected(link);
}
});
return view;
}
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 FragmentListViewActivity.OnItemSelectedListener");
}
}
}
ここに私のDetailFragmentActivityがあります:
public class DetailFragmentActivity extends Fragment {
/* (non-Javadoc)
* @see android.app.Fragment#onCreateView(android.view.LayoutInflater,
android.view.ViewGroup, android.os.Bundle)
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.layout_fragment_items_screen,
container, false);
return view;
}
}
ログキャット:
06-27 00:35:26.024: W/dalvikvm(25210): threadid=1: thread exiting with uncaught
exception (group=0x40a551f8)
06-27 00:35:26.064: E/AndroidRuntime(25210): FATAL EXCEPTION: main
06-27 00:35:26.064: E/AndroidRuntime(25210): android.view.InflateException: Binary XML
file line #2: Error inflating class fragment
06-27 00:35:26.064: E/AndroidRuntime(25210): at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
06-27 00:35:26.064: E/AndroidRuntime(25210): at
android.view.LayoutInflater.inflate(LayoutInflater.java:466)
06-27 00:35:26.064: E/AndroidRuntime(25210): at
android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-27 00:35:26.064: E/AndroidRuntime(25210): at
com.farmer.financial.estimation.MondayActivity.onCreateView(MondayActivity.java:26)
06-27 00:35:26.064: E/AndroidRuntime(25210): at
android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
06-27 00:35:26.064: E/AndroidRuntime(25210): at
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
06-27 00:35:26.064: E/AndroidRuntime(25210): at
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
06-27 00:35:26.064: E/AndroidRuntime(25210): at
android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-27 00:35:26.064: E/AndroidRuntime(25210): at
android.support.v4.app.FragmentManagerImpl.execPendingActions
(FragmentManager.java:1444)
06-27 00:35:26.064: E/AndroidRuntime(25210): at
android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
06-27 00:35:26.064: E/AndroidRuntime(25210): at
android.os.Handler.handleCallback(Handler.java:605)
06-27 00:35:26.064: E/AndroidRuntime(25210): at
android.os.Handler.dispatchMessage(Handler.java:92)
06-27 00:35:26.064: E/AndroidRuntime(25210): at
android.os.Looper.loop(Looper.java:137)
06-27 00:35:26.064: E/AndroidRuntime(25210): at
android.app.ActivityThread.main(ActivityThread.java:4424)
06-27 00:35:26.064: E/AndroidRuntime(25210): at
java.lang.reflect.Method.invokeNative(Native Method)
06-27 00:35:26.064: E/AndroidRuntime(25210): at
java.lang.reflect.Method.invoke(Method.java:511)
06-27 00:35:26.064: E/AndroidRuntime(25210): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-27 00:35:26.064: E/AndroidRuntime(25210): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-27 00:35:26.064: E/AndroidRuntime(25210): at
dalvik.system.NativeStart.main(Native Method)
06-27 00:35:26.064: E/AndroidRuntime(25210): Caused by:
java.lang.NullPointerException: name == null
06-27 00:35:26.064: E/AndroidRuntime(25210): at
java.lang.VMClassLoader.findLoadedClass(Native Method)
06-27 00:35:26.064: E/AndroidRuntime(25210): at
java.lang.ClassLoader.findLoadedClass(ClassLoader.java:354)
06-27 00:35:26.064: E/AndroidRuntime(25210): at
java.lang.ClassLoader.loadClass(ClassLoader.java:491)
06-27 00:35:26.064: E/AndroidRuntime(25210): at 1
java.lang.ClassLoader.loadClass(ClassLoader.java:461)
06-27 00:35:26.064: E/AndroidRuntime(25210): at
android.support.v4.app.Fragment.instantiate(Fragment.java:391)
06-27 00:35:26.064: E/AndroidRuntime(25210): at
android.support.v4.app.Fragment.instantiate(Fragment.java:369)
06-27 00:35:26.064: E/AndroidRuntime(25210): at
android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
06-27 00:35:26.064: E/AndroidRuntime(25210): at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:669)
06-27 00:35:26.064: E/AndroidRuntime(25210): ... 18 more
私はすべての可能性を試しましたが、いくつか間違っていました。解決策を教えてください。前もって感謝します。