2つのフラグメントが必要なアプリケーションを1つ作成したので、1つのフラグメントクラスを宣言し、そのクラス内に別の静的フラグメントクラスを作成しました。main.xmlを次のように変更しました
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:id="@+id/titles"
android:layout_width="25dp"
android:layout_height="wrap_content"
class="frag.demo.DActivity$TFragment" />
<fragment
android:id="@+id/details"
android:layout_width="25dp"
android:layout_height="wrap_content"
class="frag.demo.DActivity$DFragment" />
</FrameLayout>
このアプリケーションを実行すると、次のようなランタイム例外が発生しました
05-25 14:39:45.224: E/AndroidRuntime(911): Uncaught handler: thread main exiting due to uncaught exception
05-25 14:39:45.264: E/AndroidRuntime(911): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{frag.demo/frag.demo.JFragmentActivity}: java.lang.ClassNotFoundException: frag.demo.JFragmentActivity in loader dalvik.system.PathClassLoader@44e8c6f0
05-25 14:39:45.264: E/AndroidRuntime(911): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
05-25 14:39:45.264: E/AndroidRuntime(911): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
05-25 14:39:45.264: E/AndroidRuntime(911): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
05-25 14:39:45.264: E/AndroidRuntime(911): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
05-25 14:39:45.264: E/AndroidRuntime(911): at android.os.Handler.dispatchMessage(Handler.java:99)
05-25 14:39:45.264: E/AndroidRuntime(911): at android.os.Looper.loop(Looper.java:123)
05-25 14:39:45.264: E/AndroidRuntime(911): at android.app.ActivityThread.main(ActivityThread.java:4363)
05-25 14:39:45.264: E/AndroidRuntime(911): at java.lang.reflect.Method.invokeNative(Native Method)
05-25 14:39:45.264: E/AndroidRuntime(911): at java.lang.reflect.Method.invoke(Method.java:521)
05-25 14:39:45.264: E/AndroidRuntime(911): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-25 14:39:45.264: E/AndroidRuntime(911): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-25 14:39:45.264: E/AndroidRuntime(911): at dalvik.system.NativeStart.main(Native Method)
05-25 14:39:45.264: E/AndroidRuntime(911): Caused by: java.lang.ClassNotFoundException: frag.demo.JFragmentActivity in loader dalvik.system.PathClassLoader@44e8c6f0
05-25 14:39:45.264: E/AndroidRuntime(911): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
05-25 14:39:45.264: E/AndroidRuntime(911): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
05-25 14:39:45.264: E/AndroidRuntime(911): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
05-25 14:39:45.264: E/AndroidRuntime(911): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
05-25 14:39:45.264: E/AndroidRuntime(911): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
05-25 14:39:45.264:E / AndroidRuntime(911):...11詳細
そして、私のJFragmentActivityクラスはFragmentActivityを拡張します。だから、コードがうまくいかないところを教えてください...
マイアクティビティクラス
public class DActivity extends FragmentActivity {// from android.support.v4.app.*;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("DA","active");
if (getResources().getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE) {
// If the screen is now in landscape mode, we can show the
// dialog in-line with the list so we don't need this activity.
finish();
return;
}
if (savedInstanceState == null) {
// During initial setup, plug in the details fragment.
DFragment details = new DFragment();
details.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
}
}
public static class TFragment extends ListFragment {
boolean mDualPane;
int mCurCheckPosition = 0;
String nos[]={"1","2","3","4","5"};
public TFragment(){
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
Log.i("TF","active");
setListAdapter(new ArrayAdapter<String>(getActivity(),R.layout.simple_list,nos));
View detailsFrame = getActivity().findViewById(R.id.nos);
mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;
if (savedInstanceState != null) {
// Restore last state for checked position.
mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
}
if (mDualPane) {
// In dual-pane mode, the list view highlights the selected item.
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
// Make sure our UI is in the correct state.
showDetails(mCurCheckPosition);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("curChoice", mCurCheckPosition);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
showDetails(position);
}
void showDetails(int index) {
mCurCheckPosition = index;
if (mDualPane) {
// We can display everything in-place with fragments, so update
// the list to highlight the selected item and show the data.
getListView().setItemChecked(index, true);
// Check what fragment is currently shown, replace if needed.
DFragment details = (DFragment)
getFragmentManager().findFragmentById(R.id.nos);
if (details == null || details.getShownIndex() != index) {
// Make new fragment to show this selection.
details = DFragment.newInstance(index);
// Execute a transaction, replacing any existing fragment
// with this one inside the frame.
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.nos, details);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
} else {
// Otherwise we need to launch a new activity to display
// the dialog fragment with selected text.
Intent intent = new Intent();
intent.setClass(getActivity(), DActivity.class);
intent.putExtra("index", index);
startActivity(intent);
}
}
}
public static class DFragment extends Fragment {
String names[]={"aaa","bbb","ccc","ddd","eee"};
public DFragment() {
// TODO Auto-generated constructor stub
}
public static DFragment newInstance(int index){
DFragment f= new DFragment();
Bundle args=new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
public int getShownIndex() {
return getArguments().getInt("index", 0);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.i("DF","active");
if (container == null) {
return null;
}
ScrollView scroller = new ScrollView(getActivity());
TextView text = new TextView(getActivity());
int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
4, getActivity().getResources().getDisplayMetrics());
text.setPadding(padding, padding, padding, padding);
scroller.addView(text);
text.setText(names[getShownIndex()]);
return scroller;
}
}
}
そして私の主な活動クラスは
public class JFragmentActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("main","active");
setContentView(R.layout.main);
/*Intent i = new Intent(getApplicationContext(), DActivity.class);
startActivity(i);*/
}
}