我将must.jpg
放在drawable-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>
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
答案 0 :(得分:1)
您可以避免许多事情,并且可以非常轻松地将所需图像加载到ImageView
。
做这样的事情: -
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// New Code
ImageView poster_view = (ImageView)findViewById(R.id.imageView); //Assuming an ImgView is there in your layout activity_main
poster_view.setImageResource(R.drawable.must); //Must.jpg present in any of your drawable folders.
}
更新: - 要动态插入ImageView
,请在onCreate()
中执行此操作。
ImageView imageView = new ImageView(this);
LinearLayout.LayoutParams lp =
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(lp);
imageView.setImageResource(R.drawable.must);
yourLinearLayout.addView(imageView);
答案 1 :(得分:0)
一个问题是Android设备很可能没有名为/BakuMovies/res/drawable-hdpi
的目录。这是开发环境的目录结构,不直接传输到设备。除此之外,我不能不知道你想要做什么就给予。
修改强>
请注意,res
目录中的所有资源都包含在应用的.APK文件中。它们不作为单个文件加载到设备。要在Java代码中访问资源(字符串除外),您需要使用Resources
对象。您可以通过在Activity
对象上调用getResources()
来获取一个。获得Resources
对象后,请致电getDrawable()
或getDrawableForDensity()
以获取可绘制资源。
答案 2 :(得分:0)
试试这个。
public void createImageViewOb (){
ImageView poster_view = (ImageView)findViewById((R.id.imageView); // imageView is id of your imageView
poster_view.setImageResource(R.drawable.must);
}
<强>更新强>
首先,您必须在ImageView
版面上添加activity_main
,并将其id
设置为android:id="@+id/imageView"
。
答案 3 :(得分:0)
你用过drawable。你调用setImageDrawable方法并设置资源getResources()。getDrawable set id getdrawable方法比将显示图像。
答案 4 :(得分:0)
最可能的原因是您的应用程序在尝试解码下面一行中的图像时内存不足
Bitmap bitmap = BitmapFactory.decodeFile(poster.getAbsolutePath());
您尝试加载此图片的尺寸是多少?
答案 5 :(得分:0)
在您的活动中,您可以调用此方法将Drawable资源作为Drawable对象,并将图像设置如下:
Drawable d = getResources().getDrawable(R.drawable.must);
ImageView poster_view = (ImageView)findViewById(R.id.imageView));
poster_view.setBackgroundDrawable(d);
OR
如果您想在ImageView中显示您的drawable,您可以这样做:
ImageView poster_view = (ImageView)findViewById(R.id.imageView);
poster_view.setImageResource(getResources().getDrawable(R.drawable.must));
或在您的xml文件中
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/smiley"/>