0

当有人单击 textview 时,我正在尝试打开 Web 浏览器,但是一旦我运行该应用程序,我就会收到 Null 指针异常错误

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setMovementMethod(android.text.method.MovementMethod)”

在xml文件中

                  <TextView
            android:id="@+id/btnadfree"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/txtfeedback1"
            android:layout_margin="0dp"
            android:background="@drawable/shopping"
            android:gravity="center"
            android:padding="10dp"
            android:textColor="@android:color/white"
            android:autoLink="web"
            android:typeface="sans"/>

在java文件中

txtadfree = (TextView) findViewById(R.id.btnadfree);
      txtadfree.setMovementMethod(LinkMovementMethod.getInstance());
    txtadfree.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW);
            browserIntent.setData(Uri.parse("http://www.google.com"));
            startActivity(browserIntent);
        }
    });
4

0 回答 0