项目的警报对话框(列表)自定义行

时间:2014-01-31 15:21:38

标签: java android alertdialog

我正在尝试使用以下代码弹出由字符串数组支持的列表:

public void gameDialog(Activity activity) {



        TextView title = new TextView(activity);
        title.setText("Click a number to make a call");
        title.setBackgroundColor(Color.BLACK);
        title.setPadding(10, 10, 10, 10);
        title.setGravity(Gravity.CENTER);
        title.setTextColor(Color.WHITE);
        title.setTextSize(20);


        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setCustomTitle(title);
        //builder.setIcon(R.drawable.alert_36);

    //  builder.setMessage(""+arrayPhones);

        builder.setCancelable(false);
        builder.setItems(arrayPhones,new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialoginterface, int i) {



            }

        });

        AlertDialog alert = builder.show();


        TextView messageText = (TextView)alert.findViewById(android.R.id.message);
        messageText.setGravity(Gravity.CENTER);
    //  messageText.setTextColor(Color.RED);

    }

当我尝试将属性设置为messageText时,我得到一个空指针。我想通过将文本放在中心和不同的字体颜色来自定义上面列表的行。我错了什么提示?

这是logCat:

01-31 20:55:06.328: E/InputEventReceiver(25240): Exception dispatching input event.
01-31 20:55:06.328: E/MessageQueue-JNI(25240): Exception in MessageQueue callback: handleReceiveCallback
01-31 20:55:06.339: E/MessageQueue-JNI(25240): java.lang.NullPointerException
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at com.exa.birthdayrem.Fragment1_3.showAlert(Fragment1_3.java:347)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at com.exa.birthdayrem.Fragment1_3$1.onTouch(Fragment1_3.java:189)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.view.View.dispatchTouchEvent(View.java:7138)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2238)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1935)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2244)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1907)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2244)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1907)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2244)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1907)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2244)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1907)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2244)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1907)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2244)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1907)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2244)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1907)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2244)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1907)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1938)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1392)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.app.Activity.dispatchTouchEvent(Activity.java:2408)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1886)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.view.View.dispatchPointerEvent(View.java:7323)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3560)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3490)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4637)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4597)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4749)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.os.MessageQueue.nativePollOnce(Native Method)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.os.MessageQueue.next(MessageQueue.java:125)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.os.Looper.loop(Looper.java:140)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at android.app.ActivityThread.main(ActivityThread.java:5000)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at java.lang.reflect.Method.invokeNative(Native Method)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at java.lang.reflect.Method.invoke(Method.java:511)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
01-31 20:55:06.339: E/MessageQueue-JNI(25240):  at dalvik.system.NativeStart.main(Native Method)
01-31 20:55:06.339: D/AndroidRuntime(25240): Shutting down VM
01-31 20:55:06.340: W/dalvikvm(25240): threadid=1: thread exiting with uncaught exception (group=0x40f67908)
01-31 20:55:06.346: E/AndroidRuntime(25240): FATAL EXCEPTION: main
01-31 20:55:06.346: E/AndroidRuntime(25240): java.lang.NullPointerException
01-31 20:55:06.346: E/AndroidRuntime(25240):    at com.exa.birthdayrem.Fragment1_3.showAlert(Fragment1_3.java:347)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at com.exa.birthdayrem.Fragment1_3$1.onTouch(Fragment1_3.java:189)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.view.View.dispatchTouchEvent(View.java:7138)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2238)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1935)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2244)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1907)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2244)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1907)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2244)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1907)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2244)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1907)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2244)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1907)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2244)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1907)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2244)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1907)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2244)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1907)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1938)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1392)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.app.Activity.dispatchTouchEvent(Activity.java:2408)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1886)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.view.View.dispatchPointerEvent(View.java:7323)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3560)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3490)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4637)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4597)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4749)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.os.MessageQueue.nativePollOnce(Native Method)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.os.MessageQueue.next(MessageQueue.java:125)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.os.Looper.loop(Looper.java:140)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at android.app.ActivityThread.main(ActivityThread.java:5000)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at java.lang.reflect.Method.invokeNative(Native Method)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at java.lang.reflect.Method.invoke(Method.java:511)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
01-31 20:55:06.346: E/AndroidRuntime(25240):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

你真的需要它作为对话吗?

您可以创建一个新的Activity并添加对话框主题。如果您使用4.0之前的设备,则必须执行以下操作:

创建文件夹res / values-v11(这适用于API 11+),然后添加名为styles.xml的文件。在此文件中键入以下内容:

<resources>
    <style name="my_activity_theme" parent="android:Theme.Holo.Light"/>
</resources>

然后在位于in / res / values文件夹中的styles.xml添加以下行:

<resources>
    <style name="my_activity_theme" parent="android:Theme.Light"/>
</resources>

然后,将其应用于AndroidManifest.xml中的应用程序:

<activity
    ...
    <android:theme="@style/my_activity_theme"/>

</activity

这样您就可以进行浮动活动。我没有测试过,但我认为值得尝试。

希望有所帮助

答案 1 :(得分:0)

在您的代码中,AlertDialog尚未初始化,因此它为您提供nullpointer。通过Builder的create()方法初始化您的AlertDialog。类似的东西:

 AlertDialog alert = builder.create();
 alert.show();

此外你还没有使用任何视图。在您的方法参数中,使用活动获取一个视图,并通过此方法初始化视图组件,而不是通过警报。

public void gameDialog(Activity activity, View view) {
TextView tv = (TextView) view.findViewById(R.id.message);
}

并通过您的参数传递保存textview的父布局。