简体   繁体   English

Android 聊天应用程序在我运行时不断崩溃

[英]Android chat app keeps on crashing when I run it

I have just begun writing some code for my Chat app after days of planning but the problem is that keeps on crashing right after the Gradle Build has finished and the app is installed on my device.经过几天的计划,我刚刚开始为我的聊天应用程序编写一些代码,但问题是在 Gradle 构建完成并且应用程序安装在我的设备上之后,它一直在崩溃。 I have created a button that is expected to open a new activity but instead of doing so it crashes.我创建了一个按钮,预计会打开一个新活动,但它没有这样做,而是崩溃了。 Everything works before I have written any code, the app opens and logcat doesn't show any errors.在我编写任何代码之前一切正常,应用程序打开并且 logcat 没有显示任何错误。 Here is the error:这是错误:

    2022-08-10 19:26:48.372 15348-15348/? E/e.myapplicatio: Unknown bits set in runtime_flags: 0x40000000
2022-08-10 19:26:48.391 15348-15348/? E/RefClass: java.lang.reflect.InvocationTargetException
2022-08-10 19:26:48.474 15348-15348/com.example.myapplication E/Perf:  perftest packageName : com.example.myapplication App is allowed to use Hide APIs  
2022-08-10 19:26:48.515 15348-15374/com.example.myapplication E/libEGL: Invalid file path for libcolorx-loader.so
2022-08-10 19:26:48.528 15348-15348/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 15348
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3628)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3887)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:100)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2317)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:263)
        at android.app.ActivityThread.main(ActivityThread.java:8292)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1006)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
        at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:183)
        at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:174)
        at android.content.Context.obtainStyledAttributes(Context.java:744)
        at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:848)
        at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:815)
        at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:640)
        at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:259)
        at com.example.myapplication.MainActivity.<init>(MainActivity.java:12)
        at java.lang.Class.newInstance(Native Method)
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1254)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3616)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3887) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:140) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:100) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2317) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:263) 
        at android.app.ActivityThread.main(ActivityThread.java:8292) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1006) 

I tried following some other suggestions but they didn't work.我尝试遵循其他一些建议,但没有奏效。 Here's the snippet of my code:这是我的代码片段:

package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

public Button signUp = findViewById(R.id.signUpBtn);

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

    signUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            openActivity();
        }
    });
}

public void openActivity(){
    Intent intent = new Intent(this, LoginActivity.class);
    startActivity(intent);
}

} }

You can not use findViewById() outside of onCreate() like this and if you really wanna do that, do it by creating a new method for setting up IDs and then add that method in onCreate() .你不能像这样在onCreate() findViewById() ,如果你真的想这样做,可以通过创建一个新的方法来设置 ID,然后在onCreate()中添加该方法。

this code will do the work now:这段代码现在就可以工作了:

    package com.example.myapplication;
    import androidx.appcompat.app.AppCompatActivity;
    import android.content.Intent;
    import android.view.View;
    import android.widget.Button;
    import android.os.Bundle;
    
    public class MainActivity extends AppCompatActivity {
    
    public Button signUp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        signUp = findViewById(R.id.signUpBtn);
        signUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openActivity();
            }
        });
    }
    
    public void openActivity(){
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的聊天应用程序在 android 工作室运行时不断崩溃,但没有构建错误 - My chat app keeps crashing, on android studio when i run it but there no build errors 当我尝试运行它时,我的应用程序不断崩溃(新的) - My app keeps crashing when i try to run it (New to this) android | 当我向其中添加 admob 横幅广告时,应用程序一直崩溃 - android | App keeps crashing when I add admob banner ad into it 当我打开它时,我的应用程序一直崩溃 - My app keeps crashing when i open it 我的 Webview 应用程序在使用 Messenger 聊天时不断崩溃 - My Webview App keeps crashing for messenger chat 当我打算从 Fragment 到新 Activity (Android) 时,我的应用程序不断崩溃 - My app keeps crashing when I intent from a Fragment to a new Activity (Android) 我的应用程序在模拟器或Android手机上打开时不断崩溃 - My app keeps crashing when opening on the emulator or android phone 尝试从文件读取数据时,Android应用不断崩溃 - Android app keeps crashing when trying to read data from file 使用GoogleMap V2时,Android App不断崩溃 - Android App keeps crashing when using GoogleMap V2 Android Calculator App运行时立即崩溃 - Android Calculator App Crashing immediately when run
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM