我有一个应用程序从sdCard加载声音并在AlertDialog中显示为list
。它加载并播放完美的声音。现在,我想限制list
中要加载的声音量。所以,这是一项任务:
我在sdCard的文件夹中有N个声音。我有一个列表,基于哪里 在条件我想要显示一定数量的声音。条件是:如果 condition1> 6和condition2< 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个。如果将两个字符串放在条件下它将起作用。感谢您的评论!
答案 0 :(得分:0)
你说如果它小于6就可以正常工作。这是因为你使用了&amp;&amp;在您的条件中运算符,如果第一个条件变为false,则不检查第二个条件,因此不能访问soundQ_check
。我认为这一行影响了您的变量:
soundsQ_check=Integer.valueOf(custom_sounds.getString("RAW_1_1", ""));
您可以通过在此行之后打印此变量的值来调试它。