我在堆栈溢出中浏览了一些问题,但它没有解决我的问题。
这是我的代码。
public class MenuListActivity extends ListActivity {
String MenuNames[] = {"Consultation", "Medicine", "Diseases", "Doctor"};
protected void onCreateView(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, MenuNames);
final ListView lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
if(position == 1) {
//code specific to first list item
Intent myIntent = new Intent(v.getContext(), ConsultationActivity.class);
startActivity(myIntent);
}
}
}
);
}
logcat的。
10-12 10:53:37.572: E/AndroidRuntime(10605): FATAL EXCEPTION: main
10-12 10:53:37.572: E/AndroidRuntime(10605): Process: com.capstone.medicinehealthguide, PID: 10605
10-12 10:53:37.572: E/AndroidRuntime(10605): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.capstone.medicinehealthguide/com.capstone.medicinehealthguide.MenuListActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
10-12 10:53:37.572: E/AndroidRuntime(10605): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
10-12 10:53:37.572: E/AndroidRuntime(10605): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2317)
10-12 10:53:37.572: E/AndroidRuntime(10605): at android.app.ActivityThread.access$800(ActivityThread.java:143)
10-12 10:53:37.572: E/AndroidRuntime(10605): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
10-12 10:53:37.572: E/AndroidRuntime(10605): at android.os.Handler.dispatchMessage(Handler.java:102)
10-12 10:53:37.572: E/AndroidRuntime(10605): at android.os.Looper.loop(Looper.java:135)
10-12 10:53:37.572: E/AndroidRuntime(10605): at android.app.ActivityThread.main(ActivityThread.java:5070)
10-12 10:53:37.572: E/AndroidRuntime(10605): at java.lang.reflect.Method.invoke(Native Method)
10-12 10:53:37.572: E/AndroidRuntime(10605): at java.lang.reflect.Method.invoke(Method.java:372)
10-12 10:53:37.572: E/AndroidRuntime(10605): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:836)
10-12 10:53:37.572: E/AndroidRuntime(10605): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:631)
10-12 10:53:37.572: E/AndroidRuntime(10605): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
10-12 10:53:37.572: E/AndroidRuntime(10605): at com.capstone.medicinehealthguide.MenuListActivity.onCreate(MenuListActivity.java:24)
10-12 10:53:37.572: E/AndroidRuntime(10605): at android.app.Activity.performCreate(Activity.java:5720)
10-12 10:53:37.572: E/AndroidRuntime(10605): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102)
10-12 10:53:37.572: E/AndroidRuntime(10605): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2208)
10-12 10:53:37.572: E/AndroidRuntime(10605): ... 10 more
XML文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.capstone.medicinehealthguide.MenuListActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
我正在尝试制作一个包含列表视图的应用,当您点击它时,其他活动将会打开
答案 0 :(得分:1)
你实际上是在使用这个错误:
final ListView lv = (ListView) getListView().findViewById(R.id.listView1);
下面:
final ListView lv = (ListView) findViewById(R.id.listView1);
问题在于,您在获取列表视图,然后尝试从列表视图中获取列表视图。
尝试上面的选项,只需从内容视图中获取列表视图。
答案 1 :(得分:0)
在调用findByViewId
之前,视图必须膨胀。
你要做的是..将此代码从onCreate
方法移动到'onCreateView'方法。
在onCreateView
方法中,将其放在setContentView()
行以下
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, MenuNames);
final ListView lv = (ListView) getListView().findViewById(R.id.listView1);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
if(position == 1) {
//code specific to first list item
Intent myIntent = new Intent(v.getContext(), ConsultationActivity.class);
startActivity(myIntent);
}
}