-1

我将must.jpgindrawable-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
4

6 回答 6

2

有很多事情你可以避免并且很容易将你想要的图像加载到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);
于 2013-02-22T04:44:26.837 回答
0

你使用了可绘制的。您调用 setImageDrawable 方法,然后设置资源 getResources ().getDrawable set id getdrawable 方法,而不是图像将显示。

于 2013-02-22T04:54:24.577 回答
0

最可能的原因是您的应用程序在尝试解码下面一行中的图像时内存不足

Bitmap bitmap = BitmapFactory.decodeFile(poster.getAbsolutePath());

您尝试加载的此图像的尺寸是多少?

于 2013-02-22T04:55:23.683 回答
0

在您的活动中,您可以调用它来将您的可绘制资源作为可绘制对象并将图像设置如下:

     Drawable d = getResources().getDrawable(R.drawable.must);
     ImageView poster_view = (ImageView)findViewById(R.id.imageView));
      poster_view.setBackgroundDrawable(d);

或者

如果你想在 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"/>
于 2013-02-22T04:55:55.387 回答
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布局并将其设置idandroid:id="@+id/imageView".

于 2013-02-22T04:43:38.467 回答
0

一个问题是 Android 设备很可能没有名为/BakuMovies/res/drawable-hdpi. 这是您的开发环境的目录结构,不会直接传输到设备。除此之外,我不能在不知道你想要做什么的情况下给予。

编辑:

请注意,您res目录中的所有资源都包含在您应用的 .APK 文件中。它们不会作为单独的文件加载到设备中。为了访问 Java 代码中的资源(字符串除外),您需要使用一个Resources对象。你可以通过调用 getResources()一个Activity对象来获得一个。获得Resources对象后,调用getDrawable()getDrawableForDensity()获取可绘制资源。

于 2013-02-22T04:40:21.343 回答