0
當我運行我的Android應用程序時,出現錯誤「Unable to start activity ComponentInfo」錯誤。Android:無法啓動活動ComponentInfo:java.lang.NullPointerException
這是錯誤似乎正在發生的類。
public class GalleryZoom extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.gallery_zoom);
ImageView image = (ImageView) findViewById(R.id.imageZoom);
String selection = getIntent().getExtras().getString("image");
Toast.makeText(this, selection, Toast.LENGTH_LONG).show();
BitmapFactory.Options bmpOptions;
bmpOptions = new BitmapFactory.Options();
bmpOptions.inSampleSize = 1;
Bitmap bmp = LoadImage(getIntent().getExtras().getString("image"), bmpOptions);
image.setImageBitmap(bmp);
}
private Bitmap LoadImage(String URL, BitmapFactory.Options options) {
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in, null, options);
in.close();
} catch (IOException e1) {
}
return bitmap;
}
private InputStream OpenHttpConnection(String strURL) throws IOException {
InputStream inputStream = null;
URL url = new URL(strURL);
URLConnection conn = url.openConnection();
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setRequestMethod("GET");
httpConn.connect();
if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = httpConn.getInputStream();
}
} catch (Exception ex) {
}
return inputStream;
}
}
以下是我的LogCat錯誤消息。
07-20 00:15:12.779: D/AndroidRuntime(802): Shutting down VM
07-20 00:15:12.779: W/dalvikvm(802): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
07-20 00:15:12.789: E/AndroidRuntime(802): FATAL EXCEPTION: main
07-20 00:15:12.789: E/AndroidRuntime(802): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.example.mobile.application/com.example.example.mobile.application.gallery.GalleryZoom}: java.lang.NullPointerException
07-20 00:15:12.789: E/AndroidRuntime(802): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
07-20 00:15:12.789: E/AndroidRuntime(802): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
07-20 00:15:12.789: E/AndroidRuntime(802): at android.app.ActivityThread.access$600(ActivityThread.java:130)
07-20 00:15:12.789: E/AndroidRuntime(802): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
07-20 00:15:12.789: E/AndroidRuntime(802): at android.os.Handler.dispatchMessage(Handler.java:99)
07-20 00:15:12.789: E/AndroidRuntime(802): at android.os.Looper.loop(Looper.java:137)
07-20 00:15:12.789: E/AndroidRuntime(802): at android.app.ActivityThread.main(ActivityThread.java:4745)
07-20 00:15:12.789: E/AndroidRuntime(802): at java.lang.reflect.Method.invokeNative(Native Method)
07-20 00:15:12.789: E/AndroidRuntime(802): at java.lang.reflect.Method.invoke(Method.java:511)
07-20 00:15:12.789: E/AndroidRuntime(802): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-20 00:15:12.789: E/AndroidRuntime(802): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-20 00:15:12.789: E/AndroidRuntime(802): at dalvik.system.NativeStart.main(Native Method)
07-20 00:15:12.789: E/AndroidRuntime(802): Caused by: java.lang.NullPointerException
07-20 00:15:12.789: E/AndroidRuntime(802): at com.example.example.mobile.application.gallery.GalleryZoom.LoadImage(GalleryZoom.java:52)
07-20 00:15:12.789: E/AndroidRuntime(802): at com.example.example.mobile.application.gallery.GalleryZoom.onCreate(GalleryZoom.java:41)
07-20 00:15:12.789: E/AndroidRuntime(802): at android.app.Activity.performCreate(Activity.java:5008)
07-20 00:15:12.789: E/AndroidRuntime(802): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-20 00:15:12.789: E/AndroidRuntime(802): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
07-20 00:15:12.789: E/AndroidRuntime(802): ... 11 more
任何幫助將是非常讚賞:)和也,這工作得很好在模擬器上2.3.3,但是當最新的4.1跑了,我似乎得到這個錯誤。
我認爲這將是更好地做到:(!在= NULL)'如果{in.close()}' – Tomer 2012-08-07 08:46:23
@ ftom2您可以添加,但[關閉] (http://developer.android.com/reference/java/io/InputStream.html#close())方法可以拋出IOException。我們不想在這裏遇到任何問題。 – AMerle 2012-08-07 08:48:04
當然,但你的代碼仍然會拋出'NullPointerException',所以它不是真的有幫助嗎? – Tomer 2012-08-07 08:53:52