尝试打开 ZXingScannerView 时出现 NullPointerException
NullPointerException when trying to open ZXingScannerView
我是 Android 开发的新手,所以请多多包涵。
我在 activity 中有实现二维码扫描的代码。基本上在这段代码之前有一个 activity ,但只需单击一个按钮即可转到此处。每当我这样做时,我的应用程序就会崩溃,并且 that 是 returns 的错误。该应用程序的想法是,一旦用户单击该按钮,它将首先显示一个二维码扫描仪,扫描后,它现在将调用此 XML 文件
<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>
这是 java 文件
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();
}
}
请帮助我。
问题是该按钮是 activity_scan 的一部分并且您将 setContentView(mScannerView)
设置为其他 layout.You 必然会出现此错误,因为 activity 不会引用您所引用的视图(按钮)。
您可以做的是让 mScannerView 成为主布局的一部分,然后使用 findViewById();
获取它
如果你check the examples of ZXing...
在您的 activity XML 中,您可以为 QR 扫描器添加一个区域。
<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);
}
您的错误是,当您将内容视图替换为其他没有您要查找的 ID 的视图时,您无法在按钮上 findViewById
。
即使您 "switched" findViewById
和 setContentView
左右,您的代码也不会崩溃,但内容视图中将不再有按钮,因此单击操作将毫无意义。
我是 Android 开发的新手,所以请多多包涵。 我在 activity 中有实现二维码扫描的代码。基本上在这段代码之前有一个 activity ,但只需单击一个按钮即可转到此处。每当我这样做时,我的应用程序就会崩溃,并且 that 是 returns 的错误。该应用程序的想法是,一旦用户单击该按钮,它将首先显示一个二维码扫描仪,扫描后,它现在将调用此 XML 文件
<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>
这是 java 文件
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();
}
}
请帮助我。
问题是该按钮是 activity_scan 的一部分并且您将 setContentView(mScannerView)
设置为其他 layout.You 必然会出现此错误,因为 activity 不会引用您所引用的视图(按钮)。
您可以做的是让 mScannerView 成为主布局的一部分,然后使用 findViewById();
如果你check the examples of ZXing...
在您的 activity XML 中,您可以为 QR 扫描器添加一个区域。
<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); }
您的错误是,当您将内容视图替换为其他没有您要查找的 ID 的视图时,您无法在按钮上 findViewById
。
即使您 "switched" findViewById
和 setContentView
左右,您的代码也不会崩溃,但内容视图中将不再有按钮,因此单击操作将毫无意义。