清单中的两项活动

时间:2015-05-13 03:02:35

标签: android

所以我想要运行两个活动,第一个是当点击按钮改变背景时的活动。单击中间图像按钮时的第二个活动,打开相机,拍照然后将图像放在图像按钮中。目前只有MainActivity(摄像机活动)正在运行。如果可能,我想帮忙:)。

编辑: MyAndroidAppActivity不工作。只有MainActivity正在运行。我想帮助制作它,以便两个活动都有效。 编辑2: 没有崩溃,第二项活动完全不起作用。当我点击按钮时它应该改变背景,它什么都不做。

MainActivity:     package com.example.triptych4;

import java.io.File;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

    // label our logs "CameraApp3"
    private static String logtag = "CameraApp3";
    // tells us which camera to take a picture from
    private static int TAKE_PICTURE = 1;
    // empty variable to hold our image Uri once we store it
    private Uri imageUri;

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

        // look for the button we set in the view
        ImageButton cameraButton = (ImageButton)
                findViewById(R.id.button_camera);
        // set a listener on the button
        cameraButton.setOnClickListener(cameraListener);


    }

    // set a new listener
    private OnClickListener cameraListener = new OnClickListener() {
        public void onClick(View v) {
            // open the camera and pass in the current view
            takePhoto(v);
        }
    };

    public void takePhoto(View v) {
        // tell the phone we want to use the camera
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        // create a new temp file called pic.jpg in the "pictures" storage area of the phone
        File photo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "pic.jpg");
        // take the return data and store it in the temp file "pic.jpg"
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
        // stor the temp photo uri so we can find it later
        imageUri = Uri.fromFile(photo);
        // start the camera
        startActivityForResult(intent, TAKE_PICTURE);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    // override the original activity result function
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // call the parent
        super.onActivityResult(requestCode, resultCode, data);
        switch(requestCode) {
        // if the requestCode was equal to our camera code (1) then...
        case 1:
            // if the user took a photo and selected the photo to use
            if(resultCode == Activity.RESULT_OK) {
                // get the image uri from earlier
                Uri selectedImage = imageUri;
                // notify any apps of any changes we make
                getContentResolver().notifyChange(selectedImage, null);
                // get the imageView we set in our view earlier
                ImageButton imageButton = (ImageButton)findViewById(R.id.button_camera);
                // create a content resolver object which will allow us to access the image file at the uri above
                ContentResolver cr = getContentResolver();
                // create an empty bitmap object
                Bitmap bitmap;
                try {
                    // get the bitmap from the image uri using the content resolver api to get the image
                    bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);
                    // set the bitmap to the image view
                    imageButton.setImageBitmap(bitmap);
                    // notify the user
                    Toast.makeText(MainActivity.this, selectedImage.toString(), Toast.LENGTH_LONG).show();
                } catch(Exception e) {
                    // notify the user
                    Toast.makeText(MainActivity.this, "failed to load", Toast.LENGTH_LONG).show();
                    Log.e(logtag, e.toString());
                }
            }
        }
    }

}

MyAndroidAppActivity(更改背景活动)

package com.example.triptych4;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.view.View;
import android.view.View.OnClickListener;

public class MyAndroidAppActivity extends Activity {

    Button button;
    ImageView image;

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

        addListenerOnButton();

    }

    public void addListenerOnButton() {

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

        button = (Button) findViewById(R.id.btnChangeImage);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                image.setImageResource(R.drawable.android3d);


            }


        });

    }



}

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.triptych4"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" 
            android:screenOrientation="landscape">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MyAndroidAppActivity"
            android:label="@string/app_name" 
            android:screenOrientation="landscape">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

3 个答案:

答案 0 :(得分:0)

MainActivity中,您无法在任何地方MyAndroidAppActivity开放。首先,您必须调用MyAndroidAppActivity意图。 使用以下代码打开MyAndroidAppActivity

Intent intent = new Intent(this,MyAndroidAppActivity.class);
startActivity(intent);

您可以在MainActivity

中添加一个openAcitvity按钮
Button button = (Button) findViewById(R.id.openAcitivty);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
               Intent intent = new Intent(this,MyAndroidAppActivity.class);
               startActivity(intent);
            }


        });

答案 1 :(得分:0)

但是,你没有在你提到的代码中打开你的第二个活动

写下这行代码:

Intent intent = new Intent(this,MyAndroidAppActivity.class);
startActivity(intent);

单击图像按钮,开始第二个活动。

答案 2 :(得分:0)

使用此意图

private static final int CAMERA_REQUEST = 1888;
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(cameraIntent, CAMERA_REQUEST);