我真的不知道該怎麼辦了加載任何圖像...應用程序不會從網絡
我得到這個代碼(試過很多前):
final LoaderImageView image = (LoaderImageView) findViewById(R.id.loaderImageView);
image.setImageDrawable(getAnImageUrl()); *<--- is line 119, where logcat say the NullPointer is*emphasized text*
private String getAnImageUrl() {
return "http://i.imgur.com/n6jBV.jpg";
}
這應該顯示圖像,我也嘗試過許多其他的方法我發現在互聯網上,但它總是說:
08-15 12:37:57.770: E/AndroidRuntime(19455): FATAL EXCEPTION: main
08-15 12:37:57.770: E/AndroidRuntime(19455): java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.dlv.listactivity/com.dlv.listactivity.MainActivity}: java.lang.NullPointerException
08-15 12:37:57.770: E/AndroidRuntime(19455): at android.app.ActivityThread.performLaunchActivity
(ActivityThread.java:1651)
08-15 12:37:57.770: E/AndroidRuntime(19455): at android.app.ActivityThread.handleLaunchActivity
(ActivityThread.java:1667)
08-15 12:37:57.770: E/AndroidRuntime(19455): at android.app.ActivityThread.access$1500
(ActivityThread.java:117)
08-15 12:37:57.770: E/AndroidRuntime(19455): at android.app.ActivityThread$H.handleMessage
(ActivityThread.java:935)
08-15 12:37:57.770: E/AndroidRuntime(19455): at android.os.Handler.dispatchMessage(Handler.java:99)
08-15 12:37:57.770: E/AndroidRuntime(19455): at android.os.Looper.loop(Looper.java:123)
08-15 12:37:57.770: E/AndroidRuntime(19455): at android.app.ActivityThread.main
(ActivityThread.java:3691)
08-15 12:37:57.770: E/AndroidRuntime(19455): at java.lang.reflect.Method.invokeNative(Native
Method)
08-15 12:37:57.770: E/AndroidRuntime(19455): at java.lang.reflect.Method.invoke(Method.java:507)
08-15 12:37:57.770: E/AndroidRuntime(19455): at com.android.internal.os.ZygoteInit
$MethodAndArgsCaller.run(ZygoteInit.java:847)
08-15 12:37:57.770: E/AndroidRuntime(19455): at com.android.internal.os.ZygoteInit.main
(ZygoteInit.java:605)
08-15 12:37:57.770: E/AndroidRuntime(19455): at dalvik.system.NativeStart.main(Native Method)
08-15 12:37:57.770: E/AndroidRuntime(19455): Caused by: java.lang.NullPointerException
08-15 12:37:57.770: E/AndroidRuntime(19455): at com.dlv.listactivity.MainActivity.onCreate
(MainActivity.java:119)
08-15 12:37:57.770: E/AndroidRuntime(19455): at android.app.Instrumentation.callActivityOnCreate
(Instrumentation.java:1047)
08-15 12:37:57.770: E/AndroidRuntime(19455): at android.app.ActivityThread.performLaunchActivity
(ActivityThread.java:1615)
關於他得到一個空指針。
我也已經給允許到互聯網(嘗試過的每一個現在我發現在互聯網上):
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.DELETE_CACHE_FILES">
這是我list.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<com.dlv.listactivity.LoaderImageView
android:id="@+id/loaderImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<!-- image="http://developer.android.com/images/dialog_buttons.png" -->
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" android:layout_gravity="left|center_vertical" android:textSize="20dip" android:layout_marginLeft="10dip"/>
</LinearLayout>
我用一個listActivity
(而不是正常的Activity
),我得到了2 xml,我與inflate
結合。
我甚至還有其他的項目,我可以從互聯網上下載/顯示圖片...但是這一次不,我不能找到問題...
的充氣可以是一個問題?或者是其他東西?
請給我一些建議。
感謝目前,Bigflow
堆棧跟蹤說,這是在MainActivity.java線119 NullPointerException異常是什麼線說呢? – Petter 2012-08-15 10:57:29
@Petter'image.setImageDrawable(getAnImageUrl());' – Bigflow 2012-08-15 10:58:18
檢查此 - 有一個很好的示例下載並找出如何下載圖像,orginize列表等等http://stackoverflow.com/questions/541966/android-如何做我懶得加載的圖像在列表視圖 – dilix 2012-08-15 10:59:36