尝试从空对象引用

时间:2019-07-11 05:38:24

标签: android

public static List<Integer> listResWithFilter(String startingCharacter, boolean 
shouldFetchRaw, boolean shouldFetchDrawable) throws IllegalAccessException {
    List<Integer> listofraw = new ArrayList<>();

    if(shouldFetchRaw){
        Field[] fields=R.raw.class.getFields();
        Log.i(TAG, "listResWithFilter: raw length: " + fields.length);
        for (int i = 0; i <= fields.length; i++) {
            Field field = fields[i];
            if(field.getName().startsWith(startingCharacter)){
                int id = fields[i].getInt(fields[i]);
                listofraw.add(id);
            }
        }
    }
    if(shouldFetchDrawable){
        Field[] fields=R.drawable.class.getFields();
        Log.i(TAG, "listResWithFilter: drawable length: " + fields.length);
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            Log.i(TAG, "listResWithFilter: checking: "+field.getName());
            if(field.getName().startsWith(startingCharacter) && field.getName().length() < 5){
                //listofraw[index] = fields[i].getInt(fields[i]);
                int id = fields[i].getInt(fields[i]);
                listofraw.add(id);
                Log.i(TAG, "listResWithFilter: found match: "+field.getName()+" id: "+id);
            }
        }
        Log.i(TAG, "listResWithFilter: found matches: "+listofraw.size());
    }


    return listofraw;
}
  

java.lang.RuntimeException:无法启动活动ComponentInfo {net.zahrauniversity.madaniqaida / net.zahrauniversity.madaniqaida.lesson.DynamicLesson}:java.lang.NullPointerException:尝试从字段'int java.lang.String中读取.count”在空对象引用上           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2594)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685)           在android.app.ActivityThread.access $ 900(ActivityThread.java:188)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1530)           在android.os.Handler.dispatchMessage(Handler.java:111)           在android.os.Looper.loop(Looper.java:210)           在android.app.ActivityThread.main(ActivityThread.java:5839)           在java.lang.reflect.Method.invoke(本机方法)           在java.lang.reflect.Method.invoke(Method.java:372)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1113)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:879)        原因:java.lang.NullPointerException:尝试从空对象引用上的字段'int java.lang.String.count'中读取           在java.lang.String.startsWith(String.java:1200)           在java.lang.String.startsWith(String.java:1183)           在net.zahrauniversity.madaniqaida.Helper.LessonLayoutHelper.listResWithFilter(LessonLayoutHelper.java:142)           在net.zahrauniversity.madaniqaida.lesson.DynamicLesson.onCreate(DynamicLesson.java:129)           在android.app.Activity.performCreate(Activity.java:6177)           在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2541)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2685)           在android.app.ActivityThread.access $ 900(ActivityThread.java:188)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1530)           在android.os.Handler.dispatchMessage(Handler.java:111)           在android.os.Looper.loop(Looper.java:210)           在android.app.ActivityThread.main(ActivityThread.java:5839)           在java.lang.reflect.Method.invoke(本机方法)           在java.lang.reflect.Method.invoke(Method.java:372)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1113)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:879)

0 个答案:

没有答案