我尝试使用ListView和按钮显示简单对话框。
MainActivity.java:
package com.example.myapplication.app;
import android.app.Dialog;
import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends ActionBarActivity {
final Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void add_contact(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.contact_list);
final ListView lv = (ListView)findViewById(R.id.lstVw_contacts);
String[] values = new String[] { "one","two","three" };
final ArrayAdapter adapter = new ArrayAdapter(context,
android.R.layout.simple_list_item_1, values);
lv.setAdapter(adapter); // fail HERE. Without this line I take empty ListView
Button dialogButton = (Button) dialog.findViewById(R.id.action_dialog_close);
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml中:
<FrameLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:weightSum="1"
tools:context="com.example.myapplication.app.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add contacts"
android:onClick="add_contact"
android:id="@+id/action_add_contact"/>
</FrameLayout>
contact_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"
android:id="@+id/lstVw_contacts" />
<Button
android:id="@+id/action_dialog_close"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Close"
android:layout_below="@+id/text"
android:layout_centerHorizontal="true" />
</LinearLayout>
当我使用ArrayAdapter在MainActivity中使用ListView时,它没问题。 (类似代码)
当我在Dialog中使用ListView和ArrayAdapter时出现错误。
为什么呢?如何在没有ArrayAdapter的情况下从数组中填充ListView?
logcatg:
05-24 13:25:47.236 2864-2864/com.example.myapplication.app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb4adcba8)
05-24 13:25:47.266 2864-2864/com.example.myapplication.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.myapplication.app, PID: 2864
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3823)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.myapplication.app.MainActivity.add_contact(MainActivity.java:49)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
它失败了,因为ArrayAdapter
没有附加到右侧Context
。实际上,您应该使用this
变量而不是Context
作为context
参数:
final ArrayAdapter adapter = new ArrayAdapter(context, android.R.layout.simple_list_item_1, values);
更新:
lv
变量发生空指针异常,因为lv = null
。为什么?由于ListView属于contact_list.xml
而不属于activity_main.xml
,因此您必须附加正确的布局才能找到ID:
final ListView lv = (ListView) dialog.findViewById(R.id.lstVw_contacts);
使用dialog.findViewById(...)
是指dialog.setContentView(...)
有R.layout.contact_list
的内容视图,而ListView在此布局中,您需要在方法中引用它。