我把must.jpg
放入drawable-hdpi
文件夾並創建了這個方法。在Android中顯示圖片
public void createImageViewOb(){
File poster = new File ("/BakuMovies/res/drawable-hdpi/must.jpg");
ImageView poster_view = (ImageView)findViewById(R.drawable.must);
Bitmap bitmap = BitmapFactory.decodeFile(poster.getAbsolutePath());
poster_view.setImageBitmap(bitmap);
}
我想通過調用這個方法在mainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createImageViewOb();
}
顯示在屏幕上的圖像但是,當我啓動應用崩潰。 我會感謝任何幫助。
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/imageView1"
android:layout_marginBottom="77dp"
android:layout_toRightOf="@+id/textSwitcher1"
android:text="@string/Albert"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/aboutMeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignTop="@+id/viewFlipper1"
android:onClick="showAboutMessage"
android:text="About Me" />
</RelativeLayout>
的logcat:
02-22 05:13:29.028: E/Trace(1145): error opening trace file: No such file or directory (2)
02-22 05:13:29.498: D/AndroidRuntime(1145): Shutting down VM
02-22 05:13:29.498: W/dalvikvm(1145): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
02-22 05:13:29.518: E/AndroidRuntime(1145): FATAL EXCEPTION: main
02-22 05:13:29.518: E/AndroidRuntime(1145): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bakumovies/com.example.bakumovies.MainActivity}: java.lang.NullPointerException
02-22 05:13:29.518: E/AndroidRuntime(1145): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
02-22 05:13:29.518: E/AndroidRuntime(1145): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-22 05:13:29.518: E/AndroidRuntime(1145): at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-22 05:13:29.518: E/AndroidRuntime(1145): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-22 05:13:29.518: E/AndroidRuntime(1145): at android.os.Handler.dispatchMessage(Handler.java:99)
02-22 05:13:29.518: E/AndroidRuntime(1145): at android.os.Looper.loop(Looper.java:137)
02-22 05:13:29.518: E/AndroidRuntime(1145): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-22 05:13:29.518: E/AndroidRuntime(1145): at java.lang.reflect.Method.invokeNative(Native Method)
02-22 05:13:29.518: E/AndroidRuntime(1145): at java.lang.reflect.Method.invoke(Method.java:511)
02-22 05:13:29.518: E/AndroidRuntime(1145): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-22 05:13:29.518: E/AndroidRuntime(1145): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-22 05:13:29.518: E/AndroidRuntime(1145): at dalvik.system.NativeStart.main(Native Method)
02-22 05:13:29.518: E/AndroidRuntime(1145): Caused by: java.lang.NullPointerException
02-22 05:13:29.518: E/AndroidRuntime(1145): at com.example.bakumovies.MainActivity.createImageViewOb(MainActivity.java:44)
02-22 05:13:29.518: E/AndroidRuntime(1145): at com.example.bakumovies.MainActivity.onCreate(MainActivity.java:24)
02-22 05:13:29.518: E/AndroidRuntime(1145): at android.app.Activity.performCreate(Activity.java:5008)
02-22 05:13:29.518: E/AndroidRuntime(1145): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
02-22 05:13:29.518: E/AndroidRuntime(1145): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
02-22 05:13:29.518: E/AndroidRuntime(1145): ... 11 more
02-22 05:13:31.449: I/Process(1145): Sending signal. PID: 1145 SIG: 9
請貼上你的logcat – 2013-02-22 04:38:48
什麼是錯誤?使用logcat獲取完整的堆棧跟蹤並將其發佈到此處。 – 2013-02-22 04:39:15
另外,請發佈'activity_main.xml'。 – 2013-02-22 04:41:35