Java 尝试打开ZXingScannerView时出现NullPointerException
我是Android开发的新手,所以请容忍我。 我在一个实现qr扫描的活动中有代码。基本上,在这段代码之前有一个活动,但只需点击一个按钮就可以转到这里。每当我这样做,我的应用程序就会崩溃,那就是返回的错误。该应用程序的想法是,一旦用户单击按钮,它将首先显示一个qr扫描仪,一旦扫描完毕,它现在将调用此XML文件Java 尝试打开ZXingScannerView时出现NullPointerException,java,android,android-activity,nullpointerexception,qr-code,Java,Android,Android Activity,Nullpointerexception,Qr Code,我是Android开发的新手,所以请容忍我。 我在一个实现qr扫描的活动中有代码。基本上,在这段代码之前有一个活动,但只需点击一个按钮就可以转到这里。每当我这样做,我的应用程序就会崩溃,那就是返回的错误。该应用程序的想法是,一旦用户单击按钮,它将首先显示一个qr扫描仪,一旦扫描完毕,它现在将调用此XML文件 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_scan"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@drawable/scanbg"
tools:context=".ScanActivity">
<RelativeLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginTop="62dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Question Here"
android:textSize="22dp"
android:id="@+id/questionhere"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/questionhere"
android:hint="Answer"
android:id="@+id/answerhere"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Correct or not indicator"
android:id="@+id/indicator"
android:layout_below="@id/answerhere"
android:textSize="18dp"/>
<!--this textview will be programmed to be changed as correct or not-->
<!--if answer == correct then "correct!" else "wrong answer"-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Point(s):"
android:id="@+id/userpoints"
android:layout_below="@id/indicator"
android:textSize="18dp"/>
<Button
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/userpoints"
android:layout_alignParentEnd="true"
android:text="Go"
android:textSize="14dp"
android:background="#B0DAD5"
android:id="@+id/gosagot"/>
</RelativeLayout>
请帮帮我。问题在于该按钮是“活动扫描”的一部分,您将
设置内容视图(mScannerView)
设置为其他布局。您肯定会遇到此错误,因为该活动没有引用您正在引用的视图(按钮)
您可以做的是将mScannerView作为主布局的一部分,然后使用findviewbyd()获取它代码>如果您
在活动XML中,可以为QR扫描仪添加区域
您的错误是,当您将内容视图替换为不具有要查找的ID的其他视图时,无法在按钮上findViewById
即使您“切换”了findViewById
和setContentView
,您的代码也不会崩溃,但在内容视图中不再有按钮,因此单击操作将毫无意义 您的按钮id是“userpoints”,您正在使用R.id.gosagot
这将如何工作?您正在使用此setContentView(mScannerView)将新布局附加到活动中代码>因此旧布局不再附加到您的activity@rafsanahmad007不。身份证是戈萨戈特。用户点用于布局_below@Pavneet_Singh那么我要删除它吗?或者将其更改为布局文件?通常,除非您确切知道执行此操作的原因,否则切勿多次执行setContentView
。(在这种情况下,你需要的是碎片)我成功了。谢谢但我有一些问题。“Go”按钮可以通过扫描仪看到,也许你有什么建议我可以暂时隐藏它?也许按钮.setVisibility(View.Go)
?不知道你的意思,抱歉。请随意创建一个新帖子来解释您的问题。
public class ScanActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler{
Button validateanswer;
ProgressDialog progress;
private ZXingScannerView mScannerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
validateanswer = (Button)findViewById(R.id.gosagot); //Im having error here
validateanswer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
progress.setMessage("Connecting...");
progress.setCancelable(false);
progress.show();
EditText theanswer = (EditText)findViewById(R.id.answerhere);
String sagotdito = theanswer.getText().toString();
RequestFactory.confirmanswer(ScanActivity.this, sagotdito, new RequestCallback<Boolean>() {
@Override
public void onSuccess(Boolean response) {
runOnUiThread(new Runnable() {
@Override
public void run() {
finish();
//load new question
progress.dismiss();
}
});
}
@Override
public void onFailed(String message) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(ScanActivity.this, "Wrong Answer. Try Again!", Toast.LENGTH_LONG).show();
progress.dismiss();
}
});
}
});
}
});
}
@Override
protected void onPause() {
super.onPause();
mScannerView.stopCamera();
}
@Override
public void handleResult(final Result result) {
Log.w("handleResult",result.getText());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final EditText answertoQuestion = new EditText(this);
answertoQuestion.setHint("answer");
builder.setTitle("Scan Result");
builder.setMessage(result.getText());
builder.setPositiveButton("Go", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
setContentView(R.layout.activity_scan);
TextView j = (TextView)findViewById(R.id.questionhere);
EditText k = (EditText)findViewById(R.id.answerhere);
String n = k.getText().toString();
j.setText(result.getText());
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
public class ScannerActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.activity_simple_scanner);
ViewGroup contentFrame = (ViewGroup) findViewById(R.id.content_frame);
mScannerView = new ZXingScannerView(this);
contentFrame.addView(mScannerView);
}