我在Vogel网站上找到了AndroidFragment教程,并在Android 4.0上做到了。然后,我尝试将其移植到较低版本,例如API 8。
每当我启动我的应用程序时,我都会遇到ClassNotFound异常
请帮助我修复错误。
有我的MainActivity文件:
package com.dev.fragments;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
有我的main.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment
android:id="@+id/listFragment"
android:layout_width="150dip"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize"
class="com.dev.fragments.ListFragment" ></fragment>
<fragment
android:id="@+id/detailFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.dev.fragments.DetailFragment" >
<!-- Preview: layout=@layout/details -->
</fragment>
</LinearLayout>
有我的ListFragment文件:
package com.dev.fragments;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListFragment extends android.support.v4.app.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", "Max OS X",
"Linux", "OS/2" };
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);
}
}
}
有我的DetailFragment文件:
package com.dev.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
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);
}
}
有我的清单文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dev.fragments"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="4"
android:targetSdkVersion="15" />
<application
android:label="@string/app_name">
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<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>
还有我的错误:
08-08 08:55:02.126: E/AndroidRuntime(454): FATAL EXCEPTION: main
08-08 08:55:02.126: E/AndroidRuntime(454): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.dev.fragments/com.dev.fragments.MainActivity}: java.lang.ClassNotFoundException: com.dev.fragments.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.dev.fragments-2.apk]
08-08 08:55:02.126: E/AndroidRuntime(454): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
08-08 08:55:02.126: E/AndroidRuntime(454): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-08 08:55:02.126: E/AndroidRuntime(454): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-08 08:55:02.126: E/AndroidRuntime(454): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-08 08:55:02.126: E/AndroidRuntime(454): at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 08:55:02.126: E/AndroidRuntime(454): at android.os.Looper.loop(Looper.java:123)
08-08 08:55:02.126: E/AndroidRuntime(454): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-08 08:55:02.126: E/AndroidRuntime(454): at java.lang.reflect.Method.invokeNative(Native Method)
08-08 08:55:02.126: E/AndroidRuntime(454): at java.lang.reflect.Method.invoke(Method.java:521)
08-08 08:55:02.126: E/AndroidRuntime(454): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-08 08:55:02.126: E/AndroidRuntime(454): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-08 08:55:02.126: E/AndroidRuntime(454): at dalvik.system.NativeStart.main(Native Method)
08-08 08:55:02.126: E/AndroidRuntime(454): Caused by: java.lang.ClassNotFoundException: com.dev.fragments.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.dev.fragments-2.apk]
08-08 08:55:02.126: E/AndroidRuntime(454): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
08-08 08:55:02.126: E/AndroidRuntime(454): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
08-08 08:55:02.126: E/AndroidRuntime(454): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
08-08 08:55:02.126: E/AndroidRuntime(454): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
08-08 08:55:02.126: E/AndroidRuntime(454): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
最佳答案
我猜这是一个logcat错误?
我没有受过v4支持软件包移植的培训,但似乎您的MainActivity没有正确部署,尽管没有正确放入com.dev.fragments-2.apk中。
关于android - 从11 API到更低版本的端口 fragment 无效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11861048/