0

我想将我的项目从 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

它似乎工作......

我也必须更改FragmentActivityActivity. 因为这FragmentActivity只是支持 v4 包的一个类!

更新 2 - 找到解决方案: 这是解决方案

4

3 回答 3

0

好的,这里是解决方案。我不确切知道 folling 代表什么以及它是什么,但它运行并且在这些解决方法之后我现在有错误:

Close eclipse-> remove support-V4-> open eclipse-> right click on your project-> AndroidTools-> Add Support Libary-> right click on your project-> Properties-> Choose java build path-> Choose 'Order and Export'-> Select All-> Click 'ok'-> Project-> Clean->Run and stay happy!

于 2013-05-19T09:06:34.850 回答
0

我认为您不需要使用 support-v4 库。请告诉我你为什么使用那个库?我看到 min sdk 是 14,在这种情况下使用 sopport-v4 是没用的。

请检查 ws.stefma.projectname 包,因为 logcat 说 ws.stefma.projectname.TestFrag 不存在。

于 2013-05-19T04:12:02.687 回答
0

将 R.layout.frag_main 更改为:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <fragment
           android:id="@+id/fragment1"
           android:name="ws.stefma.projectname.Frag1"
           android:layout_width="match_parent"
           android:layout_height="wrap_content" />

    </LinearLayout>
于 2013-05-18T21:29:39.533 回答