0

Im trying to open a WebView when an image is clicked.

Is it becase my image is not on the MainActivity.xml?

Here is the navigationmenheader.xml where the image is:

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

Here is the part of the code on my MainActivity.java

public class MainActivity extends AppCompatActivity {


//Drawer variables
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;

//Sign in variables
ImageView img;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    img = (ImageView) findViewById(R.id.signIn);

    // Drawer
    mDrawerLayout=(DrawerLayout) findViewById(R.id.drawerLayout);
    mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
    mDrawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    // O button
    FloatingActionButton add = (FloatingActionButton) findViewById(R.id.floatingActionButtonADD);
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){
            startActivity(new Intent(MainActivity.this,Pop.class));
        }
    }
    );

    signIn();
}


// Drawer 3 lines button
@Override
public boolean onOptionsItemSelected(MenuItem item){
    if(mToggle.onOptionsItemSelected(item)){
        return true;
    }
    return super.onOptionsItemSelected(item);
}

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

}

Here is the 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" />

And here is my WebViewActivity.class

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");
}
}

Android SDK shows no error, but the app crashes on startup

Edit: Here is what the logcat says.

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference.

Edit 2: Added all the mainactivity code.

2 Answers2

0

Declare ImageView globally and in onCreate initialize ImageView and change signIn method like this.

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

}

Dhanumjay
  • 540
  • 7
  • 17
  • Also crashes but gives me this error. Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallBack()' on a null object reference –  Jun 14 '17 at 09:53
0

Is it because my image is not in the MainActivity.xml? Yes ,your image is in navigationmenheader.xml layout and in Main Activity you are setting layout as "setContentView(R.layout.activity_main);"but in activity_main layout, image view is not there, thats why its not running smoothly. Its Working Smoothly use this //Here is Your Main Layout

    <?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>

//and Here is Your mymainactivity Layout

    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);
            }
        });
    }
}

//Now Your webview layout

    <?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"
    />

and Your WebViewActivity

    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");
        }
    }
Mohammad Arman
  • 508
  • 4
  • 13
  • @Wangalang change your image name in Main Layout as android:src="@drawable/tv" to your image android:src="@drawable/signin2" – Mohammad Arman Jun 14 '17 at 10:52
  • sorry i had no time to try it, ill try it later today. –  Jun 14 '17 at 14:13
  • Thank you for your help, It does not crash anymore, but when I click the button, the webview does not open –  Jun 14 '17 at 15:55