尝试打开ZXingScannerView时出现NullPointerException

时间:2017-03-21 17:56:38

标签: java android android-activity nullpointerexception qr-code

我是Android开发的新手,所以请耐心等待。 我在一个实现qr扫描的活动中有代码。基本上在这段代码之前有一个活动,但它只是点击一个按钮就可以到这里。每当我这样做时,我的应用程序崩溃, 是返回的错误。应用程序的想法是,一旦用户点击按钮,它将首先显示一个qr扫描仪,一旦扫描完毕,它现在将调用此 XML 文件

library(huxtable)
ht <- hux(c('Table Header', 'Table Cell', 'Cell 3'), c('Second Header', 'Cell 2' , 'Cell 4'))
top_border(ht) <- 1
bold(ht)[1,] <- TRUE
right_border(ht)[,1] <- TRUE

这是java文件

<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>

...继续

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();
                        }
                    });
                }
            });
        }
    });
}

请帮帮我。

2 个答案:

答案 0 :(得分:1)

问题是该按钮是activity_scan的一部分,您将prepare for segue设置为其他布局。您必然会收到此错误,因为该活动没有引用您正在引用的视图(按钮)

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

获取它

答案 1 :(得分:1)

如果你check the examples of ZXing ......

在您的活动XML中,您可以为QR扫描程序添加区域。

""

抓住并在那里添加扫描仪视图。

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

您的错误是,当您将内容视图替换为其他没有您要查找的ID的视图时,您可以在按钮上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); }

即使你&#34;切换&#34; findViewByIdfindViewById周围,您的代码不会崩溃,但您在内容视图中不再有按钮,因此单击操作将毫无意义。

相关问题