以下是ListFragment的程序。它不会显示任何错误但不会运行。错误日志显示消息Fatal Exception:Main...
当我在模拟器上打开时,它显示“很遗憾,listfragment2已停止”。
Frament1.java代码
package com.example.listfragmentexample2;
import android.annotation.TargetApi;
import android.app.ListFragment;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class Fragment1 extends ListFragment {
String[] presidents = {
"Dwight D. Eisenhower",
"John F. Kennedy",
"Lyndon B. Johnson",
"Richard Nixon",
"Gerald Ford",
"Jimmy Carter",
"Ronald Reagan",
"George H. W. Bush",
"Bill Clinton",
"George W. Bush",
"Barack Obama"
};
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, presidents));
}
public void onListItemClick(ListView parent, View v,
int position, long id)
{
Toast.makeText(getActivity(),
"You have selected " + presidents[position],
Toast.LENGTH_SHORT).show();
}
}
activitymain.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<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/fragment1"
android:name="com.example.listfragmentexample2.Fragment1"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_weight="0.5" />
<fragment
android:id="@+id/fragment2"
android:name="com.example.listfragmentexample2.Fragment1"
android:layout_width="0dp"
android:layout_height="300dp"
android:layout_weight="0.5" />
</LinearLayout>
fragment1.xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawSelectorOnTop="false" />
</LinearLayout>
以下是错误日志:
03-05 17:51:24.935: E/AndroidRuntime(755): FATAL EXCEPTION: main
03-05 17:51:24.935: E/AndroidRuntime(755): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.listfragmentexample2/com.example.listfragmentexample2.Fragment1}: java.lang.ClassCastException: com.example.listfragmentexample2.Fragment1 cannot be cast to android.app.Activity
03-05 17:51:24.935: E/AndroidRuntime(755): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
03-05 17:51:24.935: E/AndroidRuntime(755): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-05 17:51:24.935: E/AndroidRuntime(755): at android.app.ActivityThread.access$600(ActivityThread.java:130)
03-05 17:51:24.935: E/AndroidRuntime(755): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-05 17:51:24.935: E/AndroidRuntime(755): at android.os.Handler.dispatchMessage(Handler.java:99)
03-05 17:51:24.935: E/AndroidRuntime(755): at android.os.Looper.loop(Looper.java:137)
03-05 17:51:24.935: E/AndroidRuntime(755): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-05 17:51:24.935: E/AndroidRuntime(755): at java.lang.reflect.Method.invokeNative(Native Method)
03-05 17:51:24.935: E/AndroidRuntime(755): at java.lang.reflect.Method.invoke(Method.java:511)
03-05 17:51:24.935: E/AndroidRuntime(755): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-05 17:51:24.935: E/AndroidRuntime(755): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-05 17:51:24.935: E/AndroidRuntime(755): at dalvik.system.NativeStart.main(Native Method)
03-05 17:51:24.935: E/AndroidRuntime(755): Caused by: java.lang.ClassCastException: com.example.listfragmentexample2.Fragment1 cannot be cast to android.app.Activity
03-05 17:51:24.935: E/AndroidRuntime(755): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
03-05 17:51:24.935: E/AndroidRuntime(755): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
03-05 17:51:24.935: E/AndroidRuntime(755): ... 11 more
)
答案 0 :(得分:1)
Caused by: java.lang.ClassNotFoundException: com.example.listfragmentexample2.MainActivity
确保清单文件和代码中的包名和类名一致。
Manifest说应该有一个名为com.example.listfragmentexample2.MainActivity
的类,但是在二进制包中找不到它。如果你确定你有它,那就清理并重建,以防早期出现构建故障。
更新了更新问题的答案:
com.example.listfragmentexample2.Fragment1 cannot be cast to android.app.Activity
您的Fragment1
在清单中被宣布为activity
,但它不是extends Activity
。只应将活动列为清单中的活动。片段需要一个活动来托管它们。
答案 1 :(得分:0)
你需要在onCreateView方法之后设置适配器
public class Fragment1 extends ListFragment {
String[] presidents = {
"Dwight D. Eisenhower",
"John F. Kennedy",
"Lyndon B. Johnson",
"Richard Nixon",
"Gerald Ford",
"Jimmy Carter",
"Ronald Reagan",
"George H. W. Bush",
"Bill Clinton",
"George W. Bush",
"Barack Obama"
};
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false);
}
@Override
protected void onStart() {
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, presidents));
super.onStart();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void onListItemClick(ListView parent, View v,
int position, long id)
{
Toast.makeText(getActivity(),
"You have selected " + presidents[position],
Toast.LENGTH_SHORT).show();
}
}
答案 2 :(得分:0)
我认为您忘记在setContentView(R.layout.activity_main);
方法中添加onCreate()
。