2013-11-14 171 views
1

我只是一個首發,plz幫助我處理這種情況。 我在這個應用程序中嘗試的是 「我將使用手機攝像頭拍照(使用一個按鈕)」---我成功了。從按鈕調用另一個活動

之後,我有另一個按鈕,將調用另一個活動,我可以看到圖像的細節。 這兩個活動時單獨嘗試工作正常,但我們無法將它們合併到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; 
       } 
    } 
} 

和第二主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> 

讓我外接U,我做了一個乾淨的構建並再次運行該代碼時間 當我按下第二個按鈕查看摘要它得到墜毀 這裏是在日食

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) 
+1

發表您的SeondActivity代碼和它的佈局文件... –

+0

安置自己的完整的日誌了。 – groff07

+0

**將您的'ImageView' ** id **名稱和'Button' ** id **名稱都改爲'xml'和'java',然後運行它。 – Hariharan

回答

-1

有活動名稱在第二個活動中不匹配。 我已經把第一個活動名稱替換爲第二個活動名稱。

最簡單的方法就是遵循下面的教程。

Call another activity from a button

0
列出的日誌

此行是您的問題,您正在將ImageView投射到某個按鈕。

+6

它應該是評論... –

+0

@ Fergal,這就是我想知道的 –

0

更改screenresult代碼

Intent intent = new Intent(MainActivity.this,ExifInfoActivity.class); 
        startActivity(intent); 
+0

@ Jyoti謝謝爲回覆 但它仍然不工作這是錯誤 11-14 17:02:39.324:E/AndroidRuntime(23736):java.lang.RuntimeException: 無法啓動活動ComponentInfo {com.example.camexif/com。 example.camexif.MainActivity}:java.lang.ClassCastException:android.widget.ImageView無法轉換爲android.widget.Button –

+0

請發佈ExifInfoActivity的代碼。 – jyomin

+0

發佈您的完整日誌。 – groff07

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");

+0

的活動標記的清單文件中刪除此代碼我已經嘗試過,辛苦工作:( –

+0

分享您的完整源代碼。我看看它,並幫助你。 – iZBasit

0

的線索是在logcat的錯誤是什麼:

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?

在你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> 

此外,爲您總結按鈕監聽,你正在構建的意圖走錯路了,請將其更改爲:

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); 
    } 
} 
相關問題