我将must.jpg
indrawable-hdpi
文件夹放入并创建了此方法。
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>
日志猫:
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