自定义对话框,列表视图崩溃

时间:2012-02-24 22:10:32

标签: android listview dialog adapter

我尝试了不同的东西,但事情仍然没有'去'

我有一项活动。在这里,当我按下菜单中的一个选项时,我需要弹出一个对话框,并在该对话框中显示字符串值的列表视图,我从字符串数组xml中获取。

我已经删除了一些代码来清理它但仍然没有运气......

myList.xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>

我的自定义对话框

public class Selector extends Dialog {

String[] testArray;
protected Selector(Context context) {
    super(context);

    ListView lijstje = (ListView) findViewById(R.layout.mylist);
    testArray = context.getResources().getStringArray(R.array.currencies);
    Log.d("test",testArray[0]);//wich shows me the 1e string and it's working
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getContext(),android.R.layout.simple_list_item_1, testArray);
    lijstje.setAdapter(adapter);

    LayoutInflater li = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = li.inflate(R.layout.mylist, null, false);
    this.setContentView(v);

}

}

在我的主要活动中,我有功能

public void showPopup()
{       
    Selector test = new Selector(this);
    test.show();
}

当我把lijstje.setAdapter(适配器);在评论我的应用程序没有崩溃,但对话框非常小而空

02-24 22:14:29.705: D/AndroidRuntime(28734): Shutting down VM
02-24 22:14:29.705: W/dalvikvm(28734): threadid=1: thread exiting with uncaught exception (group=0x40a4e1f8)
02-24 22:14:29.715: E/AndroidRuntime(28734): FATAL EXCEPTION: main
02-24 22:14:29.715: E/AndroidRuntime(28734): java.lang.NullPointerException
02-24 22:14:29.715: E/AndroidRuntime(28734):    at be.veeteedev.OmzetterActivity.showPopup(OmzetterActivity.java:83)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at be.veeteedev.OmzetterActivity.onOptionsItemSelected(OmzetterActivity.java:59)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at android.app.Activity.onMenuItemSelected(Activity.java:2502)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:950)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:163)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at android.widget.AdapterView.performItemClick(AdapterView.java:292)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at android.widget.AbsListView$1.run(AbsListView.java:3168)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at android.os.Handler.handleCallback(Handler.java:605)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at android.os.Looper.loop(Looper.java:137)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at android.app.ActivityThread.main(ActivityThread.java:4424)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at java.lang.reflect.Method.invokeNative(Native Method)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at java.lang.reflect.Method.invoke(Method.java:511)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-24 22:14:29.715: E/AndroidRuntime(28734):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

我不确定这会不可思议地解决您遇到的所有问题,但在您的Selector构造函数中,您有以下任务:

ListView lijstje = (ListView) findViewById(R.layout.mylist);

请注意您是如何尝试查找具有布局ID的视图。应该说R.id.mylist之类的东西。目前,它将解析为null,因此NullPointerException

此外,您最后一直在做setContentView(...)。如果您尝试通过上述布局文件(R.layout.mylist)来扩充视图,请确保在设置内容视图后执行此操作。

顺便说一句,如果您没有显示列表的对话框的花哨布局,则可以使用默认的AlertDialog构建器。 Have a read here, samples included。有关自定义对话框的一些帮助,请查看同一页面的further down the bottom

答案 1 :(得分:0)

我猜这是你的问题。

没有什么可以找到的
ListView lijstje = (ListView) findViewById(R.layout.mylist);
以这种方式调用时,

lijstje为null lijstje是一个布局而不是视图

还应该在其他任何内容之前设置内容视图

public class Selector extends Dialog {

String[] testArray;
protected Selector(Context context) {
    super(context);
    setContentView(R.layout.list);

    ListView lijstje = (ListView) findViewById(R.id.mylist);
    testArray = context.getResources().getStringArray(R.array.currencies);
    Log.d("test",testArray[0]);//wich shows me the 1e string and it's working
    ArrayAdapter<String> adapter = new ArrayAdapter<String> (this.getContext(),android.R.layout.simple_list_item_1, testArray);
    lijstje.setAdapter(adapter);
}

mylist.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ListView 
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>