嗨每一個我堅持做一個簡單的函數調用。這是我第一個來自iPhone的Android應用程序。每次我做的東西在OnCreate()我的應用程序崩潰。我敢肯定,這個問題很簡單我在onCreate()內調用方法卡住()
public class AndroidReaderActivity extends Activity {
public void test(){
File dir=new File("/assets");
String[] listefichiers;
int i;
listefichiers = dir.list();
for (i = 0; i < listefichiers.length; i++) {
System.out.println(listefichiers[i]);
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
test();
}
}
感謝你的幫助。
編輯: 這裏有貓LOG
05-15 19:52:55.710: E/AndroidRuntime(1956): FATAL EXCEPTION: main
05-15 19:52:55.710: E/AndroidRuntime(1956): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.infotel/com.infotel.AndroidReaderActivity}: java.lang.NullPointerException
05-15 19:52:55.710: E/AndroidRuntime(1956): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-15 19:52:55.710: E/AndroidRuntime(1956): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-15 19:52:55.710: E/AndroidRuntime(1956): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-15 19:52:55.710: E/AndroidRuntime(1956): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-15 19:52:55.710: E/AndroidRuntime(1956): at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 19:52:55.710: E/AndroidRuntime(1956): at android.os.Looper.loop(Looper.java:137)
05-15 19:52:55.710: E/AndroidRuntime(1956): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-15 19:52:55.710: E/AndroidRuntime(1956): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 19:52:55.710: E/AndroidRuntime(1956): at java.lang.reflect.Method.invoke(Method.java:511)
05-15 19:52:55.710: E/AndroidRuntime(1956): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-15 19:52:55.710: E/AndroidRuntime(1956): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-15 19:52:55.710: E/AndroidRuntime(1956): at dalvik.system.NativeStart.main(Native Method)
05-15 19:52:55.710: E/AndroidRuntime(1956): Caused by: java.lang.NullPointerException
05-15 19:52:55.710: E/AndroidRuntime(1956): at com.infotel.AndroidReaderActivity.test(AndroidReaderActivity.java:14)
05-15 19:52:55.710: E/AndroidRuntime(1956): at com.infotel.AndroidReaderActivity.onCreate(AndroidReaderActivity.java:23)
05-15 19:52:55.710: E/AndroidRuntime(1956): at android.app.Activity.performCreate(Activity.java:4465)
05-15 19:52:55.710: E/AndroidRuntime(1956): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-15 19:52:55.710: E/AndroidRuntime(1956): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-15 19:52:55.710: E/AndroidRuntime(1956): ... 11 more
程序給出了什麼錯誤? –
把你自己的方法放在onCreate方法下面 –
yiu是以一種錯誤的方式訪問資產文件夾..你將不得不使用像「file:/// ......」 –