-1

フォルダーに入れ、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
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メソッドを呼び出し、set resource getResources().getDrawable setidgetdrawableメソッドよりも画像が表示されます。

于 2013-02-22T04:54:24.577 に答える
0

最も可能性の高い原因は、以下の行の画像をデコードしようとしているときに、アプリケーションのメモリが不足していることです。

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

ロードしようとしているこの画像のサイズはどれくらいですか?

于 2013-02-22T04:55:23.683 に答える
0

アクティビティでは、これを呼び出してDrawableリソースをDrawableオブジェクトとして取得し、画像を次のように設定できます。

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

また

ドローアブルをImageViewに表示したい場合は、次のようにすることができます。

    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

問題の 1 つは、Android デバイスに という名前のディレクトリがない可能性が高いこと/BakuMovies/res/drawable-hdpiです。これは開発環境のディレクトリ構造であり、デバイスに直接転送されるわけではありません。それ以外は、あなたが何をしようとしているのかわからないまま与えることはできません。

編集:

ディレクトリ内のすべてのリソースはres、アプリの .APK ファイルに含まれていることに注意してください。これらは、個別のファイルとしてデバイスにロードされません。Java コード内のリソース (文字列以外) にアクセスするには、Resourcesオブジェクトを使用する必要があります。オブジェクトを呼び出し getResources()て取得しActivityます。オブジェクトを取得したら、 orをResources呼び出して描画可能なリソースを取得します。getDrawable()getDrawableForDensity()

于 2013-02-22T04:40:21.343 に答える
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_mainidandroid:id="@+id/imageView"

于 2013-02-22T04:43:38.467 に答える