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.