0

我需要显示 res/drawable 文件夹中的图像并将其放入 imageview 中。我做了一些研究并实施了它们。它只显示空白屏幕,但没有图像。

我的xml代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/mainlayout"
    />
</LinearLayout>

Java代码:

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {   
        String value = extras.getString("FaType");

        if(value.equals("AnimalBite"))
        {
            LinearLayout mainLayout = (LinearLayout)findViewById(R.id.mainlayout);

            ImageView imageView = new ImageView(this);
            imageView.setImageResource(R.drawable.medrem);
            LayoutParams imageViewLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            imageView.setLayoutParams(imageViewLayoutParams);

            mainLayout.addView(imageView);

        }
}

目录

08-21 20:54:35.698: E/Trace(684): error opening trace file: No such file or directory (2)
08-21 20:54:36.618: D/dalvikvm(684): GC_FOR_ALLOC freed 227K, 4% free 8142K/8455K, paused 123ms, total 142ms
08-21 20:54:36.948: D/dalvikvm(684): GC_CONCURRENT freed 196K, 4% free 8383K/8711K, paused 35ms+25ms, total 138ms
08-21 20:54:37.978: I/Choreographer(684): Skipped 199 frames!  The application may be doing too much work on its main thread.
08-21 20:54:38.158: D/gralloc_goldfish(684): Emulator without GPU emulation detected.
08-21 20:54:39.318: I/Choreographer(684): Skipped 64 frames!  The application may be doing too much work on its main thread.
08-21 20:54:47.318: I/Choreographer(684): Skipped 77 frames!  The application may be doing too much work on its main thread.
08-21 20:54:48.138: I/Choreographer(684): Skipped 45 frames!  The application may be doing too much work on its main thread.
08-21 20:54:49.798: D/AndroidRuntime(684): Shutting down VM
08-21 20:54:49.798: W/dalvikvm(684): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
08-21 20:54:49.848: E/AndroidRuntime(684): FATAL EXCEPTION: main
08-21 20:54:49.848: E/AndroidRuntime(684): java.lang.RuntimeException: Unable to start activity ComponentInfo{dr.droid/dr.droid.ImageViewer}: java.lang.NullPointerException
08-21 20:54:49.848: E/AndroidRuntime(684):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
08-21 20:54:49.848: E/AndroidRuntime(684):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-21 20:54:49.848: E/AndroidRuntime(684):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-21 20:54:49.848: E/AndroidRuntime(684):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-21 20:54:49.848: E/AndroidRuntime(684):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 20:54:49.848: E/AndroidRuntime(684):  at android.os.Looper.loop(Looper.java:137)
08-21 20:54:49.848: E/AndroidRuntime(684):  at android.app.ActivityThread.main(ActivityThread.java:4745)
08-21 20:54:49.848: E/AndroidRuntime(684):  at java.lang.reflect.Method.invokeNative(Native Method)
08-21 20:54:49.848: E/AndroidRuntime(684):  at java.lang.reflect.Method.invoke(Method.java:511)
08-21 20:54:49.848: E/AndroidRuntime(684):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-21 20:54:49.848: E/AndroidRuntime(684):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-21 20:54:49.848: E/AndroidRuntime(684):  at dalvik.system.NativeStart.main(Native Method)
08-21 20:54:49.848: E/AndroidRuntime(684): Caused by: java.lang.NullPointerException
08-21 20:54:49.848: E/AndroidRuntime(684):  at dr.droid.ImageViewer.onCreate(ImageViewer.java:29)
08-21 20:54:49.848: E/AndroidRuntime(684):  at android.app.Activity.performCreate(Activity.java:5008)
08-21 20:54:49.848: E/AndroidRuntime(684):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-21 20:54:49.848: E/AndroidRuntime(684):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-21 20:54:49.848: E/AndroidRuntime(684):  ... 11 more
08-21 20:54:52.268: I/Process(684): Sending signal. PID: 684 SIG: 9

有任何想法吗?我还需要知道这段代码的问题。因为每当我尝试在我的可绘制文件夹上添加图像并在此使用它时

imageView.setImageResource(R.drawable.medrem)

它似乎无法识别图像。请帮忙?

4

3 回答 3

3

更改您的if 条件if(value == "AnimalBite")

if(value.equals("AnimalBite"))

比较两个字符串总是使用.equals()String..

于 2012-08-21T12:44:55.147 回答
0

在您的代码中添加这一行

setContentView(R.layout.your layout name);

很高兴去。

于 2012-08-22T13:54:07.673 回答
0

你没有 setcontentview 吗?

这意味着您的 MainLayout 没有被夸大,这会导致在您的日志文件中抛出空指针异常。

手动充气或 setContentView(R.layout.xxxx);

于 2012-08-21T13:07:55.017 回答