我想将我的项目从 Activity 更改为 FragmentActivity。第一步是创建片段:
public class Frag1 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater
.inflate(R.layout.detail_fragment, container, false);
return view;
}
}
detail_fragment 是一个带有 TextView 的简单 LinearLayout
第二步是(在我的 SplashScreen 之后)将父类设为 FragmentActivity:
public class TestFrag extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.frag_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.activity_main, menu);
return true;
}
}
R.layout.frag_main:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frag"
android:name=".fragments.Frag1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
在我的 SplashScreen 上说知道:
Intent myInt = new Intent(this, TestFrag.class);
startActivity(myInt);
但我总是收到这些错误:
05-18 23:09:51.543: W/dalvikvm(28644): Unable to resolve superclass of Lws/stefma/projectname/TestFrag; (20)
05-18 23:09:51.543: W/dalvikvm(28644): Link of class 'Lws/stefma/projectname/TestFrag;' failed
05-18 23:09:51.543: E/dalvikvm(28644): Could not find class 'ws.stefma.projectname.TestFrag', referenced from method ws.stefma.projectname.Activity_Login_Registration.onCreate
05-18 23:09:51.543: W/dalvikvm(28644): VFY: unable to resolve const-class 94 (Lws/stefma/projectname/TestFrag;) in Lws/stefma/projectname/Activity_Login_Registration;
05-18 23:09:51.543: D/dalvikvm(28644): VFY: replacing opcode 0x1c at 0x000b
05-18 23:09:51.583: D/AndroidRuntime(28644): Shutting down VM
05-18 23:09:51.583: W/dalvikvm(28644): threadid=1: thread exiting with uncaught exception (group=0x41850930)
05-18 23:09:51.593: E/AndroidRuntime(28644): FATAL EXCEPTION: main
05-18 23:09:51.593: E/AndroidRuntime(28644): java.lang.NoClassDefFoundError: ws.stefma.projectname.TestFrag
05-18 23:09:51.593: E/AndroidRuntime(28644): at ws.stefma.projectname.Activity_Login_Registration.onCreate(Activity_Login_Registration.java:30)
05-18 23:09:51.593: E/AndroidRuntime(28644): at android.app.Activity.performCreate(Activity.java:5104)
05-18 23:09:51.593: E/AndroidRuntime(28644): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-18 23:09:51.593: E/AndroidRuntime(28644): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-18 23:09:51.593: E/AndroidRuntime(28644): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-18 23:09:51.593: E/AndroidRuntime(28644): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-18 23:09:51.593: E/AndroidRuntime(28644): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-18 23:09:51.593: E/AndroidRuntime(28644): at android.os.Handler.dispatchMessage(Handler.java:99)
05-18 23:09:51.593: E/AndroidRuntime(28644): at android.os.Looper.loop(Looper.java:137)
05-18 23:09:51.593: E/AndroidRuntime(28644): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-18 23:09:51.593: E/AndroidRuntime(28644): at java.lang.reflect.Method.invokeNative(Native Method)
05-18 23:09:51.593: E/AndroidRuntime(28644): at java.lang.reflect.Method.invoke(Method.java:511)
05-18 23:09:51.593: E/AndroidRuntime(28644): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-18 23:09:51.593: E/AndroidRuntime(28644): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-18 23:09:51.593: E/AndroidRuntime(28644): at dalvik.system.NativeStart.main(Native Method)
我不知道为什么......对此感到困惑:如果我从FragmentActivity
所有Activity
运行正常而没有错误!
我的 AndroidManifest 中也有这些:
<activity android:name="ws.stefma.projectname.TestFrag" />
更新:
我已经导入了support-v4 libary
. 我的AndroidManifest
被宣布为minSdk="14"
and targetSdk="17"
。因为我使用了我在 Fragment-Class 上导入的 support-v4
import android.app.support.v4.Fragment
现在我已将其更改为
import android.app.Fragment
它似乎工作......
我也必须更改FragmentActivity
为Activity
. 因为这FragmentActivity
只是支持 v4 包的一个类!
更新 2 - 找到解决方案: 这是解决方案