我创建的简单apk在打开时崩溃(可能是我的Java代码中的错误)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我创建的简单apk在打开时崩溃(可能是我的Java代码中的错误)相关的知识,希望对你有一定的参考价值。
我创建的简单APK在尝试启动时一直崩溃,但我不知道为什么。这是问题可能来自的代码(这是我在APK开始崩溃之前修改的最后一个代码):
package com.example.firstapk;
import androidx.appcompat.app.AppCompatActivity;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.google.android.material.snackbar.Snackbar;
public class MainActivity extends AppCompatActivity
private static Button button_test;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onButtonClickListener(fact_a);
String fact_a = getString(R.string.Fact1);
public void onButtonClickListener(final String fact)
button_test = (Button)findViewById(R.id.button2);
button_test.setOnClickListener(
new View.OnClickListener()
@Override
public void onClick(View v)
AlertDialog.Builder builder_test = new AlertDialog.Builder(MainActivity.this);
builder_test.setMessage(fact).setCancelable(true);
AlertDialog alert = builder_test.create();
alert.show();
);
提前感谢
答案
2020-05-03 16:59:55.786 13220-13220/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2020-05-03 16:59:55.787 13220-13220/? E/Zygote: accessInfo : 1
2020-05-03 16:59:56.230 13220-13220/com.example.firstapk E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.firstapk, PID: 13220
java.lang.RuntimeException: Unable to instantiate activity ComponentInfocom.example.firstapk/com.example.firstapk.MainActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3020)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3257)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7050)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:91)
at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:127)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:121)
at androidx.appcompat.app.AppCompatActivity.getResources(AppCompatActivity.java:543)
at android.content.Context.getString(Context.java:580)
at com.example.firstapk.MainActivity.<init>(MainActivity.java:24)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:43)
at android.app.Instrumentation.newActivity(Instrumentation.java:1215)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3008)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3257)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7050)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
另一答案
public class MainActivity extends AppCompatActivity
private static Button button_test;
String fact_a;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fact_a = getString(R.string.Fact1);
onButtonClickListener(fact_a);
public void onButtonClickListener(final String fact)
button_test = (Button)findViewById(R.id.button2);
button_test.setOnClickListener(
new View.OnClickListener()
@Override
public void onClick(View v)
AlertDialog.Builder builder_test = new AlertDialog.Builder(MainActivity.this);
builder_test.setMessage(fact).setCancelable(true);
AlertDialog alert = builder_test.create();
alert.show();
);
以上是关于我创建的简单apk在打开时崩溃(可能是我的Java代码中的错误)的主要内容,如果未能解决你的问题,请参考以下文章