1

我只是一个初学者,请帮助我解决这种情况。我在这个应用程序中尝试的是“我将使用手机摄像头拍照(使用一个按钮)”---我成功了。

在此之后,我有另一个按钮,它将调用另一个活动,我可以在其中查看图像详细信息。单独尝试时,这两个活动都运行良好,但无法将它们合并到 1 个单个应用程序中。使用此代码,应用程序崩溃;我哪里出错了??????

这是第一个屏幕的主要活动。第二个活动的类名是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;
                }
    }
}

和我把它放在 main2.xml 的第二个主要 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)
4

5 回答 5

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

这条线是您的问题,您将 ImageView 转换为 Button。

于 2013-11-14T11:26:02.097 回答
0

更改屏幕结果代码

Intent intent = new Intent(MainActivity.this,ExifInfoActivity.class);
                    startActivity(intent);
于 2013-11-14T11:30:54.407 回答
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");

于 2013-11-14T11:38:22.827 回答
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);
    }
}
于 2013-11-14T14:02:24.257 回答
-1

第二个活动中的活动名称不匹配。我用第一个活动名称代替了第二个活动名称。

最简单的方法是按照以下教程进行操作。

从按钮调用另一个活动

于 2013-11-21T13:16:00.070 回答