【发布时间】:2017-08-13 13:40:18
【问题描述】:
我是 Android 开发新手,所以请多多包涵。 我在实现 qr 扫描的活动中有代码。基本上在这段代码之前有一个活动,但它只是点击一个按钮到这里。每当我这样做时,我的应用程序就会崩溃,并且 that 是返回的错误。该应用程序的想法是一旦用户单击按钮,它将首先显示一个二维码扫描仪,一旦它被扫描,它现在将调用这个 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();
}
}
请帮帮我。
【问题讨论】:
-
你的按钮 id 是 'userpoints` 并且你正在使用
R.id.gosagot这将如何工作? -
您正在使用此
setContentView(mScannerView);将新布局附加到您的活动中,因此旧布局不再附加到您的活动中 -
@rafsanahmad007 不。它的 id 是 gosgot。用户点用于 layout_below
-
@Pavneet_Singh 我要删除它吗?还是改成布局文件?
-
一般来说,除非您确切地知道自己为什么这样做,否则不要多次使用
setContentView。 (在这些情况下,您需要 Fragments)
标签: java android android-activity nullpointerexception qr-code