I am using the same onClick method for two buttons to launch an activity, one of them to go to my Settings page and the other to go a Scanner page. The settings button is working fine but the scanner one is not. I just can't seem to find the problem. I added the activity already in my manifest. Still could not work.
MainPage.java
package mapp.com.sg.receiptscanner;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainPage extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
Button ScannerBtn = (Button)findViewById(R.id.ScannerBtn);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.SettingsBtn:
Intent i = new Intent(this, Settings.class);
startActivity(i);
break;
case R.id.ScannerBtn:
Intent x = new Intent(this, Scanner.class);
startActivity(x);
break;
}
}
}
Scanner.java
package mapp.com.sg.receiptscanner;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class Scanner extends AppCompatActivity {
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanner);
Button btnCamera = (Button)findViewById(R.id.btnCamera);
imageView = (ImageView)findViewById(R.id.imageView);
btnCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,0);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(bitmap);
}
}
Logcat
08-13 12:11:47.996 2785-2785/mapp.com.sg.receiptscanner E/AndroidRuntime: FATAL EXCEPTION: main
Process: mapp.com.sg.receiptscanner, PID: 2785
java.lang.RuntimeException: Unable to start activity ComponentInfo{mapp.com.sg.receiptscanner/mapp.com.sg.receiptscanner.Scanner}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at mapp.com.sg.receiptscanner.Scanner.onCreate(Scanner.java:25)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
activity_scanner.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context="mapp.com.sg.receiptscanner.Scanner">
<ImageView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="9"
android:id="@+id/imageView"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/holo_blue_light"
android:textColor="@android:color/white"
android:text="Open Camera"/>
activity_main_page.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="mapp.com.sg.receiptscanner.MainPage">
<Button
android:id="@+id/ScannerBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:text="Scanner"
android:onClick="onClick"
tools:layout_editor_absoluteX="130dp"
tools:layout_editor_absoluteY="47dp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Receipts"
tools:layout_editor_absoluteX="148dp"
tools:layout_editor_absoluteY="140dp"
android:layout_below="@+id/ScannerBtn"
android:layout_alignStart="@+id/ScannerBtn"
android:layout_marginTop="47dp" />
<Button
android:id="@+id/SettingsBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Settings"
android:onClick="onClick"
tools:layout_editor_absoluteX="148dp"
tools:layout_editor_absoluteY="231dp"
android:layout_centerVertical="true"
android:layout_alignStart="@+id/button2" />