代码之家  ›  专栏  ›  技术社区  ›  abc

如何使用资源中的字符串数组声明数组?[副本]

  •  0
  • abc  · 技术社区  · 6 年前

    我在资源目录中创建了一个字符串数组,其中包含三项值。然后尝试将数组放入字符串x中。它不会显示任何错误,但会崩溃。

     String x[]= getResources().getStringArray(R.array.z_array);
        String y[]={"1","2","3"};
    enter code here
    

    字符串数组代码如下-

    <string-array name="z_array">
    <item>a</item>
    <item>b</item>
    <item>c</item>
    

    如果我使用

         String x[]= {"a","b","c"};
    

    它工作得很好。

    这是我的自定义适配器代码-

       public class MyCustomAdapter extends ArrayAdapter{
        public MyCustomAdapter(Context context, int textViewResourceId,
                               String[] objects,String[] objects2){
    
            super(context, textViewResourceId, objects);
    
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return getCustomView(position, convertView, parent);
        }
    
        public View getCustomView(int position, View convertView, ViewGroup parent) {
    
            LayoutInflater inflater = getLayoutInflater();
    
            View view = inflater.inflate(R.layout.activity_list_view, parent, false);
    
            TextView nameV= (TextView) view.findViewById(R.id.uuuu);
    
            nameV.setText(x[position]);
    
            TextView authorNameV = (TextView) view.findViewById(R.id.vvvv);
            authorNameV.setText(y[position]);
    
            switch (position) {
                default:
                    break;
            }
    
            return view ;
        }
    
    }
    

    我的问题是如何使用资源中的字符串数组声明数组?

    Logcat公司

        02-25 03:45:34.851 5845-5845/? E/memtrack: Couldn't load memtrack module (No such file or directory)
    02-25 03:45:34.851 5845-5845/? E/android.os.Debug: failed to load memtrack module: -2
    02-25 03:45:40.011 5857-5857/? E/memtrack: Couldn't load memtrack module (No such file or directory)
    02-25 03:45:40.011 5857-5857/? E/android.os.Debug: failed to load memtrack module: -2
    02-25 03:45:40.741 5870-5870/? E/memtrack: Couldn't load memtrack module (No such file or directory)
    02-25 03:45:40.741 5870-5870/? E/android.os.Debug: failed to load memtrack module: -2
    02-25 03:45:40.931 5881-5881/? E/AndroidRuntime: in writeCrashedAppName, pkgName :maruf.infinity.story.englishshortstories
    02-25 03:45:40.931 5881-5881/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                     Process: maruf.infinity.story.englishshortstories, PID: 5881
                                                     java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{maruf.infinity.story.englishshortstories/maruf.infinity.story.englishshortstories.MainActivity}: java.lang.NullPointerException
                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                                                         at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                         at android.os.Looper.loop(Looper.java:136)
                                                         at android.app.ActivityThread.main(ActivityThread.java:5021)
                                                         at java.lang.reflect.Method.invokeNative(Native Method)
                                                         at java.lang.reflect.Method.invoke(Method.java:515)
                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
                                                         at dalvik.system.NativeStart.main(Native Method)
                                                      Caused by: java.lang.NullPointerException
                                                         at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
                                                         at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
                                                         at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:542)
                                                         at maruf.infinity.story.englishshortstories.MainActivity.<init>(MainActivity.java:35)
                                                         at java.lang.Class.newInstanceImpl(Native Method)
                                                         at java.lang.Class.newInstance(Class.java:1208)
                                                         at android.app.Instrumentation.newActivity(Instrumentation.java:1064)
                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
                                                         at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                         at android.os.Looper.loop(Looper.java:136) 
                                                         at android.app.ActivityThread.main(ActivityThread.java:5021) 
                                                         at java.lang.reflect.Method.invokeNative(Native Method) 
                                                         at java.lang.reflect.Method.invoke(Method.java:515) 
                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) 
                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 
                                                         at dalvik.system.NativeStart.main(Native Method) 
    02-25 03:45:41.311 5898-5898/? E/AndroidRuntime: in writeCrashedAppName, pkgName :maruf.infinity.story.englishshortstories
    02-25 03:45:41.311 5898-5898/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                     Process: maruf.infinity.story.englishshortstories, PID: 5898
                                                     java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{maruf.infinity.story.englishshortstories/maruf.infinity.story.englishshortstories.MainActivity}: java.lang.NullPointerException
                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                                                         at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                         at android.os.Looper.loop(Looper.java:136)
                                                         at android.app.ActivityThread.main(ActivityThread.java:5021)
                                                         at java.lang.reflect.Method.invokeNative(Native Method)
                                                         at java.lang.reflect.Method.invoke(Method.java:515)
                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
                                                         at dalvik.system.NativeStart.main(Native Method)
                                                      Caused by: java.lang.NullPointerException
                                                         at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
                                                         at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
                                                         at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:542)
                                                         at maruf.infinity.story.englishshortstories.MainActivity.<init>(MainActivity.java:35)
                                                         at java.lang.Class.newInstanceImpl(Native Method)
                                                         at java.lang.Class.newInstance(Class.java:1208)
                                                         at android.app.Instrumentation.newActivity(Instrumentation.java:1064)
                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
                                                         at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                         at android.os.Looper.loop(Looper.java:136) 
                                                         at android.app.ActivityThread.main(ActivityThread.java:5021) 
                                                         at java.lang.reflect.Method.invokeNative(Native Method) 
                                                         at java.lang.reflect.Method.invoke(Method.java:515) 
                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) 
                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 
                                                         at dalvik.system.NativeStart.main(Native Method) 
    02-25 03:45:41.451 5910-5910/? E/AndroidRuntime: in writeCrashedAppName, pkgName :maruf.infinity.story.englishshortstories
    02-25 03:45:41.451 5910-5910/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                     Process: maruf.infinity.story.englishshortstories, PID: 5910
                                                     java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{maruf.infinity.story.englishshortstories/maruf.infinity.story.englishshortstories.MainActivity}: java.lang.NullPointerException
                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                                                         at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                         at android.os.Looper.loop(Looper.java:136)
                                                         at android.app.ActivityThread.main(ActivityThread.java:5021)
                                                         at java.lang.reflect.Method.invokeNative(Native Method)
                                                         at java.lang.reflect.Method.invoke(Method.java:515)
                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
                                                         at dalvik.system.NativeStart.main(Native Method)
                                                      Caused by: java.lang.NullPointerException
                                                         at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
                                                         at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
                                                         at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:542)
                                                         at maruf.infinity.story.englishshortstories.MainActivity.<init>(MainActivity.java:35)
                                                         at java.lang.Class.newInstanceImpl(Native Method)
                                                         at java.lang.Class.newInstance(Class.java:1208)
                                                         at android.app.Instrumentation.newActivity(Instrumentation.java:1064)
                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
                                                         at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                         at android.os.Looper.loop(Looper.java:136) 
                                                         at android.app.ActivityThread.main(ActivityThread.java:5021) 
                                                         at java.lang.reflect.Method.invokeNative(Native Method) 
                                                         at java.lang.reflect.Method.invoke(Method.java:515) 
                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827) 
                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643) 
                                                         at dalvik.system.NativeStart.main(Native Method) 
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Mehdi Jahed Manesh    6 年前

    您关于从资源获取阵列的代码是可以的。确保 x[] 值,您可以将其打印在 logcat

    for (String name : x) {
            Log.d(name);
        }
    

    有一个很大的,但我想您在MainActivity中创建了适配器作为内部类,对吗?

    如果否:您的x[]和y[]为空。 如果是:演示如何实例化适配器并将其设置为列表视图。