我试图使用一个类扩展为Listfragment但我无法打开它。
我有2个活动,firts是主要的,并扩展ActionBarActivity以获取旧API的操作栏:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
public class MainActivity extends ActionBarActivity {
//......
public void makeAndEvent (View v){
Intent intent = new Intent(getBaseContext(), Events.class);
startActivity(intent);
}
}
这里的秒数是冲突的:
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Events extends ListFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_events, container, false);
return rootView;
}
}
但是,当应用程序尝试启动第二个活动,事件时,我在日志中收到此错误:
08-02 18:59:38.279: E/AndroidRuntime(12284): FATAL EXCEPTION: main
08-02 18:59:38.279: E/AndroidRuntime(12284): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.app.myapp/com.app.myapp.Events}: java.lang.ClassCastException: com.app.myapp.Events cannot be cast to android.app.Activity
08-02 18:59:38.279: E/AndroidRuntime(12284): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1998)
08-02 18:59:38.279: E/AndroidRuntime(12284): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2099)
08-02 18:59:38.279: E/AndroidRuntime(12284): at android.app.ActivityThread.access$600(ActivityThread.java:138)
08-02 18:59:38.279: E/AndroidRuntime(12284): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
08-02 18:59:38.279: E/AndroidRuntime(12284): at android.os.Handler.dispatchMessage(Handler.java:99)
08-02 18:59:38.279: E/AndroidRuntime(12284): at android.os.Looper.loop(Looper.java:137)
08-02 18:59:38.279: E/AndroidRuntime(12284): at android.app.ActivityThread.main(ActivityThread.java:4929)
08-02 18:59:38.279: E/AndroidRuntime(12284): at java.lang.reflect.Method.invokeNative(Native Method)
08-02 18:59:38.279: E/AndroidRuntime(12284): at java.lang.reflect.Method.invoke(Method.java:511)
08-02 18:59:38.279: E/AndroidRuntime(12284): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
08-02 18:59:38.279: E/AndroidRuntime(12284): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
08-02 18:59:38.279: E/AndroidRuntime(12284): at dalvik.system.NativeStart.main(Native Method)
08-02 18:59:38.279: E/AndroidRuntime(12284): Caused by: java.lang.ClassCastException: com.app.myapp.Events cannot be cast to android.app.Activity
08-02 18:59:38.279: E/AndroidRuntime(12284): at android.app.Instrumentation.newActivity(Instrumentation.java:1056)
08-02 18:59:38.279: E/AndroidRuntime(12284): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1989)
有人可以帮助我吗?
答案 0 :(得分:1)
无法启动碎片。它们将被包含在活动中。您可以使用ListActivity或在FragmentActivity上添加ListFragment。