我有两个活动,一个活动不显示其内容,但两个活动都显示其内容

时间:2016-04-02 23:22:41

标签: java android eclipse javahelp

我正在使用Android Studio并且我在两个活动中使用相同的代码,但是当我启动它时, NewActivity 将不会显示图像。这些活动是独立进行的,但是当它们连接起来时就不会发生。

activity_main.xml中

server {

listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html index.htm;

## default location ##
location / {
  access_log off;
  proxy_pass http://localhost:8080;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header Host $host;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

MainActivity.java

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
   android:padding="16dp"
    tools:context=" .MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:textStyle="bold"
        android:text="@string/choose_image"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignRight="@+id/img_show2"
        android:layout_alignEnd="@+id/img_show2" />

    <ImageButton
        android:id="@+id/img_show"
        android:layout_width="76dp"
        android:layout_height="71dp"
        android:scaleType="fitCenter"
        android:layout_alignTop="@+id/img_show6"
        android:layout_alignLeft="@+id/img_show6"
        android:layout_alignStart="@+id/img_show6" />

    <Button
        android:id="@+id/btn_choose_image"
        android:text="@string/choose_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <ImageButton
        android:id="@+id/img_show2"
        android:layout_width="76dp"
        android:layout_height="71dp"
        android:scaleType="fitCenter"
        android:layout_above="@+id/img_show9"
        android:layout_alignLeft="@+id/img_show01"
        android:layout_alignStart="@+id/img_show01" />

    <ImageButton
        android:layout_width="76dp"
        android:layout_height="71dp"
        android:id="@+id/img_show3"
        android:scaleType="fitCenter"
        android:layout_alignTop="@+id/img_show2"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <ImageButton
        android:layout_width="76dp"
        android:layout_height="71dp"
        android:id="@+id/img_show4"
        android:scaleType="fitCenter"
        android:layout_above="@+id/btn_choose_image"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ImageButton
        android:layout_width="76dp"
        android:layout_height="71dp"
        android:id="@+id/img_show5"
        android:scaleType="fitCenter"
        android:layout_marginLeft="42dp"
        android:layout_marginStart="42dp"
        android:layout_below="@+id/imageButton"
        android:layout_toRightOf="@+id/img_show"
        android:layout_toEndOf="@+id/img_show" />

    <ImageButton
        android:layout_width="76dp"
        android:layout_height="71dp"
        android:id="@+id/img_show6"
        android:scaleType="fitCenter"
        android:layout_above="@+id/img_show4"
        android:layout_centerHorizontal="true" />

    <ImageButton
        android:layout_width="76dp"
        android:layout_height="71dp"
        android:id="@+id/img_show7"
        android:scaleType="fitCenter"
        android:layout_alignBottom="@+id/img_show4"
        android:layout_alignLeft="@+id/img_show5"
        android:layout_alignStart="@+id/img_show5" />

    <ImageButton
        android:layout_width="76dp"
        android:layout_height="71dp"
        android:id="@+id/img_show8"
        android:scaleType="fitCenter"
        android:layout_above="@+id/btn_choose_image"
        android:layout_alignLeft="@+id/img_show7"
        android:layout_alignStart="@+id/img_show7" />

    <ImageButton
        android:layout_width="76dp"
        android:layout_height="71dp"
        android:id="@+id/img_show9"
        android:scaleType="fitCenter"
        android:layout_above="@+id/btn_choose_image"
        android:layout_alignRight="@+id/btn_choose_image"
        android:layout_alignEnd="@+id/btn_choose_image" />
    <ImageButton
        android:layout_width="76dp"
        android:layout_height="71dp"
        android:id="@+id/img_show1"
        android:scaleType="fitCenter"
        android:layout_alignBottom="@+id/img_show7"
        android:layout_centerHorizontal="true" />
    <ImageButton
        android:layout_width="76dp"
        android:layout_height="71dp"
        android:id="@+id/img_show01"
        android:scaleType="fitCenter"
        android:layout_below="@+id/img_show2"
        android:layout_toLeftOf="@+id/btn_choose_image"
        android:layout_toStartOf="@+id/btn_choose_image" />

    <ImageButton
        android:layout_width="96dp"
        android:layout_height="91dp"
        android:id="@+id/imageButton"
        android:scaleType="fitCenter"
        android:layout_above="@+id/img_show2"
        android:layout_alignLeft="@+id/img_show2"
        android:layout_alignStart="@+id/img_show2" />
    <Button
        android:layout_width="96dp"
        android:layout_height="91dp"
        android:id="@+id/Button1"
        android:text="@string/Button1"
        android:layout_above="@+id/img_show3"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

NewActivity.java文件

package autismpuzzles.tokenrewardsystem_austismpuzzles;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

import static android.view.View.*;

public class MainActivity extends AppCompatActivity {
    ImageButton img_show;
    ImageButton img_show2;
    ImageButton img_show3;
    ImageButton img_show4;
    ImageButton img_show5;
    ImageButton img_show6;
    ImageButton img_show7;
    ImageButton img_show8;
    ImageButton img_show9;
    Button btn_choose_image;
    private static final int PICK_IMAGE = 100;
    Uri imageUri;
    private final int REQUEST_CODE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    img_show = (ImageButton) findViewById(R.id.img_show);
    img_show2 = (ImageButton) findViewById(R.id.img_show2);
    img_show3 = (ImageButton) findViewById(R.id.img_show3);
    img_show4 = (ImageButton) findViewById(R.id.img_show4);
    img_show5 = (ImageButton) findViewById(R.id.img_show5);
    img_show6 = (ImageButton) findViewById(R.id.img_show6);
    img_show7 = (ImageButton) findViewById(R.id.img_show7);
    img_show8 = (ImageButton) findViewById(R.id.img_show8);
    img_show9 = (ImageButton) findViewById(R.id.img_show9);
    btn_choose_image = (Button) findViewById(R.id.btn_choose_image);
    btn_choose_image.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick (View v){
        openGallery();
        Intent intent = new Intent (MainActivity.this, NewActivity.class);
        startActivityForResult(intent, 1);
    }

   }

);
 }


private void openGallery() {
    Intent gallery = new Intent(Intent.ACTION_PICK,             
 MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(gallery, PICK_IMAGE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode,
     Intent data)
 {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
        imageUri = data.getData();
        img_show.setImageURI(imageUri);
        img_show2.setImageURI(imageUri);
        img_show3.setImageURI(imageUri);
        img_show4.setImageURI(imageUri);
        img_show5.setImageURI(imageUri);
        img_show6.setImageURI(imageUri);
        img_show7.setImageURI(imageUri);
        img_show8.setImageURI(imageUri);
        img_show9.setImageURI(imageUri);

    }
}

}

0 个答案:

没有答案