更改包名称应用程序后给出错误“java.lang.NullPointerException:尝试获取空数组的长度”[duplicate]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了更改包名称应用程序后给出错误“java.lang.NullPointerException:尝试获取空数组的长度”[duplicate]相关的知识,希望对你有一定的参考价值。
这个问题在这里已有答案:
注意: - 请停止投票我的问题,我不是android开发人员。
我不是在Android编程中,无法找到错误,我已经购买了一个应用程序的源代码,您可以从中下载.mp3歌曲。我已根据开发人员指令更改了包名称,但之后我收到此错误“java.lang.NullPointerException:尝试获取null数组的长度”。但是,如果使用旧包名称运行旧应用程序它工作正常,我联系卖家但他没有回复所以我决定在这里发布我的问题。请帮我解决这个错误。下面是logcat显示错误的代码,还附带了截图供审阅。
package com.mp3songsdownloader.searchonline;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
public class DownloadFragment extends Fragment implements constants {
View view;
ListView lv;
String[] items;
RelativeLayout NoShow;
ArrayAdapter<String> adp;
ArrayList<File> mySongs;
public DownloadFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void setMenuVisibility(final boolean visible) {
super.setMenuVisibility(visible);
if (visible) {
mySongs = findSongs(new File(Environment.getExternalStorageDirectory()+DOWNLOAD_DIRECTORY));
setVal();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_two, container, false);
lv=(ListView)view.findViewById(R.id.lvVideo);
NoShow=(RelativeLayout)view.findViewById(R.id.NothingToShow);
File dir = new File(Environment.getExternalStorageDirectory()+DOWNLOAD_DIRECTORY);
if (!dir.exists() && !dir.isDirectory()) {
dir.mkdir();
}
mySongs = findSongs(dir);
setVal();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, final int position, long arg3) {
final CharSequence[] items = {"Play", "Delete"};
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Options");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Delete")) {
removeItemFromList(position);
} else if (items[item].equals("Play")) {
Intent intent1 = new Intent(getActivity(), PlayerActivity.class);
Bundle extras = new Bundle();
//intent.putExtra(ID, m.getID());
//extras.putString("TITLE", mySongs.get(position).getName().toString());
extras.putString("TITLE", mySongs.get(position).getName());
extras.putString("ARTWORK", "nothing");
extras.putString("STREAM", mySongs.get(position).getAbsolutePath());
intent1.putExtras(extras);
startActivity(intent1);
}
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
return view;
}
public void setVal(){
items = new String[mySongs.size()];
if(mySongs.size()>0){
NoShow.setVisibility(NoShow.INVISIBLE);
lv.setVisibility(lv.VISIBLE);
}
for(int i=0; i<mySongs.size(); i++){
//toast(mySongs.get(i).toString());
// items[i]=mySongs.get(i).getName().toString().replace(".mp3","");
items[i]=mySongs.get(i).getName().replace(".mp3","");
}
adp= new ArrayAdapter<String>(getActivity(),R.layout.song_down_layout,R.id.title,items);
lv.setAdapter(adp);
}
protected void removeItemFromList(int position) {
final int deletePosition = position;
AlertDialog.Builder alert = new AlertDialog.Builder(
getActivity());
alert.setTitle("Delete");
alert.setMessage("Do you want delete this item?");
alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
File f = new File(mySongs.get(deletePosition).getAbsolutePath());
f.delete();
mySongs.remove(deletePosition);
setVal();
adp.notifyDataSetChanged();
adp.notifyDataSetInvalidated();
Toast.makeText(getActivity(), "Song Deleted!", Toast.LENGTH_SHORT).show();
}
});
alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
alert.show();
}
public ArrayList<File> findSongs(File root){
ArrayList<File> inFiles = new ArrayList<File>();
File[] files = root.listFiles();
for (File file : files) {
if (file.isDirectory()) {
inFiles.addAll(findSongs(file));
} else {
if(file.getName().endsWith(".mp3")){
inFiles.add(file);
}
}
}
return inFiles;
}
public void toast (String text){
Toast.makeText(getActivity(),text, Toast.LENGTH_SHORT).show();
}
}
Android Manifest.xml文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mp3songsdownloader.searchonline" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:label="@string/app_name"
android:theme="@style/MyMaterialTheme"
android:name="com.mp3songsdownloader.searchonline.App" >
<!--This meta-data tag is required to use Google Play Services.-->
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
<activity
android:name="com.mp3songsdownloader.searchonline.MainActivity"
android:label="@string/app_name"
android:windowSoftInputMode="adjustNothing" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.mp3songsdownloader.searchonline.PlayerActivity"
android:label="@string/app_name"
android:theme="@style/MyDialogTheme" >
</activity>
</application>
</manifest>
例外意味着
File[] files = root.listFiles();
将files
设为null
。这意味着listFiles()
已经返回null
。 javadoc的listFiles()
说:
“如果此抽象路径名不表示目录,或者发生I / O错误,则返回null。”
所以你需要弄清楚你为你想要列出的目录形成File
的方式有什么问题。
感谢所有人的帮助,我发现这只是因为没有外部存储的许可。
以上是关于更改包名称应用程序后给出错误“java.lang.NullPointerException:尝试获取空数组的长度”[duplicate]的主要内容,如果未能解决你的问题,请参考以下文章
“应用程序不断停止”弹出窗口并给出错误 java.lang.RuntimeException
Android Bundle java.lang.NoClassDefFoundError
Eclipse ADT 22.6.2 仍然给出错误“java.lang.NullPointerException”
引起:java.lang.ClassNotFoundException:net.sourceforge.cobertura.coveragedata.HasBeenInstrumented