我的QRcode扫描仪功能不起作用

时间:2019-08-02 06:19:19

标签: android-studio qr-code

我正在QRcode中设置TrackActivity扫描仪功能。代码看起来不错,但是当我运行应用程序并单击“扫描”按钮时,什么也没发生。

我在一个新项目中尝试了QRcode扫描仪功能,并且工作正常。  这是我的TrackActivity.class

package com.example.majugoscreen;

 import android.content.Intent;
 import android.os.Bundle;
 import android.support.design.widget.FloatingActionButton;
 import android.support.design.widget.Snackbar;
 import android.support.v7.app.AppCompatActivity;
 import android.support.v7.widget.Toolbar;
 import android.view.View;
 import android.widget.Button;
 import android.widget.TextView;
 import android.widget.Toast;

 public class TrackActivity extends AppCompatActivity {

  public static TextView resultTExtview;
  Button scan_btn;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_track);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", 
            Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();


            resultTExtview = (TextView) findViewById(R.id.result_text);
            scan_btn = (Button) findViewById(R.id.btn_scan);
            scan_btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    startActivity(new 
      Intent(getApplicationContext(),ScanCodeActivity.class));
                }
            });
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

            Bundle bundle = getIntent().getExtras();
            if(bundle != null){
                if(bundle.getString("Track") !=null){
                    Toast.makeText(getApplicationContext(),
                            "Activity" + bundle.getString("Track"),
                            Toast.LENGTH_SHORT).show();
                }
            }
        }

    });
}

}

这是我的ScanCodeActivity.class

  package com.example.majugoscreen;

  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;

  import com.google.zxing.Result;

  import me.dm7.barcodescanner.zxing.ZXingScannerView;

  class ScanCodeActivity extends AppCompatActivity implements 
  ZXingScannerView.ResultHandler{

   ZXingScannerView ScannerView;
   @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ScannerView = new ZXingScannerView(this);
    setContentView(ScannerView);
    }

    @Override
    public void handleResult(Result result) {

    TrackActivity.resultTExtview.setText(result.getText());
    onBackPressed();

     }

     @Override
      protected void onPause(){
      super.onPause();

      ScannerView.stopCamera();
      }

     @Override
     protected void onResume(){
     super.onResume();

     ScannerView.setResultHandler(this);
     ScannerView.startCamera();
      }
     }

这是我的activity_track.xml

   <?xml version="1.0" encoding="utf-8"?>
   <android.support.design.widget.CoordinatorLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:gravity='center'
   tools:context=".TrackActivity">

   <android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

     </android.support.design.widget.AppBarLayout>

     <include layout="@layout/content_track" />

     <android.support.design.widget.FloatingActionButton
       android:id="@+id/fab"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_gravity="bottom|end"
       android:layout_margin="@dimen/fab_margin"
       app:srcCompat="@android:drawable/ic_dialog_email" />


     <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Scan QR and Code "
       android:layout_gravity="center_horizontal"
       android:layout_marginTop="290dp"
       android:textSize="16sp"
       android:id="@+id/btn_scan"
       />

     <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Result"
        android:gravity="center"
        android:textSize="22sp"
        android:layout_marginTop="350dp"
        android:id="@+id/result_text"

        />
      </android.support.design.widget.CoordinatorLayout>

0 个答案:

没有答案