尝试将链接添加到我的对话框但获取NullPointerException

时间:2015-11-06 15:37:28

标签: android hyperlink dialog

我想在我的对话框中添加一些链接,我有标准文本,但我也希望有一些可点击的链接。但我得到了NPE错误。我尝试了很多解决方案,但我总是遇到nullpointerexception错误;

这是我的代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            builder = new AlertDialog.Builder(new ContextThemeWrapper(this,
                    android.R.style.Theme_Holo_Light_Dialog));
        } else {
            builder = new AlertDialog.Builder(this);
        }

        builder.setTitle(getString(R.string.dialogTitle) + " " + getResources().getString(R.string.app_name));
        String msg = getResources().getString(R.string.dialogMessage);
        final SpannableString DialogMessage = new SpannableString(msg);
        Linkify.addLinks(DialogMessage, Linkify.ALL);
        builder.setMessage("Version " + AppVersion + "\n" + DialogMessage);
        builder.setNeutralButton(getString(R.string.neutralText),
                new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });
        builder.show();
        ((TextView) dialog.findViewById(android.R.id.message))
        .setMovementMethod(LinkMovementMethod.getInstance());

这是我的LogCat:

11-06 16:36:36.774: E/AndroidRuntime(8521): FATAL EXCEPTION: main
11-06 16:36:36.774: E/AndroidRuntime(8521): java.lang.NullPointerException
11-06 16:36:36.774: E/AndroidRuntime(8521):     at com.sm.flashlightpro.MainFlashlightActivity.showInfoDialog(MainFlashlightActivity.java:213)
11-06 16:36:36.774: E/AndroidRuntime(8521):     at com.sm.flashlightpro.MainFlashlightActivity$3.onClick(MainFlashlightActivity.java:124)
11-06 16:36:36.774: E/AndroidRuntime(8521):     at android.view.View.performClick(View.java:4421)
11-06 16:36:36.774: E/AndroidRuntime(8521):     at android.view.View$PerformClick.run(View.java:17903)
11-06 16:36:36.774: E/AndroidRuntime(8521):     at android.os.Handler.handleCallback(Handler.java:730)
11-06 16:36:36.774: E/AndroidRuntime(8521):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-06 16:36:36.774: E/AndroidRuntime(8521):     at android.os.Looper.loop(Looper.java:213)
11-06 16:36:36.774: E/AndroidRuntime(8521):     at android.app.ActivityThread.main(ActivityThread.java:5225)
11-06 16:36:36.774: E/AndroidRuntime(8521):     at java.lang.reflect.Method.invokeNative(Native Method)
11-06 16:36:36.774: E/AndroidRuntime(8521):     at java.lang.reflect.Method.invoke(Method.java:525)
11-06 16:36:36.774: E/AndroidRuntime(8521):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
11-06 16:36:36.774: E/AndroidRuntime(8521):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
11-06 16:36:36.774: E/AndroidRuntime(8521):     at dalvik.system.NativeStart.main(Native Method)

第213行是:

((TextView) dialog.findViewById(android.R.id.message))

2 个答案:

答案 0 :(得分:2)

可能dialognull

尝试使用从Dialog方法返回的show()对象:

dialog=builder.show();
TextView textView=(TextView)dialog.findViewById(android.R.id.message);

答案 1 :(得分:0)

你的对话来自哪里?

((TextView) dialog.findViewById(android.R.id.message))