方法和变量

时间:2015-06-23 20:59:30

标签: android

再次询问有关我的代码的问题。它没有正常工作,我用我的XML调用它们,但它们仍然无法正常工作。以下是代码:问题代码以粗体突出显示。我也发布了我的XML。顶部的Java代码和我的XML代码位于底部。

     package app.com.example.android.shoutoutapp;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.R;
import android.*;
import java.io.FileNotFoundException;

public class MainActivity extends Activity {

    TextView textTargetUri;
    ImageView targetImage;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button buttonAddPhoto = (Button)findViewById(R.id.add_photo);
        textTargetUri = (TextView)findViewById(R.id.targeturi);
        targetImage = (ImageView)findViewById(R.id.targetimage);

        buttonAddPhoto.setOnClickListener(new Button.OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, 0);
            }});
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK){
            Uri targetUri = data.getData();
            textTargetUri.setText(targetUri.toString());
            Bitmap bitmap;
            try {
                bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
                targetImage.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

<RelativeLayout>
<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:id="@+id/imageView" />




     <TextView
            android:text= "Thot of The Day!"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="60sp"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:id="@+id/text_shout_out"



            />

        <Button
            android:id="@+id/add_photo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Add Photo"
            android:gravity="center"
            android:layout_below="@+id/text_shout_out"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginTop="131dp"
            android:onClick="LoadImage"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/targeturi"/>

    </RelativeLayout>

1 个答案:

答案 0 :(得分:2)

您的ImageView标识为imageView而不是targetimage

更改xml中的ImageView,如下所示

<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/targetimage" />