无法启动新活动,继续刷新

时间:2015-06-23 03:43:14

标签: android

我想创建一个简单的图像按钮来启动一个新的活动(CameraActivity),在这种情况下,是一个简单的相机应用程序,可以拍摄照片并将其存储在目录中。

MainActivity:

 public void CameraButton() {

    ImageButton Click = (ImageButton) findViewById(R.id.CameraButton);
    Click.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this,CameraActivity.class);
            startActivity(intent);
        }
    });
}

CameraActivity.class:

import java.io.File;

import android.view.View;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.Toast;

public class CameraActivity extends MainActivity
{

static int TAKE_PIC = 1;
Uri outPutfileUri;

public void onClick(View v)
{

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File file = new File(Environment.getExternalStorageDirectory(),
            "MyPhoto.jpg");
    outPutfileUri = Uri.fromFile(file);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutfileUri);
    startActivityForResult(intent, TAKE_PIC);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == TAKE_PIC && resultCode == RESULT_OK) {
        Toast.makeText(this, outPutfileUri.toString(), Toast.LENGTH_LONG).show();
    }
}
}

activity_main XML文件:

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/CameraButton"
    android:background="@drawable/camerapicture"
    android:onClick="onClick"
    android:layout_alignParentBottom="true"
    android:layout_alignParentStart="true" />

每次点击按钮,应用程序都会自动刷新。我在清单文件中声明了CameraActivity。

2 个答案:

答案 0 :(得分:0)

您正在从MainActivity启动CameraActivity,但您从MainActivity扩展了CameraActivity,因此直接启动MainActivity的CameraActivity使其看起来如此  清爽。要解决这个问题,你需要从Activity类中扩展CameraActivity,如下所示

public class CameraActivity extends Activity
{
  //your Activity body 
}

或者如果您使用AppCompact,那么您需要从ActionBarActivity扩展,如此

 public class CameraActivity extends ActionBarActivity
 {
  //your Activity body 
 }

我跳这会帮助你

答案 1 :(得分:0)

请更改您的CameraActivity代码

import java.io.File;

import android.view.View;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.Toast;

public class CameraActivity extends Activity
{

static int TAKE_PIC = 1;
Uri outPutfileUri;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.YOUR_CAMERA_ACTIVITY_LAYOUT);



    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File file = new File(Environment.getExternalStorageDirectory(),
            "MyPhoto.jpg");
    outPutfileUri = Uri.fromFile(file);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutfileUri);
    startActivityForResult(intent, TAKE_PIC);

    }

public void onClick(View v)
{

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File file = new File(Environment.getExternalStorageDirectory(),
            "MyPhoto.jpg");
    outPutfileUri = Uri.fromFile(file);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutfileUri);
    startActivityForResult(intent, TAKE_PIC);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == TAKE_PIC && resultCode == RESULT_OK) {
        Toast.makeText(this, outPutfileUri.toString(), Toast.LENGTH_LONG).show();
    }
}
}