当我尝试更改活动意图时应用程序崩溃

时间:2021-06-03 17:26:45

标签: java android xml android-studio

我数周以来一直试图解决这个问题但失败了,当我点击登录(用 ID“lin”表示)打开一个新活动时,应用程序崩溃了,我不知道这是不是有问题意图或其他什么,这里是代码。清单应该没问题,所以我认为这是带有 Intent ab 的 MainActivity 中的问题。另一个活动称为 Qrcode。我试图将 appcompatactivty 更改为活动但没有用,我真的不知道该怎么做。

编辑:我发布了 qrcode 活动的代码,我是从这个问题的答案中得到的:Android, How to read QR code in my application?,当然仅用于教育目的。

编辑 2:logcat 已发布,对于提出此问题的任何问题,抱歉,这是我在这里问的第一个问题。

日志猫

2021-06-04 10:08:06.177 27399-27399/? I/h24distributor: Late-enabling -Xcheck:jni
2021-06-04 10:08:06.197 27399-27399/? I/h24distributor: Unquickening 21 vdex files!
2021-06-04 10:08:06.204 27399-27399/? E/h24distributor: Unknown bits set in runtime_flags: 0x800000
2021-06-04 10:08:06.343 27399-27399/? I/Perf: Connecting to perf service.
2021-06-04 10:08:06.360 27399-27399/? D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2021-06-04 10:08:06.360 27399-27399/com.example.h24distributori D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2021-06-04 10:08:06.386 27399-27399/com.example.h24distributori E/Config:Grabber:OnePlusFontConfig: Index 0 requested, with a size of 0
2021-06-04 10:08:06.417 27399-27399/com.example.h24distributori E/h24distributor: [frame_perf] perfboost open tb_ctl file failed, isApp[1], errno=No such file or directory
2021-06-04 10:08:06.418 27399-27399/com.example.h24distributori E/libprocessgroup: set_timerslack_ns write failed: Operation not permitted
2021-06-04 10:08:06.465 27399-27399/com.example.h24distributori E/h24distributor: Invalid ID 0x00000000.
2021-06-04 10:08:06.465 27399-27399/com.example.h24distributori E/h24distributor: Invalid ID 0x00000000.
2021-06-04 10:08:06.495 27399-27399/com.example.h24distributori E/h24distributor: Invalid ID 0x00000000.
2021-06-04 10:08:06.497 27399-27399/com.example.h24distributori I/chatty: uid=10692(com.example.h24distributori) identical 1 line
2021-06-04 10:08:06.497 27399-27399/com.example.h24distributori E/h24distributor: Invalid ID 0x00000000.
2021-06-04 10:08:06.506 27399-27399/com.example.h24distributori E/h24distributor: Invalid ID 0x00000000.
2021-06-04 10:08:06.506 27399-27399/com.example.h24distributori I/chatty: uid=10692(com.example.h24distributori) identical 1 line
2021-06-04 10:08:06.507 27399-27399/com.example.h24distributori E/h24distributor: Invalid ID 0x00000000.
2021-06-04 10:08:06.541 27399-27399/com.example.h24distributori I/OPFD_Manager: Dark? false, OP Force ? false
2021-06-04 10:08:06.542 27399-27399/com.example.h24distributori V/ViewRootImpl: The specified message queue synchronization  barrier token has not been posted or has already been removed
2021-06-04 10:08:06.608 27399-27399/com.example.h24distributori D/ViewRootImpl: support adaptive color gamut feature!
2021-06-04 10:08:06.646 27399-9660/com.example.h24distributori I/AdrenoGLES-0: QUALCOMM build                   : 559e78c, Ib9d997268a
    Build Date                       : 10/13/20
    OpenGL ES Shader Compiler Version: EV031.32.02.02
    Local Branch                     : mybrancheb0d76cb-f27a-f1ff-9cdc-1458d0b52ae8
    Remote Branch                    : quic/gfx-adreno.lnx.1.0.r99-rel
    Remote Branch                    : NONE
    Reconstruct Branch               : NOTHING
