Android 尝试通过图像单击打开网络视图

Android 尝试通过图像单击打开网络视图,android,webview,imageview,android-sdk-2.3,Android,Webview,Imageview,Android Sdk 2.3,我试图在单击图像时打开网络视图 是因为我的图像不在MainActivity.xml上吗 以下是navigationmenheader.xml,其中的图像是: <ImageView android:id="@+id/signIn" android:layout_width="match_parent" android:layout_height="61dp" android:src="@drawable/signin2" android:tex

我试图在单击图像时打开网络视图

是因为我的图像不在MainActivity.xml上吗

以下是navigationmenheader.xml,其中的图像是:

    <ImageView
    android:id="@+id/signIn"
    android:layout_width="match_parent"
    android:layout_height="61dp"
    android:src="@drawable/signin2"
    android:textAlignment="center" />
}

以下是WebViewActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Android SDK没有显示错误,但应用程序在启动时崩溃

编辑: 下面是logcat所说的

原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.ImageView.setOnClickListener(android.view.view$OnClickListener)”


编辑2:添加了所有的主活动代码。

全局声明ImageView,并在onCreate中初始化ImageView并更改签名方法,如下所示

public void signIn() {
img.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
    Intent intent = new Intent(MainActivity.this, WebViewActivity.class);
    startActivity(intent);
}
});

}是因为我的图像不在MainActivity.xml中吗? ,您的图像位于navigationmenheader.xml版式中,在主活动中,您将版式设置为“setContentView(R.layout.Activity_Main);”,但在Activity_Main版式中,图像视图不在那里,这就是它运行不平稳的原因。 使用此工具,其工作平稳 //这是你的主要布局

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/signIn"
        android:layout_width="match_parent"
        android:layout_height="61dp"
        android:src="@drawable/tv"
        android:textAlignment="center" />

</LinearLayout>
//现在,您的webview布局

    <?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

有关异常、stacktrace和崩溃原因,请参阅logcat。您可以在上面发布您的logcat.write
@Override
的可能副本
public void onCreate(
show logcat for the exceptionAlso崩溃,但给出了此错误。原因:java.lang.NullPointerException:尝试对空对象调用虚拟方法“android.view.Window$Callback android.view.Window.getCallBack()”reference@Wangalang将主版面中的图像名称更改为android:src=“@drawable/tv”到您的图像android:src=“@drawable/signin2”很抱歉,我没有时间尝试,我今天晚些时候会尝试。感谢您的帮助,它不再崩溃,但当我单击按钮时,网络视图无法打开
    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/signIn"
        android:layout_width="match_parent"
        android:layout_height="61dp"
        android:src="@drawable/tv"
        android:textAlignment="center" />

</LinearLayout>
    import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

/**
 * Created by Mohammad Arman on 6/14/2017.
 */
public class MyMainActivity extends Activity {
    ImageView img;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mymainactivity);
        img = (ImageView) findViewById(R.id.signIn);
        signIn();

    }
    public void signIn() {
        img.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(MyMainActivity.this, WebViewActivity.class);
                startActivity(intent);
            }
        });
    }
}
    <?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />
    import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

/**
 * Created by Mohammad Arman on 6/14/2017.
 */

    public class WebViewActivity extends Activity {

        private WebView webView;

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.webview);

            webView = (WebView) findViewById(R.id.webView1);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.loadUrl("http://www.google.com");
        }
    }