I m a beginner to android development. I am trying to create an app that is able to scan QR code
. At first I have done everything on the MainActivity
and everything works fine but now I need to make a class called QRScanner
and then to call on the MainActivity
when the user clicks the button to scan something.
I tried many of the solutions that I found but the app always crash when I click the scan
Button
.
This is what I have done.
The MainActivity
class :
public class MainActivity extends Activity implements View.OnClickListener {
public Button Scanner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Scanner =(Button)findViewById(R.id.buttonScanQR);
Scanner.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent QRScanner = new Intent(this, QRScanner .class);
startActivity(QRScanner );
QRScanner QRS =new QRScanner ();
QRS.scanQR(v);
}
}
I also tried to implement this method:
private void startActivityScanner(){
startActivity( new Intent("pathHere.QRScanner " ));
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.buttonScanQR:
startActivityScanner();
QRScanner QRScanner1 =new QRScanner ();
QRScanner1.scanQR(v);
break;
}
}
And this is the QRScanner class :
public class QRScanner extends Activity {
/* QR Code Stuff taken from http://examples.javacodegeeks.com/android/android-barcode-and-qr-scanner-example/ */
static final String ACTION_SCAN = "com.google.zxing.client.android.SCAN";
public void scanQR(View v) {
try {
Intent intent = new Intent(ACTION_SCAN);
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
} catch (ActivityNotFoundException anfe) {
showDialog(QRScanner.this, "No Scanner Found", "Download a scanner code activity?", "Yes", "No").show();
}
}
private static AlertDialog showDialog(final Activity act, CharSequence title, CharSequence message, CharSequence buttonYes, CharSequence buttonNo) {
AlertDialog.Builder downloadDialog = new AlertDialog.Builder(act);
downloadDialog.setTitle(title);
downloadDialog.setMessage(message);
downloadDialog.setPositiveButton(buttonYes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
Uri uri = Uri.parse("market://search?q=pname:" + "com.google.zxing.client.android");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
try {
act.startActivity(intent);
} catch (ActivityNotFoundException anfe) {
}
}
});
downloadDialog.setNegativeButton(buttonNo, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
}
});
return downloadDialog.show();
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
Toast toast = Toast.makeText(this, "Content:" + contents + " Format:" + format, Toast.LENGTH_LONG);
toast.show();
}
}
}
}
I would really appreciate an explanation of what I may be missing.
This is the new log(the crash error)
06-02 16:12:15.005 2380-2395/sn.t.app W/EGL_emulation﹕ eglSurfaceAttrib not implemented
06-02 16:12:15.005 2380-2395/sn.t.app W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xb3f1b920, error=EGL_SUCCESS
06-02 16:12:18.825 2380-2380/sn.t.app D/AndroidRuntime﹕ Shutting down VM
--------- beginning of crash
06-02 16:12:18.826 2380-2380/sn.t.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: sn.t.app, PID: 2380
java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
at android.app.Activity.startActivityForResult(Activity.java:3745)
at android.app.Activity.startActivityForResult(Activity.java:3706)
at snet.tuberlin.app.QRCode.scanQR(QRCode.java:22)
at snet.tuberlin.app.MainActivity.onClick(MainActivity.java:49)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-02 16:12:23.556 2380-2387/sn.t.app W/art﹕ Suspending all threads took: 13.760ms
06-02 16:12:59.072 2380-2387/sn.t.app W/art﹕ Suspending all threads took: 8.775ms
See below code snippets if it helps
// MainActivity
@Override
public void onClick(View v) {
Intent QRScanner = new Intent(this, QRScanner.class);
startActivity(QRScanner);
// NOT required, as you should never create instance of your Activity with 'new'
//QRScanner QRS =new QRScanner();
//QRS.scanQR(v);
}
// QRScanner
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_new_layout);
// in your_new_layout should have a "Button" with id "buttonScanQR"
scanner =(Button)findViewById(R.id.buttonScanQR);
scanner.setOnClickListener(this); // this will basically invoke your onClick(View v)
// If you want to hold on to your method signature "scanQR(View v)"
/*
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
scanQR(v);
}
});
*/
}
public void onClick(View v) {
try {
Intent intent = new Intent(ACTION_SCAN);
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
} catch (ActivityNotFoundException anfe) {
showDialog(QRScanner.this, "No Scanner Found", "Download a scanner code activity?", "Yes", "No").show();
}
}