0

我创建了一个示例片段应用程序。我有两个片段,如列表片段和细节片段。片段类的代码:

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>

这是我从互联网来源获得的第一个片段程序,在运行此应用程序时,我得到了运行时异常,一些类未找到异常。请给我正确的方法...

4

4 回答 4

0

您的包在 Android Manifest 中称为 My.Fragment,但在布局中您已将其指定为 My.fragment

于 2012-05-28T05:07:55.253 回答
0

当您的代码中将My.fragment其定义为My.Fragment.

这就是为什么你得到一个ClassNotFoundException.

于 2012-05-28T05:10:56.967 回答
0

您的main.xml代码包含一个带有 id 的片段detailfragment。它应该是线性布局而不是片段。

于 2012-06-25T13:18:56.780 回答
0

在 main.xml 文件中,您应该正确定义 class="My.fragment.DetailFragment"。

我在网上看到了这个例子,我也有同样的例外。

我用包名定义了“DetailFragment”位置,它可以工作!

但是在这个原始示例中,它是由项目名称定义的,因此片段无法找到该类。

于 2012-08-02T08:53:49.817 回答