【问题标题】:NullPointerException when trying to open ZXingScannerView尝试打开 ZXingScannerView 时出现 NullPointerException
【发布时间】: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


【解决方案1】:

如果你check the examples of ZXing...

在您的活动 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

即使您“切换”findViewByIdsetContentView,您的代码也不会崩溃,但内容视图中将不再有按钮,因此单击操作将毫无意义。

【讨论】:

  • 我成功了。谢谢!但我有一些问题。可以通过扫描仪看到 Go 按钮,也许您有什么建议可以暂时隐藏它?
  • 或许button.setVisibility(View.GONE)?
  • 不明白你的意思,抱歉。随意创建一个新帖子来解释您的问题。
【解决方案2】:

问题在于该按钮是activity_scan的一部分,并且您将setContentView(mScannerView)设置为其他布局。您一定会收到此错误,因为活动没有对您所引用的视图(按钮)的引用。

您可以做的是让 mScannerView 成为您的主要布局的一部分,然后使用 findViewById(); 获取它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-26
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多