Android Studio:java.lang.RuntimeException:无法启动 activity ComponentInfo
Android Studio: java.lang.RuntimeException: Unable to start activity ComponentInfo
我正在尝试为 Android Studio 使用 AnyChart 模块,但它一直给我错误 java.lang.RuntimeException: Unable to start activity ComponentInfo
。
以下是一些值得注意的文件:
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.anychart.AnyChart;
import com.anychart.AnyChartView;
import com.anychart.chart.common.dataentry.DataEntry;
import com.anychart.chart.common.dataentry.ValueDataEntry;
import com.anychart.charts.Pie;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Pie pie = AnyChart.pie();
//
// List<DataEntry> data = new ArrayList<>();
// data.add(new ValueDataEntry("John", 10000));
// data.add(new ValueDataEntry("Jake", 12000));
// data.add(new ValueDataEntry("Peter", 18000));
//AnyChartView anyChartView = (AnyChartView) findViewById(R.id.any_chart_view);
//anyChartView.setChart(pie);
}
}
build.gradle(项目)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.1"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle(应用程序)
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "30.0.1"
defaultConfig {
applicationId "org.jack.project"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.github.AnyChart:AnyChart-Android:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<!-- <com.anychart.anychart.AnyChartView-->
<!-- android:id="@+id/any_chart_view"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="match_parent"-->
<!-- />-->
</androidx.constraintlayout.widget.ConstraintLayout>
这里是错误:
Process: org.jack.project, PID: 956
java.lang.RuntimeException: Unable to start activity ComponentInfo{org.jack.project/org.jack.project.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.anychart.AnyChartView$JsListener com.anychart.AnyChartView.getJsListener()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
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:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.anychart.AnyChartView$JsListener com.anychart.AnyChartView.getJsListener()' on a null object reference
at com.anychart.APIlib.addJSLine(APIlib.java:27)
at com.anychart.charts.Pie.<init>(Pie.java:34)
at com.anychart.AnyChart.pie(AnyChart.java:130)
at org.jack.project.MainActivity.onCreate(MainActivity.java:23)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
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:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
我已经尝试了 cleaning/clearing cache/restarting/syncing 我的 gradle 文件。
在模拟器上,它说我的应用程序不断重启。
感谢您的帮助。
根本原因
我查看了 AnyChart-Android 源代码并了解了代码流程。
步骤 1. 来自我们的应用程序 Activity/Fragment
Pie pie = AnyChart.pie();
步骤 2. AnyChart.java
public static com.anychart.charts.Pie pie() {
return new com.anychart.charts.Pie("anychart.pie()");
}
步骤 3. Pie.java
public Pie(String jsChart) {
jsBase = "pie" + ++variableIndex;
APIlib.getInstance().addJSLine(jsBase + " = " + jsChart + ";");
}
步骤 4. APIlib.java
public class APIlib {
private AnyChartView anyChartView;
private static volatile APIlib instance;
public static APIlib getInstance() {
APIlib localInstance = instance;
if (localInstance == null) {
synchronized (APIlib.class) {
localInstance = instance;
if (localInstance == null) {
instance = localInstance = new APIlib();
}
}
}
return localInstance;
}
public void setActiveAnyChartView(AnyChartView anyChartView) {
this.anyChartView = anyChartView;
}
public void addJSLine(String jsLine) {
anyChartView.getJsListener().onJsLineAdd(jsLine);
}
}
正如我们看到的,当我们调用addJSLine(String)
方法时,它调用了anyChartView
的getJsListener()
方法,但是这个实例只是通过setActiveAnyChartView
方法设置的。并且此方法仅从 AnyChartView.java.
调用
@SuppressLint({"SetJavaScriptEnabled", "AddJavascriptInterface"})
private void init() {
...
APIlib.getInstance().setActiveAnyChartView(this);
...
}
init()
方法将在 AnyCharView
class.
的构造函数中调用
public AnyChartView(Context context) {
super(context);
init();
}
public AnyChartView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public AnyChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
因为在activity_main.xml
里面,你注释掉了代码
<!-- <com.anychart.anychart.AnyChartView-->
<!-- android:id="@+id/any_chart_view"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="match_parent"-->
<!-- />-->
这就解释了为什么出现异常。
解决方法:确保至少创建了一个AnyChartView
实例。
解决方案 1. 在 xml 文件
中声明 AnyCharView
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.anychart.AnyChartView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
解决方案2.如果想在运行时加一个AnyChartView
,那么可以使用
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val anyChartView = AnyChartView(this)
val pie: Pie = AnyChart.pie()
}
}
注意:我看到这个问题已经被报告了here,但是还没有答案。
我正在尝试为 Android Studio 使用 AnyChart 模块,但它一直给我错误 java.lang.RuntimeException: Unable to start activity ComponentInfo
。
以下是一些值得注意的文件:
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.anychart.AnyChart;
import com.anychart.AnyChartView;
import com.anychart.chart.common.dataentry.DataEntry;
import com.anychart.chart.common.dataentry.ValueDataEntry;
import com.anychart.charts.Pie;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Pie pie = AnyChart.pie();
//
// List<DataEntry> data = new ArrayList<>();
// data.add(new ValueDataEntry("John", 10000));
// data.add(new ValueDataEntry("Jake", 12000));
// data.add(new ValueDataEntry("Peter", 18000));
//AnyChartView anyChartView = (AnyChartView) findViewById(R.id.any_chart_view);
//anyChartView.setChart(pie);
}
}
build.gradle(项目)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.1"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle(应用程序)
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "30.0.1"
defaultConfig {
applicationId "org.jack.project"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.github.AnyChart:AnyChart-Android:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<!-- <com.anychart.anychart.AnyChartView-->
<!-- android:id="@+id/any_chart_view"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="match_parent"-->
<!-- />-->
</androidx.constraintlayout.widget.ConstraintLayout>
这里是错误:
Process: org.jack.project, PID: 956
java.lang.RuntimeException: Unable to start activity ComponentInfo{org.jack.project/org.jack.project.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.anychart.AnyChartView$JsListener com.anychart.AnyChartView.getJsListener()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
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:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.anychart.AnyChartView$JsListener com.anychart.AnyChartView.getJsListener()' on a null object reference
at com.anychart.APIlib.addJSLine(APIlib.java:27)
at com.anychart.charts.Pie.<init>(Pie.java:34)
at com.anychart.AnyChart.pie(AnyChart.java:130)
at org.jack.project.MainActivity.onCreate(MainActivity.java:23)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
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:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
我已经尝试了 cleaning/clearing cache/restarting/syncing 我的 gradle 文件。
在模拟器上,它说我的应用程序不断重启。
感谢您的帮助。
根本原因
我查看了 AnyChart-Android 源代码并了解了代码流程。
步骤 1. 来自我们的应用程序 Activity/Fragment
Pie pie = AnyChart.pie();
步骤 2. AnyChart.java
public static com.anychart.charts.Pie pie() {
return new com.anychart.charts.Pie("anychart.pie()");
}
步骤 3. Pie.java
public Pie(String jsChart) {
jsBase = "pie" + ++variableIndex;
APIlib.getInstance().addJSLine(jsBase + " = " + jsChart + ";");
}
步骤 4. APIlib.java
public class APIlib {
private AnyChartView anyChartView;
private static volatile APIlib instance;
public static APIlib getInstance() {
APIlib localInstance = instance;
if (localInstance == null) {
synchronized (APIlib.class) {
localInstance = instance;
if (localInstance == null) {
instance = localInstance = new APIlib();
}
}
}
return localInstance;
}
public void setActiveAnyChartView(AnyChartView anyChartView) {
this.anyChartView = anyChartView;
}
public void addJSLine(String jsLine) {
anyChartView.getJsListener().onJsLineAdd(jsLine);
}
}
正如我们看到的,当我们调用addJSLine(String)
方法时,它调用了anyChartView
的getJsListener()
方法,但是这个实例只是通过setActiveAnyChartView
方法设置的。并且此方法仅从 AnyChartView.java.
@SuppressLint({"SetJavaScriptEnabled", "AddJavascriptInterface"})
private void init() {
...
APIlib.getInstance().setActiveAnyChartView(this);
...
}
init()
方法将在 AnyCharView
class.
public AnyChartView(Context context) {
super(context);
init();
}
public AnyChartView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public AnyChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
因为在activity_main.xml
里面,你注释掉了代码
<!-- <com.anychart.anychart.AnyChartView-->
<!-- android:id="@+id/any_chart_view"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="match_parent"-->
<!-- />-->
这就解释了为什么出现异常。
解决方法:确保至少创建了一个AnyChartView
实例。
解决方案 1. 在 xml 文件
中声明AnyCharView
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.anychart.AnyChartView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
解决方案2.如果想在运行时加一个AnyChartView
,那么可以使用
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val anyChartView = AnyChartView(this)
val pie: Pie = AnyChart.pie()
}
}
注意:我看到这个问题已经被报告了here,但是还没有答案。