我是android编程的新手,我看过一个从服务器获取文件名并将其存储到列表中并构建一个listview来显示所有文件的教程,但这给了我空指针错误。谁能帮我改正代码,然后告诉我我的错误是什么,以便我学习。这是代码
public class VideoGalleryList extends AppCompatActivity {
URL url1;
List serverDir;
ListView galleryList;
public final String TAG="data.dot";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_gallery_list);
findViews();
}
protected void findViews() {
galleryList = findViewById(R.id.gallerylist);
List arraylist=GetfromServer();
ArrayAdapter arrayAdapter= new ArrayAdapter(this,android.R.layout.simple_list_item_1,arraylist);
galleryList.setAdapter(arrayAdapter);
}
public List GetfromServer(){
try {
url1=new URL("http://myipaddress/videos/");
ApacheURLLister lister = new ApacheURLLister();
serverDir=lister.listAll(url1);
Log.d(TAG,serverDir.toString());
}
catch (Exception e){
e.printStackTrace();
}
return serverDir;
}
}
我的错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.a49ersense, PID: 2022
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.a49ersense/com.example.a49ersense.VideoGalleryList}: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:392)
at android.widget.ListView.setAdapter(ListView.java:585)
at com.example.a49ersense.VideoGalleryList.findViews(VideoGalleryList.java:34)
at com.example.a49ersense.VideoGalleryList.onCreate(VideoGalleryList.java:27)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
答案 0 :(得分:0)
我希望这对您有用。
在您的findViews()
方法中初始化您的列表
serverDir = new ArrayList();
并使用GetfromServer()
这样的方法
public List GetfromServer(){
try {
url1=new URL("http://myipaddress/videos/");
ApacheURLLister lister = new ApacheURLLister();
serverDir.addAll(lister.listAll(url1));
Log.d(TAG,serverDir.toString());
}
catch (Exception e){
e.printStackTrace();
}
return serverDir;
}
答案 1 :(得分:-2)
首先检查空指针异常
List arraylist=GetfromServer();
if(arraylist!= null){
ArrayAdapter arrayAdapter= new ArrayAdapter(this,android.R.layout.simple_list_item_1,arraylist);
galleryList.setAdapter(arrayAdapter)
};
or
List arraylist = new ArrayList<>();
arraylist=GetfromServer();
ArrayAdapter arrayAdapter= new ArrayAdapter(this,android.R.layout.simple_list_item_1,arraylist);
galleryList.setAdapter(arrayAdapter)
因为您的函数getFromServer函数可能返回空值,所以您首先要初始化变量