android - 从 Dialog 类打开 url

标签 android nullpointerexception

我尝试打开一个对话框类的 url。我用下面的代码尝试了它,但我得到了 NullPointerException。我该怎么做?

我从我的 Activity 开始对话:

AboutDialog about = new AboutDialog(this);
about.setTitle(R.string.Text_about);
about.show();

这是对话框类:

public class AboutDialog extends Dialog implements OnClickListener{
private Button button_ok;
.
.
.

}

public void onClick(View arg0) {
            myActivity activity = new myActivity();
            String url = "http://www.test.com";
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            activity.startActivity(i); 
}

最佳答案

您正在 onCick 方法中创建一个新 Activity 。不应手动创建 Activity 。它们是从系统设置的。我猜是 Activity 设置不正确,这会导致 NullPointerException。

您应该通过 getContext 获得有效的上下文.使用

getContext().startActivity(i); 

代替

activity.startActivity(i); 

关于android - 从 Dialog 类打开 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13218386/

相关文章:

android - 水平 ScrollView 中的回收器 View 在 android oreo 中不起作用,但在以下版本中运行良好

android - lockCanvas() 上的 NullPointerException

java - NullPointerException 导致 Android 应用程序崩溃

java - 记录 log4j 和 Null 对象

尝试在 java static 中打印矩阵值时出现 java.lang.NullPointerException

android应用程序直接使用android设备中保存的帐户凭据登录

android - Android 2.3.3 中的 HTML 标签解析错误

android - Google 钱包的发布是否改变了 Android 开发者可用的任何功能?

android - Proguard 不再适用于 Retrofit

Java:列表、bufferedScanner 和错误:java.lang.NullPointerException