简体   繁体   中英

App crashing when i try to change activity Intent

I've been trying to fix this for weeks but failed, when I click on login (indicated with the id "lin") to open a new activity the app crash, i don't know if it's a problem with the Intent or something else, here is the code. The manifest should be ok so I think it's a problem in the MainActivity with Intent ab. The other activity is called Qrcode. I tried to change appcompatactivty to activity but didn't work, i don't really know what to do.

Edit: I posted the code of the qrcode activity, i got it from the answers of this question: Android, How to read QR code in my application? , only for educational purpose of course.

Edit 2: logcat posted, sorry for any issues with asking this question, it's the first question i ask here.

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

MainActivity


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

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

Manifest

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

Qrcode activity:

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

ur code in MainActivity seems ok and I think don't have any problem. In my opinion your Qrcode Activity has some bugs in it, like onCreate method, you should see the Logcat logs in android Studio, btw u can attach the Qrcode activity codes here, it is really helpful. another way to find the bug is by using the try-catch in your code and log the exception

Here's the error

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)

What does it mean? You are trying to setOnClickListener on a null object reference.

Check the next line in the log

 at com.example.h24distributori.Qrcode.onCreate(Qrcode.java:59)

"QrCode.java:59" This means that the error is in QrCode activity on line 59.

You are setting a onclicklistener to 'button' on line 59. This means that 'button' variable is null.

But I can see that you have initialized the button variable

 button = (Button)findViewById(R.id.button);

So why is it still null? There is no button with id R.id.button, so findViewById() returns null. Check the QrCode activity layout xml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM