方法调用中发生IllegalStateException

时间:2016-01-15 18:30:13

标签: java android

我是android新手。我目前正在开发一个多选文件列表并压缩文件。所以我编码了多个选择,但我有异常,这是IllegalstateException。到目前为止,我已经在我的onSdcardistner方法实现工作调试thaat但listTree()方法不起作用。当我从OnsdcardlistenerMethod()调用它时 这里是代码和例外

package com.example.testmutilplefile;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;

import android.os.Environment;
import android.util.Log;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ArrayAdapter;




public class MainActivity extends ActionBarActivity {
    public static String TAG = "SelectFiles";
    // public static String ZIP="zippingfile";
    public File filepath = Environment.getExternalStorageDirectory();
    public ListView directorytree;
    public ListView filetree;
    public TextView directorynames;
    public TextView files;
    public Button sdclick;
    public ArrayList<File> directorylist = new ArrayList<File>();
    public ArrayList<String> directoryname = new ArrayList<String>();
    public ArrayList<File> filelist = new ArrayList<File>();
    public ArrayList<String> myfilename = new ArrayList<String>();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialComponent();
        sdclick.setClickable(true);

    }


    public void initialComponent() {

        sdclick = (Button) findViewById(R.id.sdcardbutton);
        directorynames = (TextView) findViewById(R.id.directoriesname);
        files = (TextView) findViewById(R.id.filesname);
        directorytree = (ListView) findViewById(R.id.directorySelectionList);
        filetree = (ListView) findViewById(R.id.fileSelectionList);

    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    public void onSdCardListener(View v) {
        File parentfile = filepath.getParentFile();
        Log.d(TAG, parentfile.toString());
        //listTree();

        if (filepath.equals(Environment.getExternalStorageDirectory())) {
            Toast.makeText(this, "cant exit external storage",
                    Toast.LENGTH_SHORT).show();
                    listTree();

        } else {
            filepath = parentfile;
            //listTree();

        }
    }


    private void listTree() {
        // TODO Auto-generated method stub
        FileFilter filefilter = new FileFilter() {
            public boolean accept(File file) {
                return file.isFile();

            }

        };
        FileFilter directoryfilter = new FileFilter() {
            public boolean accept(File file) {
                return file.isDirectory();
            }
        };
        /* List of Directory */
        if (filepath.exists() && filepath.length() > 0) {
            File[] directorylistArray = filepath.listFiles(directoryfilter);
            for (File file : directorylistArray) {
                directorylist.add(file);
                directoryname.add(file.getName());
            }

            ArrayAdapter<String> directoryadapter = new ArrayAdapter<String>(
                    this, android.R.layout.simple_list_item_1, directoryname);
            directorytree.setAdapter(directoryadapter);
            File[] filelistArray = filepath.listFiles(filefilter);

            for (File file : filelistArray) {
                filelist.add(file);
                myfilename.add(file.getName());

            }

            ArrayAdapter<String> fileadapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, myfilename);
            filetree.setAdapter(fileadapter);
            Log.d(TAG, "List Created");

        }







    }





}

和我的例外是

01-15 13:06:57.640: D/dalvikvm(804): GC_FOR_ALLOC freed 65K, 5% free 3223K/3364K, paused 25ms, total 28ms
01-15 13:06:57.650: I/dalvikvm-heap(804): Grow heap (frag case) to 4.285MB for 1127536-byte allocation
01-15 13:06:57.690: D/dalvikvm(804): GC_FOR_ALLOC freed 3K, 4% free 4321K/4468K, paused 35ms, total 35ms
01-15 13:06:57.980: D/gralloc_goldfish(804): Emulator without GPU emulation detected.
01-15 13:07:08.210: D/SelectFiles(804): /storage
01-15 13:07:08.340: D/AndroidRuntime(804): Shutting down VM
01-15 13:07:08.340: W/dalvikvm(804): threadid=1: thread exiting with uncaught exception (group=0xb2a77ba8)
01-15 13:07:08.430: E/AndroidRuntime(804): FATAL EXCEPTION: main
01-15 13:07:08.430: E/AndroidRuntime(804): Process: com.example.testmutilplefile, PID: 804
01-15 13:07:08.430: E/AndroidRuntime(804): java.lang.IllegalStateException: Could not execute method of the activity
01-15 13:07:08.430: E/AndroidRuntime(804):  at android.view.View$1.onClick(View.java:3823)
01-15 13:07:08.430: E/AndroidRuntime(804):  at android.view.View.performClick(View.java:4438)
01-15 13:07:08.430: E/AndroidRuntime(804):  at android.view.View$PerformClick.run(View.java:18422)
01-15 13:07:08.430: E/AndroidRuntime(804):  at android.os.Handler.handleCallback(Handler.java:733)
01-15 13:07:08.430: E/AndroidRuntime(804):  at android.os.Handler.dispatchMessage(Handler.java:95)
01-15 13:07:08.430: E/AndroidRuntime(804):  at android.os.Looper.loop(Looper.java:136)
01-15 13:07:08.430: E/AndroidRuntime(804):  at android.app.ActivityThread.main(ActivityThread.java:5001)
01-15 13:07:08.430: E/AndroidRuntime(804):  at java.lang.reflect.Method.invokeNative(Native Method)
01-15 13:07:08.430: E/AndroidRuntime(804):  at java.lang.reflect.Method.invoke(Method.java:515)
01-15 13:07:08.430: E/AndroidRuntime(804):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
01-15 13:07:08.430: E/AndroidRuntime(804):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
01-15 13:07:08.430: E/AndroidRuntime(804):  at dalvik.system.NativeStart.main(Native Method)
01-15 13:07:08.430: E/AndroidRuntime(804): Caused by: java.lang.reflect.InvocationTargetException
01-15 13:07:08.430: E/AndroidRuntime(804):  at java.lang.reflect.Method.invokeNative(Native Method)
01-15 13:07:08.430: E/AndroidRuntime(804):  at java.lang.reflect.Method.invoke(Method.java:515)
01-15 13:07:08.430: E/AndroidRuntime(804):  at android.view.View$1.onClick(View.java:3818)
01-15 13:07:08.430: E/AndroidRuntime(804):  ... 11 more
01-15 13:07:08.430: E/AndroidRuntime(804): Caused by: java.lang.NullPointerException
01-15 13:07:08.430: E/AndroidRuntime(804):  at com.example.testmutilplefile.MainActivity.listTree(MainActivity.java:118)
01-15 13:07:08.430: E/AndroidRuntime(804):  at com.example.testmutilplefile.MainActivity.onSdCardListener(MainActivity.java:91)
01-15 13:07:08.430: E/AndroidRuntime(804):  ... 14 more
01-15 13:07:18.930: I/Process(804): Sending signal. PID: 804 SIG: 9

请给我一个指导我怎么能解决它,我没有得到它提前感谢

2 个答案:

答案 0 :(得分:1)

filepath.listFiles(directoryfilter);在118行中返回NPE,因此您需要或在清单文件<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

中添加权限

答案 1 :(得分:0)

你真正的问题是:

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

试试这个:

 if(filepath != null)
    if(filepath.exists() && filepath.length() > 0) 

使用文件导致异常使用try..catch,以便您的应用不会崩溃。