gpt4 book ai didi

java - 我无法使用java类获取资源。错误: Attempt to invoke virtual method

转载 作者:行者123 更新时间:2023-12-01 09:14:15 25 4
gpt4 key购买 nike

任何人都可以帮助我,我找不到如何修复此错误 引起原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.content.res.Resources android.content.Context.getResources()”

import java.util.ArrayList;
import java.util.List;

/** * 由 Ineza 于 2016 年 11 月 18 日创建。 */

public class DerpData extends Application {
private static Context mContext;

@Override
public void onCreate() {
super.onCreate();
mContext = this;
}

public static Context getContext(){
return mContext;
}



private static final String[] titles = DerpData.getContext().getResources().getStringArray(R.array.listArray);
private static final int[] icons =DerpData.getContext().getResources().getIntArray(R.array.imgs);


public static List<LisItem> getListData() {
List<LisItem> data = new ArrayList<>();

//Repeat process 4 times, so that we have enough data to demonstrate a scrollable
//RecyclerView
for (int x = 0; x < 4; x++) {
//create ListItem with dummy data, then add them to our List
for (int i = 0; i < titles.length && i < icons.length; i++) {
LisItem item = new LisItem();
item.setImageResId(icons[i]);
item.setTittle(titles[i]);
data.add(item);
}
}
return data;
}

}

这是logcat的一部分

11-19 06:07:42.188 6497-6497/com.example.ineza.assthree E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ineza.assthree, PID: 6497
java.lang.ExceptionInInitializerError
at com.example.ineza.assthree.RecyClerViewActivity.onCreate(RecyClerViewActivity.java:27)
at android.app.Activity.performCreate(Activity.java:6664)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at com.example.ineza.assthree.model.DerpData.<clinit>(DerpData.java:30)
at com.example.ineza.assthree.RecyClerViewActivity.onCreate(RecyClerViewActivity.java:27) 
at android.app.Activity.performCreate(Activity.java:6664) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
11-19 06:07:42.223 1326-1337/system_process W/ActivityManager: Force finishing activity com.example.ineza.assthree/.RecyClerViewActivity
11-19 06:07:42.284 1326-1337/system_process W/ActivityManager: Force finishing activity com.example.ineza.assthree/.LVCustomActivity
11-19 06:07:42.348 1326-1326/system_process W/art: Long monitor contention with owner Binder:1326_1 (1337) at void com.android.server.am.AppErrors.crashApplicationInner(com.android.server.am.Proc essRecord, android.app.ApplicationErrorReport$CrashInfo)(AppErrors.java:328) waiters=0 in int com.android.server.am.ActivityManagerService.broadcastIntent(android.app.IApplicationThread, android.content.Intent, java.lang.String, android.content.IIntentReceiver, int, java.lang.String, android.os.Bundle, java.lang.String[], int, android.os.Bundle, boolean, boolean, int) for 144ms
11-19 06:07:42.561 1326-1343/system_process E/BatteryStatsService: power: Missing API
11-19 06:07:42.562 1326-1375/system_process D/WifiNative-HAL: Failing getSupportedFeatureset because HAL isn't started
11-19 06:07:42.569 1326-1343/system_process E/BluetoothAdapter: Bluetooth binder is null
11-19 06:07:42.571 1326-1343/system_process E/BatteryStatsService: no controller energy info supplied
11-19 06:07:42.620 1326-1343/system_process E/BatteryStatsService: modem info is invalid: ModemActivityInfo{ mTimestamp=0 mSleepTimeMs=0 mIdleTimeMs=0 mTxTimeMs[]=[0, 0, 0, 0, 0] mRxTimeMs=0 mEnergyUsed=0}
11-19 06:07:42.723 1326-2409/system_process I/OpenGLRenderer: Initialized EGL, version 1.4

最佳答案

初始化类时,代码尝试从静态上下文访问静态变量。静态初始值设定项在创建类的任何实例之前执行。 mContext 在实例创建后在生命周期回调期间初始化。像这样更改您的代码(根据下面的评论更新):

@Override
public void onCreate() {
super.onCreate();
mContext = this;
titles = mContext.getResources().getStringArray(R.array.listArray);
icons = mContext.getResources().getIntArray(R.array.imgs);
}

private static final String[] titles;
private static final int[] icons;

关于java - 我无法使用java类获取资源。错误: Attempt to invoke virtual method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40692210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com