从Button调用另一个活动

时间:2013-11-14 11:21:17

标签: android android-layout android-intent

我只是一个先发制人,请帮我解决这个问题。 我在这个应用程序中尝试的是 “我将使用手机摄像头拍摄照片(二手按钮)” - 我成功了。

在此之后,我有另一个按钮,它将调用另一个活动,我可以看到图像细节。 单独尝试时的两个活动都工作正常,但无法将它们合并到一个单独的应用程序。 使用此代码,应用程序崩溃;我在哪里错了?????

这是第一个屏幕的主要活动。 第二个活动的类名是ExifInfoActivity.java

package com.example.camexif;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;


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

        Button btnTakePhoto;
        Button Summary;
        ImageView imgTakenPhoto;
        private static final int CAMERA_PIC_REQUEST = 1888;


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

            btnTakePhoto = (Button) findViewById(R.id.button1);
            imgTakenPhoto = (ImageView) findViewById(R.id.imageView1);

            btnTakePhoto.setOnClickListener(new btnTakePhotoClicker());
            Summary=(Button)findViewById(R.id.button2);
            Summary.setOnClickListener(new screenresult());
        }

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

              if (requestCode == CAMERA_PIC_REQUEST) {
                  Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
                  imgTakenPhoto.setImageBitmap(thumbnail);
              }
        }

        class btnTakePhotoClicker implements Button.OnClickListener
        {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
            }
        }
        class screenresult implements Button.OnClickListener
        {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent intent = new Intent("package com.example.camexif.ExifInfoActivity");
                startActivity(intent);
            }
        }
    }

这是清单

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

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

    <application
        android:allowBackup="true"
        android:hasCode="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.camexif.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <activity android:name="com.example.exifinterfaceexample.ExifInfoActivity"
                  android:label="Exif Info">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这是activitymain.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="Press The Button To Capture"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="14dp"
        android:text="Capture" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="65dp"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="64dp"
        android:text="Press To See Summary" />

</RelativeLayout>

这里有一些日志声明

    11-14 16:37:48.516:
     E/AndroidRuntime(22924): FATAL EXCEPTION: main
    11-14 16:37:48.516: E/AndroidRuntime(22924): java.lang.RuntimeException: Unable to start activity 
    ComponentInfo{com.example.camexif/com.example.camexif.MainActivity}: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.Button
    11-14 16:37:48.516: E/AndroidRuntime(22924):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
    11-14 16:37:48.516: E/AndroidRuntime(22924):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
    11-14 16:37:48.516: E/AndroidRuntime(22924):    at android.app.ActivityThread.access$600(ActivityThread.java:162)
    11-14 16:37:48.516: E/AndroidRuntime(22924):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)

这是ExifInterfaceExample的代码

package com.example.camexif;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

public class ExifInterfaceExample extends ListActivity {
        private PictureCursorAdapter adapter = null;

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

        Cursor pictures = getContentResolver().query(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,null, null, null, null);

        if(null != pictures)
        {
                pictures.moveToFirst();

                adapter = new PictureCursorAdapter(this, R.layout.listitem, pictures);

                setListAdapter(adapter);
        }
    }

    @Override
        protected void onListItemClick(ListView list, View view, int position, long id) {
                super.onListItemClick(list, view, position, id);

                String filepath = (String) view.getTag();
                Intent intent = new Intent(this, ExifInfoActivity.class);

                intent.putExtra("file_path", filepath);

                startActivity(intent);
        }

        private class PictureCursorAdapter extends SimpleCursorAdapter{

                @SuppressWarnings("deprecation")
                public PictureCursorAdapter(Context context, int layout, Cursor c) {
                        super(context, layout, c, 
                                        new String[] { MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.Images.ImageColumns.DATA, MediaStore.Images.ImageColumns.SIZE},
                                        new int[] { R.id.displayname, R.id.path, R.id.size });
                }

                @Override
                public void bindView(View view, Context context, Cursor cursor) {
                        TextView title = (TextView)view.findViewById(R.id.displayname);
                        TextView path = (TextView)view.findViewById(R.id.path);
                        TextView size = (TextView)view.findViewById(R.id.size);

                        title.setText(cursor.getString(
                                        cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)));

                        path.setText(cursor.getString(
                                        cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA)));

                        int sizeIndex = cursor.getColumnIndex(MediaStore.Images.ImageColumns.SIZE);  

                        size.setText(android.text.format.Formatter.formatFileSize(ExifInterfaceExample.this, cursor.getLong(sizeIndex)));

                        view.setTag(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA)));
                }

                @Override
                public View newView(Context context, Cursor cursor, ViewGroup parent) {
                        LayoutInflater inflater = LayoutInflater.from(context);
                        View v = inflater.inflate(R.layout.listitem, parent, false);

                        bindView(v, context, cursor);

                        return v;
                }
    }
}

