2012-11-19 136 views
0

我知道有類似的問題,但所有答案都是特定於這些應用程序的。請幫忙。 這是我這是有問題java.lang.RuntimeException:無法啓動活動ComponentInfo {myActivityPathName}:java.lang.NullPointerException

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) 
+4

http://nikez.nu/wp-content/uploads/2012/05/image.png – waqaslam

+0

您需要添加TextActivity到Manifiest –

+0

添加logcat的輸出! – narendranathjoshi

回答

2

filesInFolderString數組,你傳遞給ArrayAdapternull,這將你扔在堆棧跟蹤看到NullPointerException。確保這個數組(filesInfolder)是非空。當您從getFiles()方法中返回該數組時,請對其進行修改,以便在files.length0時返回空數組。

+0

它的工作,非常感謝! – narendranathjoshi

相關問題