我创建了一个示例片段应用程序。我有两个片段,如列表片段和细节片段。片段类的代码:
public class ListFragment1 extends ListFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
DetailFragment fragment = (DetailFragment) getFragmentManager()
.findFragmentById(R.id.detailFragment);
if (fragment != null && fragment.isInLayout()) {
fragment.setText(item);
} else {
Intent intent = new Intent(getActivity().getApplicationContext(),
DetailActivity.class);
intent.putExtra("value", item);
startActivity(intent);
}
}
}
细节片段类:
public class DetailFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("Test", "hello");
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details, container, false);
return view;
}
public void setText(String item) {
TextView view = (TextView) getView().findViewById(R.id.detailsText);
view.setText(item);
}
}
和Activity类DetailActivity类
public class DetailActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Need to check if Activity has been switched to landscape mode
// If yes, finished and go back to the start Activity
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
finish();
return;
}
setContentView(R.layout.details_activity_layout);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String s = extras.getString("value");
TextView view = (TextView) findViewById(R.id.detailsText);
view.setText(s);
}
}
}
主要活动类:
public class MyFragmentActivity extends FragmentActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
主要的.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
android:id="@+id/listFragment"
android:layout_width="150dip"
android:layout_height="wrap_content"
class="My.fragment.ListFragment1" ></fragment>
<fragment
android:id="@+id/detailFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
class="My.fragment.DetailFragment" >
<!-- Preview: layout=@layout/details -->
</fragment>
我的 Logcat 跟踪是..
05-28 10:42:28.866: W/dalvikvm(484): Unable to resolve superclass of LMy/Fragment/MyFragmentActivity; (21)
05-28 10:42:28.866: W/dalvikvm(484): Link of class 'LMy/Fragment/MyFragmentActivity;' failed
05-28 10:42:28.906: D/AndroidRuntime(484): Shutting down VM
05-28 10:42:28.906: W/dalvikvm(484): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
05-28 10:42:28.906: E/AndroidRuntime(484): Uncaught handler: thread main exiting due to uncaught exception
05-28 10:42:28.945: E/AndroidRuntime(484): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{My.Fragment/My.Fragment.MyFragmentActivity}: java.lang.ClassNotFoundException: My.Fragment.MyFragmentActivity in loader dalvik.system.PathClassLoader@44e8c748
05-28 10:42:28.945: E/AndroidRuntime(484): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
05-28 10:42:28.945: E/AndroidRuntime(484): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
05-28 10:42:28.945: E/AndroidRuntime(484): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
05-28 10:42:28.945: E/AndroidRuntime(484): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
05-28 10:42:28.945: E/AndroidRuntime(484): at android.os.Handler.dispatchMessage(Handler.java:99)
05-28 10:42:28.945: E/AndroidRuntime(484): at android.os.Looper.loop(Looper.java:123)
05-28 10:42:28.945: E/AndroidRuntime(484): at android.app.ActivityThread.main(ActivityThread.java:4363)
05-28 10:42:28.945: E/AndroidRuntime(484): at java.lang.reflect.Method.invokeNative(Native Method)
05-28 10:42:28.945: E/AndroidRuntime(484): at java.lang.reflect.Method.invoke(Method.java:521)
05-28 10:42:28.945: E/AndroidRuntime(484): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-28 10:42:28.945: E/AndroidRuntime(484): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-28 10:42:28.945: E/AndroidRuntime(484): at dalvik.system.NativeStart.main(Native Method)
05-28 10:42:28.945: E/AndroidRuntime(484): Caused by: java.lang.ClassNotFoundException: My.Fragment.MyFragmentActivity in loader dalvik.system.PathClassLoader@44e8c748
05-28 10:42:28.945: E/AndroidRuntime(484): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
05-28 10:42:28.945: E/AndroidRuntime(484): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
05-28 10:42:28.945: E/AndroidRuntime(484): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
05-28 10:42:28.945: E/AndroidRuntime(484): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
05-28 10:42:28.945: E/AndroidRuntime(484): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
05-28 10:42:28.945: E/AndroidRuntime(484): ... 11 more
我的 Manifest.xml 是
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="My.Fragment"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name="MyFragmentActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="DetailActivity"></activity>
</application>
</manifest>
这是我从互联网来源获得的第一个片段程序,在运行此应用程序时,我得到了运行时异常,一些类未找到异常。请给我正确的方法...