我想將我的項目從Activity更改爲FragmentActivity。第一步是創建片段:開始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是一個簡單的LinearLayout與一個TextView
第二步是(我的閃屏後)使父類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" />
在我SplashScr EEN是說知道:
Intent myInt = new Intent(this, TestFrag.class);
startActivity(myInt);
但alwas我得到這些錯誤:
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"
和targetSdk="17"
。 因爲我用的支持-V4我對片段一流的進口現在
import android.app.support.v4.Fragment
我已經改變,要
import android.app.Fragment
,它似乎工作...
此外,我必須將FragmentActivity
更改爲Activity
。因爲FragmentActivity
只是支持v4軟件包的一個類。
更新2 - 發現的解決方案: Here is the solution
你有什麼minSdk,targetSdk?您是否使用兼容包? – MikeWallaceDev
我有minSdk =「14」和tagetSdk =「17」。另外,我還有Import-Support-v4軟件包並希望使用它們... – StefMa
由於minSdk是14,所以它不會令人滿意。如果minSdk小於11,則需要此庫。片段出現在API v11上。 – Rusfearuth