当用户点击按钮时,我试图从MainActivity类中的一个名为QrScann的类中执行一个方法,但每次单击按钮时应用程序崩溃,它就会给我这个日志崩溃输出
06-02 17:00:24.890 7067-7084/t.n.app W/EGL_emulation﹕ eglSurfaceAttrib not implemented
06-02 17:00:24.890 7067-7084/t.n.app W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xb3ee6da0, error=EGL_SUCCESS
06-02 17:00:27.473 7067-7067/t.n.app D/AndroidRuntime﹕ Shutting down VM
06-02 17:00:27.474 7067-7067/t.n.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: snet.tuberlin.app, PID: 7067
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:23)
at snet.tuberlin.app.MainActivity.onClick(MainActivity.java:48)
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)
这是具有我想要执行的方法(扫描程序)的类:
public class QrScann extends Activity {
static final String ACTION_SCAN = "com.google.zxing.client.android.SCAN";
public void Scanner(View v) {
try {
Intent intent = new Intent(ACTION_SCAN);
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
} catch (ActivityNotFoundException anfe) {
showDialog(QrScann.this, "No Scanner Found", "Download a scanner code activity?", "Yes", "No").show();
}
}
该课程还有其他方法。 这就是我在MainActivity类中所做的:
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 QrScann = new Intent(this, QrScann.class);
//startActivity(QrScann );
QrScann QRScanner =new QrScann();
QRScanner.Scanner(v);
}
}
我真的很感激我可能缺少的解释。
答案 0 :(得分:0)
您不创建“new
”活动,通过提供类来启动它们Intents
,然后通过在启动Intent中提供额外内容和操作来处理生命周期方法中的操作
因此,基本上,QrScann QRScanner = new QRScann();
是尝试创建系统组件的错误方法。您应该使用Intent intent = new Intent(MainActivity.this, QrScann.class);
并在意图中提供操作/附加功能。
答案 1 :(得分:0)
问题是您的QrScann
课程不是有效Activity
。您不能简单地使用Activity
关键字实例化新的new
类 - 您需要允许框架创建活动,并通过startActivity()
上的QrScann
启动活动生命周期。有效的上下文。
看起来您startActivityForResult()
延长了活动的唯一目的是能够致电QrScann
。相反,MainActivity
应该引用其startActivityForResult()
并在其上调用MainActivity
,或者回拨View v=inflater.inflate(R.layout.fragment_b,container,false);
View v = findViewById(R.id.fragB);
。
答案 2 :(得分:0)
public class QrScann {
static final String ACTION_SCAN = "com.google.zxing.client.android.SCAN";
private MainActivity mContext;
public QrScann(MainActivity context) {
mContext = context;
}
public void Scanner(View v) {
try {
Intent intent = new Intent(ACTION_SCAN);
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
mContext.startActivityForResult(intent, 0);
} catch (ActivityNotFoundException anfe) {
//show dialog is deprecated , try DialogFragments
mContext.showDialog(QrScann.this, "No Scanner Found", "Download a scanner code activity?", "Yes", "No").show();
}
}
来自活动
@Override
public void onClick(View v) {
QrScann QRScanner =new QrScann(this);
QRScanner.Scanner(v);
}
答案 3 :(得分:0)
您不能仅像Activity
new QrScann()
那样实例,您必须这样做:
Intent intent = new Intent(this, QrScann.class);
startActivity(intent);