和第二个主要的xml我把它放在main2.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" >

    </ListView>

</RelativeLayout>

让我添加你,我做了一个干净的构建,并再次运行代码 当我按下第二个按钮看到它崩溃的摘要 这是eclipse中列出的日志

E/AndroidRuntime(27058): FATAL EXCEPTION: main
  E/AndroidRuntime(27058): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.camexif/com.example.camexif.ExifInfoActivity}; have you declared this activity in your AndroidManifest.xml?
  E/AndroidRuntime(27058):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1624)
  E/AndroidRuntime(27058):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1423)
  E/AndroidRuntime(27058):  at android.app.Activity.startActivityForResult(Activity.java:3388)
  E/AndroidRuntime(27058):  at android.app.Activity.startActivityForResult(Activity.java:3349)
  E/AndroidRuntime(27058):  at android.app.Activity.startActivity(Activity.java:3584)
  E/AndroidRuntime(27058):  at android.app.Activity.startActivity(Activity.java:3552)
  E/AndroidRuntime(27058):  at com.example.camexif.MainActivity$screenresult.onClick(MainActivity.java:61)
  E/AndroidRuntime(27058):  at android.view.View.performClick(View.java:4212)
  E/AndroidRuntime(27058):  at android.view.View$PerformClick.run(View.java:17476)
  E/AndroidRuntime(27058):  at android.os.Handler.handleCallback(Handler.java:800)
  E/AndroidRuntime(27058):  at android.os.Handler.dispatchMessage(Handler.java:100)
  E/AndroidRuntime(27058):  at android.os.Looper.loop(Looper.java:194)
  E/AndroidRuntime(27058):  at android.app.ActivityThread.main(ActivityThread.java:5371)
  E/AndroidRuntime(27058):  at java.lang.reflect.Method.invokeNative(Native Method)
  E/AndroidRuntime(27058):  at java.lang.reflect.Method.invoke(Method.java:525)
  E/AndroidRuntime(27058):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
  E/AndroidRuntime(27058):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
  E/AndroidRuntime(27058):  at dalvik.system.NativeStart.main(Native Method)

5 个答案:

答案 0 :(得分:0)

android.widget.ImageView cannot be cast to android.widget.Button

这一行是你的问题,在某处你将ImageView转换为Button。

答案 1 :(得分:0)

使用

更改screenresult代码
Intent intent = new Intent(MainActivity.this,ExifInfoActivity.class);
                    startActivity(intent);

答案 2 :(得分:0)

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

    Button btnTakePhoto;
    Button Summary;
    ImageView imgTakenPhoto;
    private static final int CAMERA_PIC_REQUEST = 1888;


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

        btnTakePhoto = (Button) findViewById(R.id.button1);
        btnTakePhoto.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

        }
    });                
        imgTakenPhoto = (ImageView) findViewById(R.id.imageView1);

        Summary=(Button)findViewById(R.id.button2);
        Summary.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent("package com.example.camexif.ExifInfoActivity");
            startActivity(intent);

        }
    });
    }

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

          if (requestCode == CAMERA_PIC_REQUEST) {
              Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
              imgTakenPhoto.setImageBitmap(thumbnail);
          }
    }


}

我认为上面的代码可以解决您的问题。虽然我不知道你试图通过Intent intent = new Intent("package com.example.camexif.ExifInfoActivity");

实现什么目标

答案 3 :(得分:0)

线索出现在logcat错误中:

  

android.content.ActivityNotFoundException:无法找到显式   活动课   {com.example.camexif /的 com.example.camexif.ExifInfoActivity };有你   在AndroidManifest.xml中声明了这个活动?

在AndroidManifest中,您使用以下包声明了一项活动:

<activity android:name="com.example.exifinterfaceexample.ExifInfoActivity"
          android:label="Exif Info">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
    </intent-filter>
</activity>

但您的 ExifInfoActivity 位于不同的包中:

package com.example.camexif;

所以按下按钮时会出现错误。

在AndroidManifest中,将包更改为:

<activity android:name="com.example.camexif.ExifInfoActivity"
      android:label="Exif Info">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
    </intent-filter>
</activity>

此外,对于您的摘要按钮侦听器,您正在以错误的方式构建Intent,请将其更改为:

class screenresult implements Button.OnClickListener
{
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        //change
        //Intent intent = new Intent("package com.example.camexif.ExifInfoActivity");
        //to
        Intent intent = new Intent(this, ExifInfoActivity.class);
        startActivity(intent);
    }
}

答案 4 :(得分:-1)

第二项活动中的活动名称不匹配。 我把第一个活动名称替换为第二个活动名称。

最简单的方法是遵循以下教程。

Call another activity from a button