我的第一个应用

时间:2017-08-04 13:26:13

标签: android nullpointerexception

我是Android Studio编程的新手,我编写了我的第一个(更大)应用程序。当我在调试器中启动它时,该应用程序工作正常。 但当我连接我的平板电脑并尝试启动我的应用程序时,我立即收到错误

08-04 14:40:27.752 30433-30433/com.applications.ars.mynotizenundactivityapp E/AndroidRuntime: FATAL EXCEPTION: main                                                           Process: com.applications.ars.mynotizenundactivityapp, PID: 30433
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.applications.ars.mynotizenundactivityapp/com.applications.ars.mynotizenundactivityapp.DateiAuswahlActivity}: java.lang.NullPointerException: Attempt to get length of null array                                                                   
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2984)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
Caused by: java.lang.NullPointerException: Attempt to get length of null array
at com.applications.ars.mynotizenundactivityapp.DateiAuswahlActivity.onCreate(DateiAuswahlActivity.java:96)
at android.app.Activity.performCreate(Activity.java:6955)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045) 
at android.app.ActivityThread.-wrap14(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6776) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386) 

我用Google搜索了错误,并且发现在我的布局中我的.xml可能存在问题。

这是我的.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.applications.ars.mynotizenundactivityapp.DateiAuswahlActivity">
    <ListView
        android:id="@+id/lv_myListView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:entries="@color/colorAccent"
        android:visibility="visible" />
</android.support.constraint.ConstraintLayout>

这是我的java文件:

package com.applications.ars.mynotizenundactivityapp;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;
import java.util.concurrent.ExecutionException;


public class DateiAuswahlActivity<T> extends AppCompatActivity {

    String my_projektverzeichnis;
    String my_projektordnername = "ars_MyOrdner";
    ArrayList my_karteikastenListe = new ArrayList();
    ArrayList my_URLKarteikastenListe = new ArrayList();
    String my_URLKarteikaesten_longString;
    String [] URLKarteikastenArray;
    ListAdapter adapter;

    ListView lv;
    int stopper = 1;
    GetURLFolderContent my_getURLFolderContent = new GetURLFolderContent();

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_dateiauswahl, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_URL_Download) {
            Toast.makeText(DateiAuswahlActivity.this, "Karteikästen werden heruntergeladen", Toast.LENGTH_LONG).show();
            try {
                my_URLKarteikaesten_longString = my_getURLFolderContent.execute("http://www.meuchelmord.net/andy/Android/listFiles.php").get();
                stopper =1;
            } catch (InterruptedException e) {
                Toast.makeText(DateiAuswahlActivity.this, "Sorry, keine Internetverbindung", Toast.LENGTH_LONG).show();
                e.printStackTrace();
            } catch (ExecutionException e) {
                Toast.makeText(DateiAuswahlActivity.this, "Sorry, keine Internetverbindung", Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
            URLKarteikastenArray = my_URLKarteikaesten_longString.split("<br />");
            for (int i=0;i<URLKarteikastenArray.length-1;i++){
                my_URLKarteikastenListe.add(URLKarteikastenArray[i]);
            }


            for (int i=0;i<my_URLKarteikastenListe.size();i++){
                String my_tempurl = "http://www.meuchelmord.net/andy/Android/".concat(my_URLKarteikastenListe.get(i).toString());
                GetURLFile my_URLFile = new GetURLFile();
                my_URLFile.execute(my_tempurl,my_URLKarteikastenListe.get(i).toString());
                my_URLFile = null;
            }
            my_karteikastenListe = my_URLKarteikastenListe;
            for (int i=0;i<my_karteikastenListe.size();i++){
                my_karteikastenListe.set(i,my_karteikastenListe.get(i).toString().replace(".kartei",""));
            }
            adapter = new ArrayAdapter<T>(getApplicationContext(), android.R.layout.simple_list_item_1, my_karteikastenListe);
            //lv = (ListView)findViewById(R.id.lv_myListView);
            lv.setAdapter(adapter);

            stopper =1;
            return true;
        }


        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_datei_auswahl);

        my_projektverzeichnis = Environment.getExternalStorageDirectory() + "/" + my_projektordnername;
        File my_dateien_alle = new File(my_projektverzeichnis);
        File[] my_dateien_alle_Array = my_dateien_alle.listFiles();


        for(int i = 0; i<my_dateien_alle_Array.length;i++){
            if (my_dateien_alle_Array[i].getName().endsWith(".kartei") ){

                String my_tempname = my_dateien_alle_Array[i].toString().replace(".kartei","");
                String[] my_temparray = my_tempname.split("/");
                my_tempname = my_temparray[my_temparray.length-1];
                my_karteikastenListe.add(my_tempname);
            }
        }

        //List valueList = new ArrayList<String>();
        //for (int i = 0; i < 10; i++)
        //{
        //    valueList.add("value"+i);
        //}
        //ListAdapter adapter = new ArrayAdapter<T>(getApplicationContext(), android.R.layout.simple_list_item_1, my_karteikastenListe);
        adapter = new ArrayAdapter<T>(getApplicationContext(), android.R.layout.simple_list_item_1, my_karteikastenListe);
        lv = (ListView)findViewById(R.id.lv_myListView);
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {


            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long id) {
                Intent intent = new Intent();
                intent.setClassName(getPackageName(), getPackageName()+".MainActivity");
                intent.putExtra("Karteikasten", lv.getAdapter().getItem(arg2).toString());
                startActivity(intent);
            }
            });
        stopper = 1;
    }
}

关于我的平板电脑: 三星Galaxy TAB A Android 7.0版 内核版本:3.18.14-11316739

我的模拟器配置没有问题: Android 7.0(Google API) API 24

有没有人可以给我一个小费,我可以搜索问题? 我不知道......

希望得到答复, 安迪

3 个答案:

答案 0 :(得分:1)

java.lang.NullPointerException: Attempt to get length of null array

在您的尝试中使用my_dateien_alle_Array.length,但此ArrayList没有长度,因为它是NULL

答案 1 :(得分:0)

看起来my_dateien_alle.listFiles()正在返回null

File[] my_dateien_alle_Array = my_dateien_alle.listFiles();

当你试图获得空数组的长度时,你会遇到NPE:

for (int i=0;i<my_URLKarteikastenListe.size();i++){

答案 2 :(得分:0)

File my_dateien_alle = new File(my_projektverzeichnis);我猜这个目录没有被创建,所以下一行File[] my_dateien_alle_Array = my_dateien_alle.listFiles();返回null

如果目录不存在,您可能需要这样的内容:

File dir = new File(Environment.getExternalStorageDirectory() + "/dirname");
if (!dir.exists()) {
    folder.mkdir();
}