java.lang.RuntimeException:无法启动活动ComponentInfo {myActivityPathName}:java.lang.NullPointerException

时间:2012-11-19 17:08:53

标签: android android-activity

我知道周围有类似的问题,但所有答案都是针对这些应用程序的。请帮忙。 这是我遇到问题的活动的代码

 public class MainActivity extends Activity implements OnClickListener
{
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
ViewFlipper flipper;
private GestureDetector gestureDetector;
static View passedView;
static String folderName = "myOffice";
static String folderPath = Environment.getExternalStorageDirectory() + "/" + folderName;

@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    flipper = (ViewFlipper)findViewById(R.id.viewFlipper1);
    flipper.setOnClickListener(this);
    Button button1_1 = (Button)findViewById(R.id.button1_1);
    button1_1.setOnClickListener(this);
    Button button1_2 = (Button)findViewById(R.id.button1_2);
    button1_2.setOnClickListener(this);
    Button button3 = (Button)findViewById(R.id.button3);
    button3.setOnClickListener(this);
    Button newDoc = (Button)findViewById(R.id.newDoc);
    newDoc.setOnClickListener(this);
    listTest();
    gestureDetector = new GestureDetector(new MyGestureDetector());
    View mainview = (View) findViewById(R.id.viewFlipper1);

    // Set the touch listener for the main view to be our custom gesture listener
    mainview.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) 
        {
            if (gestureDetector.onTouchEvent(event)) 
            {
                return true;
            }
            return false;
        }
    });
}

public static void checkForDirectory() 
{
    File folder = new File(folderPath);
    if (folder.exists() == false) 
    {
        folder.mkdir();
    }       
}

public String[] getFiles() 
{
    String[] fileNames;
    File f = new File(folderPath);
    File[] files = f.listFiles();
    if (files.length == 0)
    {
        return null;
    }
    else 
    {
        fileNames = new String[files.length];
        for (int i=0; i < files.length; i++) 
        {
            fileNames[i] = files[i].getName();
        }
        return fileNames;
    }       
}

public void listTest()
{
    final ListView listView;
    String[] filesInFolder = getFiles();
    listView = (ListView)findViewById(R.id.listView1);
    listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, filesInFolder));
    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() 
    {
        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View v, int position, long id) 
        {
            TextView tv = (TextView)v;
            String fName = (String) tv.getText();
            File file = new File(folderPath, fName);
            showDeletedToast();
            return file.delete();               
        }

        private void showDeletedToast() 
        {
            Toast toast = Toast.makeText(getApplicationContext(), "File deleted, Will be removed from the list.", Toast.LENGTH_LONG);
            toast.show();
        }
    });

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
        {
            Intent intent = new Intent(getApplicationContext(), TextActivity.class);
            startActivity(intent);
            passedView = TextActivity.getView(v);
        }               
    }); 
    listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, filesInFolder));

}   

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

@Override
public void onClick(View v)
{   
    if (v.getId()==  R.id.button1_1) 
    {
        flipper.showNext();
    }
    else if(v.getId()==  R.id.button1_2)
    {
        flipper.showNext();
        flipper.showNext();
    }
    else if(v.getId()== R.id.newDoc)
    {
        Intent intent = new Intent(this, TextActivity.class);
        startActivity(intent);
    }
}

class MyGestureDetector extends SimpleOnGestureListener 
{
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) 
    {
        if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
        {
            return false;
        }

        // right to left swipe
        if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY)
        {
            flipper.showNext();
            MainActivity.this.overridePendingTransition(
                    R.anim.slide_in_right,
                    R.anim.slide_out_left
                    );
            // left to right swipe
        } 
        else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY)
        {
            flipper.showPrevious();
            MainActivity.this.overridePendingTransition(
                    R.anim.slide_in_left, 
                    R.anim.slide_out_right
                    );
        }

        return false;
    }

    // It is necessary to return true from onDown for the onFling event to register
    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }
}

}

为什么会发生这种情况?如何解决这个问题? LogCat输出:

11-19 22:55:58.730: E/AndroidRuntime(10964): FATAL EXCEPTION: main
11-19 22:55:58.730: E/AndroidRuntime(10964): java.lang.RuntimeException: Unable to start                                                        activity ComponentInfo{com.illuminatiRN.myOffice/com.illuminatiRN.myOffice.MainActivity}:   java.lang.NullPointerException
11-19 22:55:58.730: E/AndroidRuntime(10964):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1818)
11-19 22:55:58.730: E/AndroidRuntime(10964):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1834)
11-19 22:55:58.730: E/AndroidRuntime(10964):    at android.app.ActivityThread.access$500(ActivityThread.java:122)
11-19 22:55:58.730: E/AndroidRuntime(10964):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1027)
11-19 22:55:58.730: E/AndroidRuntime(10964):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-19 22:55:58.730: E/AndroidRuntime(10964):    at android.os.Looper.loop(Looper.java:132)
11-19 22:55:58.730: E/AndroidRuntime(10964):    at android.app.ActivityThread.main(ActivityThread.java:4126)
11-19 22:55:58.730: E/AndroidRuntime(10964):    at java.lang.reflect.Method.invokeNative(Native Method)
11-19 22:55:58.730: E/AndroidRuntime(10964):    at java.lang.reflect.Method.invoke(Method.java:491)
11-19 22:55:58.730: E/AndroidRuntime(10964):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
11-19 22:55:58.730: E/AndroidRuntime(10964):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
11-19 22:55:58.730: E/AndroidRuntime(10964):    at dalvik.system.NativeStart.main(Native Method)
11-19 22:55:58.730: E/AndroidRuntime(10964): Caused by: java.lang.NullPointerException
11-19 22:55:58.730: E/AndroidRuntime(10964):    at java.util.Arrays$ArrayList.<init>(Arrays.java:47)
11-19 22:55:58.730: E/AndroidRuntime(10964):    at java.util.Arrays.asList(Arrays.java:163)
11-19 22:55:58.730: E/AndroidRuntime(10964):    at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
11-19 22:55:58.730: E/AndroidRuntime(10964):    at com.illuminatiRN.myOffice.MainActivity.listTest(MainActivity.java:100)
11-19 22:55:58.730: E/AndroidRuntime(10964):    at com.illuminatiRN.myOffice.MainActivity.onCreate(MainActivity.java:49)
11-19 22:55:58.730: E/AndroidRuntime(10964):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
11-19 22:55:58.730: E/AndroidRuntime(10964):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1782)

1 个答案:

答案 0 :(得分:2)

传递给String filesInFolder ArrayAdapter数组是null,它会抛出您在堆栈跟踪中看到的NullPointerException。确保此数组( filesInFolder 非空。当您从getFiles()方法返回该数组时,请对其进行修改,以便在files.length0时返回空数组。