0

我确实有一个应用程序,它从 sdCard 加载声音并在 AlertDialog 中显示为list. 它加载和播放听起来很完美。现在,我想限制加载到list. 所以,这里有一个任务:

我在 sdCard 的文件夹中确实有 N 个声音。我确实有一个列表,根据条件我想显示 A 数量的声音。条件是:如果条件1>6 和条件2<40 则为假并在列表中显示6 个声音,否则如果条件为真,则在列表中显示所有声音。

这是我在代码中实现它的方式,但它给了我NullPointerException.

ins_mp3List = ins_Dir.listFiles(new Mp3Filter()); //list of all sounds in directory(N)

 user_instruments = new String[ins_mp3List.length]; //will be used in a system internally
 user_instruments_toShow =new String[ins_mp3List.length]; //will be shown in the list
 soundsQ_check=Integer.valueOf(custom_sounds.getString("RAW_1_1", ""));// the value is 0, it is checked
//Condition
 if (ins_mp3List.length>6 && soundsQ_check<40){ 
             user_instruments_block=true;
             for (int i=0; i<= 5; i++) {
              String [] seperated = ins_mp3List[i].toString().split(directory);
              user_instruments[i]=ins_mp3List[i].toString();
              user_instruments_toShow[i] = seperated[1];    
              Log.v("Instruments","true");
             }

         }else{
             for (int i=0; i< ins_mp3List.length; i++) {

              user_instruments_block=false;
              String [] seperated = ins_mp3List[i].toString().split(directory);
              user_instruments[i]=ins_mp3List[i].toString();
              user_instruments_toShow[i] = seperated[1];            
            Log.v("Instruments", user_instruments[i]);
               }

在 sdCard 文件夹中有 8 个文件。soundQ_check=0.

LogCat 给出错误:

01-27 19:52:11.652: E/AndroidRuntime(2667): FATAL EXCEPTION: main
01-27 19:52:11.652: E/AndroidRuntime(2667): java.lang.NullPointerException
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.widget.AbsListView.obtainView(AbsListView.java:2019)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.widget.ListView.onMeasure(ListView.java:1155)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.view.View.measure(View.java:12881)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:676)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:557)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.view.View.measure(View.java:12881)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:676)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:557)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.view.View.measure(View.java:12881)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:297)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.view.View.measure(View.java:12881)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:297)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.view.View.measure(View.java:12881)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:297)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2111)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.view.View.measure(View.java:12881)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.os.Looper.loop(Looper.java:137)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at android.app.ActivityThread.main(ActivityThread.java:4441)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at java.lang.reflect.Method.invokeNative(Native Method)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at java.lang.reflect.Method.invoke(Method.java:511)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-27 19:52:11.652: E/AndroidRuntime(2667):     at dalvik.system.NativeStart.main(Native Method)

我认为某个地方的情况是错误的......任何帮助将不胜感激。如果声音的数量小于 6,它可以正常工作,但如果大于 6,它会给出 nullPointerException ...

UPD LAST:我找到了解决方案。我不应该把

 user_instruments = new String[ins_mp3List.length]; //will be used in a system internally
 user_instruments_toShow =new String[ins_mp3List.length]; //will 

条件之外。这些线路应该在条件下运行。由于长度不同,它给了我错误。实际长度是 8,我只初始化了其中的 6 个。如果将这两个字符串都置于条件下,它将起作用。感谢您的评论!

4

1 回答 1

0

你是说如果它小于 6,它就可以正常工作。这是因为你在你的条件下使用了 && 运算符,如果第一个条件变为假,它不会检查第二个条件,因此不能访问。soundQ_check我认为这条线是影响你的变量:

 soundsQ_check=Integer.valueOf(custom_sounds.getString("RAW_1_1", ""));

您可以通过在此行之后打印此变量的值来调试它。

于 2013-01-27T15:26:18.947 回答