更改ImageView资源错误

时间:2014-03-10 15:23:50

标签: android android-imageview

我是android的新手,并且开始制作一个基本的Pokedex。我在此活动上创建了两个按钮,每个按钮都会导致相同的新活动。一切正常,直到我在imageview上添加更改资源。

我希望能够根据用户按下的按钮更改新活动中的图像。我在case语句中尝试了按钮,但是有一个错误。所以我将它移到MainActivity并收到错误。

package com.example.pokedex;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.app.Activity;
import android.content.Intent;

public class MainActivity extends Activity implements OnClickListener {

Button buttonsquirtle;
Button buttonelectabuzz;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    buttonsquirtle=(Button)findViewById(R.id.moves);
    buttonelectabuzz=(Button)findViewById(R.id.moves2);
    buttonsquirtle.setOnClickListener(this);
    buttonelectabuzz.setOnClickListener(this);

    ImageView imagemark = (ImageView) findViewById(R.id.imageView1);
    imagemark.setImageResource(R.drawable.electabuzzicon);

}

@Override
public void onClick(View v) {

    switch (v.getId()){
    case R.id.moves:

        Intent i= new Intent(MainActivity.this, Squirtle.class);
        startActivity(i);
        break;
    case R.id.moves2:

        Intent j= new Intent(MainActivity.this, Squirtle.class);
        startActivity(j);   
        break;

    default:
        break;
    }       

}


}

Logcat错误发布如下:

03-10 11:05:01.560: E/AndroidRuntime(32080): FATAL EXCEPTION: main
03-10 11:05:01.560: E/AndroidRuntime(32080): java.lang.RuntimeException: Unable to start      activity ComponentInfo{com.example.pokedex/com.example.pokedex.MainActivity}:     java.lang.NullPointerException
03-10 11:05:01.560: E/AndroidRuntime(32080):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
03-10 11:05:01.560: E/AndroidRuntime(32080):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
03-10 11:05:01.560: E/AndroidRuntime(32080):    at android.app.ActivityThread.access$700(ActivityThread.java:159)
03-10 11:05:01.560: E/AndroidRuntime(32080):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
03-10 11:05:01.560: E/AndroidRuntime(32080):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-10 11:05:01.560: E/AndroidRuntime(32080):    at android.os.Looper.loop(Looper.java:137)
03-10 11:05:01.560: E/AndroidRuntime(32080):    at android.app.ActivityThread.main(ActivityThread.java:5419)
03-10 11:05:01.560: E/AndroidRuntime(32080):    at java.lang.reflect.Method.invokeNative(Native Method)
03-10 11:05:01.560: E/AndroidRuntime(32080):    at java.lang.reflect.Method.invoke(Method.java:525)
03-10 11:05:01.560: E/AndroidRuntime(32080):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
03-10 11:05:01.560: E/AndroidRuntime(32080):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
03-10 11:05:01.560: E/AndroidRuntime(32080):    at dalvik.system.NativeStart.main(Native Method)
03-10 11:05:01.560: E/AndroidRuntime(32080): Caused by: java.lang.NullPointerException
03-10 11:05:01.560: E/AndroidRuntime(32080):    at com.example.pokedex.MainActivity.onCreate(MainActivity.java:28)
03-10 11:05:01.560: E/AndroidRuntime(32080):    at android.app.Activity.performCreate(Activity.java:5372)
03-10 11:05:01.560: E/AndroidRuntime(32080):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
03-10 11:05:01.560: E/AndroidRuntime(32080):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
03-10 11:05:01.560: E/AndroidRuntime(32080):    ... 11 more

这是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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Button
    android:id="@+id/moves"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="50dp"
    android:text="@string/squirtle_button" />

<Button
    android:id="@+id/moves2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/moves"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:text="@string/electabuzz_button" />

</RelativeLayout>

这是imageView1所在的另一个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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Squirtle" >

<Button
    android:id="@+id/moves"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="58dp"
    android:layout_marginTop="52dp"
    android:text="@string/moves" />

<Button
    android:id="@+id/coverage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/moves"
    android:layout_below="@+id/moves"
    android:text="@string/weaknesses" />

<Button
    android:id="@+id/evolution"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/coverage"
    android:layout_below="@+id/coverage"
    android:text="@string/evolution" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/evolution"
    android:layout_toLeftOf="@+id/moves"
    android:src="@drawable/squirtleicon"
    android:contentDescription="@string/squirtle_image_desc" />

</RelativeLayout>

提前谢谢。

1 个答案:

答案 0 :(得分:0)

我会通过在启动第二个活动时使用intent extra来实现此目的:

// Launch the same activity for both buttons, but add a different value in the extras
case R.id.moves:
    Intent i= new Intent(MainActivity.this, Squirtle.class);
    i.putExtra("buttonPressed", "squirtle");
    startActivity(i);
    break;
case R.id.moves2:
    Intent j= new Intent(MainActivity.this, Squirtle.class);
    j.putExtra("buttonPressed", "electabuzz");
    startActivity(j);   
    break;

然后,在你的第二个Activity的onCreate方法中:

// Get Intent used to launch activity
Intent launchingIntent = getIntent();
// Get extras
Bundle content = launchingIntent.getExtras();

// Set image based on which button was pressed
String buttonPressed = content.getString("buttonPressed");
ImageView imageView = (ImageView) findViewById(R.id.imageView1);

if (buttonPressed.equals("squirtle")) {
    imageView.setImageResource(R.drawable.squirtleicon);
} else if (buttonPressed.equals("electabuzz")) {
    imageView.setImageResource(R.drawable.electabuzzicon);
}

这意味着根据用户在第一个活动中按下的按钮,在第二个活动中显示不同的图像。

使用常量定义字符串也可能是一个好主意,以防止错误类型的错误,例如在MainActivity中:

public final static String EXTRA_BUTTON_PRESSED = "buttonPressed";
public final static String BUTTON_SQUIRTLE = "squirtle";
public final static String BUTTON_ELECTABUZZ = "electabuzz";

然后在代码中引用它:

i.putExtra(EXTRA_BUTTON_PRESSED, BUTTON_SQUIRTLE);

...

j.putExtra(EXTRA_BUTTON_PRESSED, BUTTON_ELECTABUZZ);

在第二个活动中:

String buttonPressed = content.getString(MainActivity.EXTRA_BUTTON_PRESSED);

...

if (buttonPressed == MainActivity.BUTTON_SQUIRTLE) { ...

第一种方式可行,但更容易受到错别字的影响,除其他外:)