将图像发送到另一个活动,图像没有显示没有错误

时间:2014-02-12 17:42:40

标签: android eclipse android-activity imageview

所以我发送了一些文字到另一个活动,我正在尝试发送图像。没有任何错误并进行了一些研究(Android how to get image via putExtra)但是我的图像没有显示出来。想知道我的代码中没有包含什么来显示图像。提前谢谢。

//第一个活动

public class IntentsActivity extends Activity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button btn = (Button)this.findViewById(R.id.button1);
    btn.setOnClickListener(myListener);

}
// Create an anonymous implementation of OnClickListener
private OnClickListener myListener = new OnClickListener() {
  // do something when the button is clicked
    public void onClick(View v) {

        Intent myIntent = new Intent(v.getContext(), SecondPage.class);
        String text = ((TextView)findViewById(R.id.textView1)).getText().toString();

        ImageView Selection = ((ImageView) findViewById(R.id.imageView1));

        myIntent.putExtra("Text", text);
        myIntent.putExtra("img", R.drawable.icon);

        startActivityForResult(myIntent, 0); 
        } 
    };

}

//第二项活动

public class SecondPage extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);

    //get extras
    Intent intent = this.getIntent();
    Bundle b = intent.getExtras();
    String text = b.getString("Text");

    //show text
    TextView tv = (TextView) findViewById(R.id.textView1);
    tv.setText(text);

    //show image
    ImageView image = (ImageView) findViewById(R.id.imageView1);
    int resource = getIntent().getIntExtra("img", 0);
    image.setImageDrawable(getResources().getDrawable(resource));

    Button btn = (Button)this.findViewById(R.id.button1);
    btn.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent myIntent = new Intent(v.getContext(), IntentsActivity.class);
            startActivityForResult(myIntent, 0);

        }   
    });
    }
}

//一些XML(没有别的东西可以相关)

    <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/FirstLayout"
    android:textAppearance="?android:attr/textAppearanceLarge" >
</TextView>

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textCapWords" >

    <requestFocus />
</EditText>

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/icon" />

//来自armand的logcat错误

02-12 12:05:36.024: E/AndroidRuntime(562): FATAL EXCEPTION: main
02-12 12:05:36.024: E/AndroidRuntime(562): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.colum.iam/edu.colum.iam.SecondPage}: java.lang.NullPointerException
02-12 12:05:36.024: E/AndroidRuntime(562):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-12 12:05:36.024: E/AndroidRuntime(562):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-12 12:05:36.024: E/AndroidRuntime(562):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-12 12:05:36.024: E/AndroidRuntime(562):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-12 12:05:36.024: E/AndroidRuntime(562):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 12:05:36.024: E/AndroidRuntime(562):  at android.os.Looper.loop(Looper.java:123)
02-12 12:05:36.024: E/AndroidRuntime(562):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-12 12:05:36.024: E/AndroidRuntime(562):  at java.lang.reflect.Method.invokeNative(Native Method)
02-12 12:05:36.024: E/AndroidRuntime(562):  at java.lang.reflect.Method.invoke(Method.java:507)
02-12 12:05:36.024: E/AndroidRuntime(562):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-12 12:05:36.024: E/AndroidRuntime(562):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-12 12:05:36.024: E/AndroidRuntime(562):  at dalvik.system.NativeStart.main(Native Method)
02-12 12:05:36.024: E/AndroidRuntime(562): Caused by: java.lang.NullPointerException
02-12 12:05:36.024: E/AndroidRuntime(562):  at edu.colum.iam.SecondPage.onCreate(SecondPage.java:31)
02-12 12:05:36.024: E/AndroidRuntime(562):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-12 12:05:36.024: E/AndroidRuntime(562):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-12 12:05:36.024: E/AndroidRuntime(562):  ... 11 more

3 个答案:

答案 0 :(得分:0)

您还需要从Intent获取img密钥,该密钥在SecondPage Activity中包含可绘制ID:

    int drwable_id = bundle.getInt("img")); // get drawable id
   //show image
    ImageView image = (ImageView) findViewById(R.id.imageView1);
    image.setImageDrawable(getResources().getDrawable(drwable_id));

答案 1 :(得分:0)

在SecondPage上初始化图像后添加此内容:

int resource = getIntent().getIntExtra("img", 0);
image.setImageDrawable(getResources().getDrawable(resource));

答案 2 :(得分:0)

您必须检索图片链接,将代码更改为:

//show image
ImageView image = (ImageView) findViewById(R.id.imageView1);

int image_link = getIntent().getIntExtra("img", 0);

image.setImageResource(image_link);