编辑:Android应用程序强制关闭,出现空指针异常-ArrayList无法填充项目



编辑:我意识到这已经被标记为重复。但我相信这有点不同,因为我并不是在问什么是空指针异常,或者如何在"编程基础"级别上修复它。如果我错了,请纠正我,但我相信我的问题涉及到一个更复杂的问题,即安卓框架。我的问题是,我一直试图按照YouTube的基本教程创建一个简单的媒体播放器应用程序,但该应用程序在模拟器中打开后会立即强制关闭。我认为这是某种配置错误,因为我已经完全遵循了教程。我已经根据给出的建议更新了我的代码片段。在模拟器中,应用程序强制关闭,但当我在手机上运行设备时,会显示toast消息"Null Array"。因此,我的问题是,为什么程序没有将项目添加到mySongs ArrayList中,因为我的设备的内部和外部存储目录中都有.mp3文件。我的代码如下。。。。

MainActivity.java:

public class MainActivity extends AppCompatActivity
{
    ListView lv;
    String[] items;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv = (ListView) findViewById(R.id.lvPlaylist);
    final ArrayList<File> mySongs = findSongs(Environment.getExternalStorageDirectory());
    items = new String[mySongs.size()];
    if (mySongs != null && !mySongs.isEmpty())
    {
        for (int i = 0; i < mySongs.size(); i++)
        {
            //toast(mySongs.get(i).getName().toString());
            items[i] = mySongs.get(i).getName().toString().replace("mp3", "").replace(".wav", "");
        }
    }
    else
    {
        //Log.e(getString(R.string.Exception));
        Toast.makeText(this, "Null Array", Toast.LENGTH_SHORT).show();
    }
    ArrayAdapter<String> adp = new ArrayAdapter<String>(getApplicationContext(), R.layout.song_layout, R.id.textView,items);
    lv.setAdapter(adp);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            startActivity(new Intent(getApplicationContext(), Player.class).putExtra("pos", position).putExtra("songlist", mySongs));
        }
    });
}
public ArrayList<File> findSongs(File root)
{
    ArrayList<File> al = new ArrayList<File>();
    File[] files = root.listFiles();
    for (File singleFile : files)
    {
        if (singleFile.isDirectory() && !singleFile.isHidden())
        {
            findSongs(singleFile);
        }
        else
        {
            if (singleFile.getName().endsWith(".mp3") || singleFile.getName().endsWith(".wav"))
            {
                al.add(singleFile);
            }
        }
    }
    return al;
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.user.mediaplayer.MainActivity">
<ListView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/lvPlaylist"
    android:layout_centerHorizontal="true"
    android:layout_alignParentTop="true" />
</RelativeLayout>

日志文件:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.mediaplayer/com.example.user.mediaplayer.MainActivity}: java.lang.NullPointerException: Attempt to get length of null array
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 Caused by: java.lang.NullPointerException: Attempt to get length of null array
    at com.example.user.mediaplayer.MainActivity.findSongs(MainActivity.java:42)
    at com.example.user.mediaplayer.MainActivity.onCreate(MainActivity.java:28)
    at android.app.Activity.performCreate(Activity.java:6237)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我还包括:

uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
uses-permission android:name="android.permission.STORAGE"
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

在AndroidManifest.xml文件中。如有任何正确方向的建议,我们将不胜感激。谢谢

您正在尝试获取ArrayList mySongs的长度。如果从findSongs()方法接收到的ArrayList为空,则可能导致mySongs.length 行出现Null Pointer异常

在对ArrayList mySongs执行任何操作之前,请检查其是否为null。

相关内容

  • 没有找到相关文章

最新更新