2021-06-04 10:08:06.646 27399-9660/com.example.h24distributori I/AdrenoGLES-0: Build Config                     : S P 10.0.7 AArch64
2021-06-04 10:08:06.646 27399-9660/com.example.h24distributori I/AdrenoGLES-0: Driver Path                      : /vendor/lib64/egl/libGLESv2_adreno.so
2021-06-04 10:08:06.654 27399-9660/com.example.h24distributori I/AdrenoGLES-0: PFP: 0x016ee190, ME: 0x00000000
2021-06-04 10:08:06.740 27399-27399/com.example.h24distributori D/DecorView: onWindowFocusChangedFromViewRoot hasFocus: true, DecorView@a86767f[MainActivity]
2021-06-04 10:08:06.743 27399-27399/com.example.h24distributori D/ViewRootImpl[MainActivity]: windowFocusChanged hasFocus=true inTouchMode=true
2021-06-04 10:08:15.552 27399-9660/com.example.h24distributori W/DisplayEventDispatcher: dispatcher 0xb4000077f19b29f0 ~ ignoring unknown event type 0x6d746f6e
2021-06-04 10:08:15.552 27399-27399/com.example.h24distributori W/DisplayEventDispatcher: dispatcher 0xb4000077a19c5930 ~ ignoring unknown event type 0x6d746f6e
2021-06-04 10:08:15.927 27399-27399/com.example.h24distributori D/ViewRootImpl[MainActivity]: windowFocusChanged hasFocus=false inTouchMode=true
2021-06-04 10:08:41.340 27399-27399/com.example.h24distributori D/ActivityThread: pid:27399 tid:27399 doframe Callback
2021-06-04 10:08:41.351 27399-9660/com.example.h24distributori W/DisplayEventDispatcher: dispatcher 0xb4000077f19b29f0 ~ ignoring unknown event type 0x6d746f6e
2021-06-04 10:08:41.351 27399-27399/com.example.h24distributori W/DisplayEventDispatcher: dispatcher 0xb4000077a19c5930 ~ ignoring unknown event type 0x6d746f6e
2021-06-04 10:08:41.530 27399-27399/com.example.h24distributori D/DecorView: onWindowFocusChangedFromViewRoot hasFocus: true, DecorView@a86767f[MainActivity]
2021-06-04 10:08:41.531 27399-27399/com.example.h24distributori D/ViewRootImpl[MainActivity]: windowFocusChanged hasFocus=true inTouchMode=true
2021-06-04 10:08:42.117 27399-27399/com.example.h24distributori D/AndroidRuntime: Shutting down VM
2021-06-04 10:08:42.118 27399-27399/com.example.h24distributori E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.h24distributori, PID: 27399
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.h24distributori/com.example.h24distributori.Qrcode}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3611)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3775)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2246)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:233)
        at android.app.ActivityThread.main(ActivityThread.java:8010)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.example.h24distributori.Qrcode.onCreate(Qrcode.java:59)
        at android.app.Activity.performCreate(Activity.java:8006)
        at android.app.Activity.performCreate(Activity.java:7990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3584)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3775) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2246) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:233) 
        at android.app.ActivityThread.main(ActivityThread.java:8010) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:631) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978) 
2021-06-04 10:08:42.149 27399-27399/com.example.h24distributori I/Process: Sending signal. PID: 27399 SIG: 9

主活动


import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.content.Intent;
import android.graphics.Typeface;
//import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends Activity {

        EditText pswd,usrusr;
        TextView sup,lin;
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            lin = (TextView) findViewById(R.id.lin);
            usrusr = (EditText) findViewById(R.id.usrusr);
            pswd = (EditText) findViewById(R.id.pswrdd);
            sup = (TextView) findViewById(R.id.sup);

            lin.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick (View v)
                {
                    Intent ab = new Intent(MainActivity.this, Qrcode.class);
                    startActivity(ab);
                }
            });
            sup.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick (View v)
                {
                    Intent it = new Intent(MainActivity.this, Signup.class);
                    startActivity(it);
                }
            });
        }
    }

Activity 主布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@drawable/grdnt"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:paddingLeft="60dp"
        android:paddingRight="60dp">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="16dp">
                <ImageView
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:src="@drawable/usrusr"
                    android:layout_gravity="center"/>
                <EditText
                    android:id="@+id/usrusr"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Username"
                    android:textColorHint="#d9e5f3"
                    android:layout_marginLeft="15dp"
                    android:background="#00000000"
                    android:letterSpacing="0.1"
                    android:textSize="16dp"
                    android:inputType="text"
                    android:textColor="#fff"
                    />
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#4a5a71"></LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="16dp">
                <ImageView
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:src="@drawable/pswd"
                    android:layout_gravity="center"/>
                <EditText
                    android:id="@+id/pswrdd"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Password"
                    android:textColorHint="#d9e5f3"
                    android:layout_marginLeft="15dp"
                    android:background="#00000000"
                    android:inputType="textPassword"
                    android:letterSpacing="0.1"
                    android:textSize="16dp"
                    android:textColor="#fff"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#4a5a71"></LinearLayout>
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="60dp"
        android:paddingRight="60dp"
        android:orientation="vertical"
        android:gravity="bottom"
        android:layout_marginBottom="20dp">
        <TextView
            android:id="@+id/lin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#009a9a"
            android:text="Login"
            android:textColor="#d9e5f3"
            android:textStyle="bold"
            android:gravity="center"
            android:layout_gravity="bottom"
            android:padding="16dp"
            android:layout_marginBottom="50dp"
            android:letterSpacing="0.2"/>
        <TextView
            android:id="@+id/sup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Non hai un account? Registrati"
            android:textColor="#d9e5f3"
            android:letterSpacing="0.1"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:layout_marginBottom="400dp">
        <ImageView
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:src="@drawable/logo_large"
            />
    </LinearLayout>

</RelativeLayout>

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.h24distributori">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:usesCleartextTraffic="true">
        <activity android:name=".Qrcode"></activity>
        <activity android:name=".Signup" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

二维码活动:

package com.example.h24distributori;

import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

public class Qrcode extends Activity {

        ImageView imageView;
        Button button;
        Button btnScan;
        EditText editText;
        String EditTextValue ;
        Thread thread ;
        public final static int QRcodeWidth = 350 ;
        Bitmap bitmap ;

        TextView tv_qr_readTxt;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_qrcode);

            imageView = (ImageView)findViewById(R.id.imageView);
            editText = (EditText)findViewById(R.id.editText);
            button = (Button)findViewById(R.id.button);
            btnScan = (Button)findViewById(R.id.btnScan);
            tv_qr_readTxt = (TextView) findViewById(R.id.tv_qr_readTxt);

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(!editText.getText().toString().isEmpty()){
                        EditTextValue = editText.getText().toString();

                        try {
                            bitmap = TextToImageEncode(EditTextValue);

                            imageView.setImageBitmap(bitmap);

                        } catch (WriterException e) {
                            e.printStackTrace();
                        }
                    }
                    else{
                        editText.requestFocus();
                        Toast.makeText(Qrcode.this, "Please Enter Your Scanned Test" , Toast.LENGTH_LONG).show();
                    }

                }
            });


            btnScan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    IntentIntegrator integrator = new IntentIntegrator(Qrcode.this);
                    integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
                    integrator.setPrompt("Scan");
                    integrator.setCameraId(0);
                    integrator.setBeepEnabled(false);
                    integrator.setBarcodeImageEnabled(false);
                    integrator.initiateScan();

                }
            });
        }


        Bitmap TextToImageEncode(String Value) throws WriterException {
            BitMatrix bitMatrix;
            try {
                bitMatrix = new MultiFormatWriter().encode(
                        Value,
                        BarcodeFormat.DATA_MATRIX.QR_CODE,
                        QRcodeWidth, QRcodeWidth, null
                );

            } catch (IllegalArgumentException Illegalargumentexception) {

                return null;
            }
            int bitMatrixWidth = bitMatrix.getWidth();

            int bitMatrixHeight = bitMatrix.getHeight();

            int[] pixels = new int[bitMatrixWidth * bitMatrixHeight];

            for (int y = 0; y < bitMatrixHeight; y++) {
                int offset = y * bitMatrixWidth;

                for (int x = 0; x < bitMatrixWidth; x++) {

                    pixels[offset + x] = bitMatrix.get(x, y) ?
                            getResources().getColor(R.color.QRCodeBlackColor):getResources().getColor(R.color.QRCodeWhiteColor);
                }
            }
            Bitmap bitmap = Bitmap.createBitmap(bitMatrixWidth, bitMatrixHeight, Bitmap.Config.ARGB_4444);

            bitmap.setPixels(pixels, 0, 350, 0, 0, bitMatrixWidth, bitMatrixHeight);
            return bitmap;
        }




        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
            if(result != null) {
                if(result.getContents() == null) {
                    Log.e("Scan*******", "Cancelled scan");

                } else {
                    Log.e("Scan", "Scanned");

                    tv_qr_readTxt.setText(result.getContents());
                    Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
                }
            } else {
                // This is important, otherwise the result will not be passed to the fragment
                super.onActivityResult(requestCode, resultCode, data);
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

您在 MainActivity 中的代码看起来没问题,我认为没有任何问题。 在我看来你的 Qrcode Activity 有一些错误,比如 onCreate 方法, 您应该会在 android Studio 中看到 Logcat 日志,顺便说一句,您可以在此处附加 Qrcode 活动代码,这真的很有帮助。 另一种查找错误的方法是在代码中使用 try-catch 并记录异常

答案 1 :(得分:0)

这里是错误

<块引用>

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' 对空对象引用 在 com.example.h24distributori.Qrcode.onCreate(Qrcode.java:59)

什么意思? 您正在尝试对空对象引用设置 OnClickListener。

检查日志中的下一行

<块引用>
    at com.example.h24distributori.Qrcode.onCreate(Qrcode.java:59)

"QrCode.java:59" 这意味着错误在第 59 行的 QrCode 活动中。

您正在第 59 行将 onclicklistener 设置为 'button'。这意味着 'button' 变量为空。

但是我可以看到你已经初始化了按钮变量

<块引用>
        button = (Button)findViewById(R.id.button);

那为什么还是null呢? 没有 id 为 R.id.button 的按钮,因此 findViewById() 返回 null。检查 QrCode 活动布局 xml

相关问题