My camera activity is not working:
I launch it from listActivity.
I have another three activities from the list activity but camera activity/intent force closes.
Camera.java
package com.alpha.beta;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
public class Camera extends Activity implements View.OnClickListener {
ImageView Image;
ImageView setWall;
ImageButton TakePic;
Bitmap bmap;
Intent i;
final static int cameraData = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
vars();
}
private void vars() {
// TODO Auto-generated method stub
Image = (ImageView) findViewById(R.id.ivReturnPic);
setWall = (ImageView) findViewById(R.id.bSetWallpaper);
TakePic = (ImageButton) findViewById(R.id.iBTakePic);
setWall.setOnClickListener(this);
TakePic.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.iBTakePic:
try {
getApplicationContext().setWallpaper(bmap);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.bSetWallpaper:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, cameraData);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
bmap = (Bitmap) extras.get("data");
Image.setImageBitmap(bmap);
}
}
}
Logcat
12-02 20:44:11.229: W/dalvikvm(6322): threadid=1: thread exiting with uncaught exception (group=0x40018578)
12-02 20:44:11.229: E/AndroidRuntime(6322): FATAL EXCEPTION: main
12-02 20:44:11.229: E/AndroidRuntime(6322): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.alpha.beta/com.alpha.beta.Camera}: java.lang.ClassCastException: android.widget.Button
12-02 20:44:11.229: E/AndroidRuntime(6322): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
12-02 20:44:11.229: E/AndroidRuntime(6322): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
12-02 20:44:11.229: E/AndroidRuntime(6322): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-02 20:44:11.229: E/AndroidRuntime(6322): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
12-02 20:44:11.229: E/AndroidRuntime(6322): at android.os.Handler.dispatchMessage(Handler.java:99)
12-02 20:44:11.229: E/AndroidRuntime(6322): at android.os.Looper.loop(Looper.java:130)
12-02 20:44:11.229: E/AndroidRuntime(6322): at android.app.ActivityThread.main(ActivityThread.java:3687)
12-02 20:44:11.229: E/AndroidRuntime(6322): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 20:44:11.229: E/AndroidRuntime(6322): at java.lang.reflect.Method.invoke(Method.java:507)
12-02 20:44:11.229: E/AndroidRuntime(6322): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-02 20:44:11.229: E/AndroidRuntime(6322): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-02 20:44:11.229: E/AndroidRuntime(6322): at dalvik.system.NativeStart.main(Native Method)
12-02 20:44:11.229: E/AndroidRuntime(6322): Caused by: java.lang.ClassCastException: android.widget.Button
12-02 20:44:11.229: E/AndroidRuntime(6322): at com.alpha.beta.Camera.vars(Camera.java:34)
12-02 20:44:11.229: E/AndroidRuntime(6322): at com.alpha.beta.Camera.onCreate(Camera.java:27)
12-02 20:44:11.229: E/AndroidRuntime(6322): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-02 20:44:11.229: E/AndroidRuntime(6322): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
12-02 20:44:11.229: E/AndroidRuntime(6322): … 11 more
Manifest File
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alpha.beta"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Splash"
android:label="@string/title_activity_app" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Menu"
android:label="@string/title_activity_app" >
<action android:name="com.alpha.beta.Menu" />
<category android:name="android.intent.category.DEFAULT" />
</activity>
<activity
android:name=".App"
android:label="@string/title_activity_app" >
</activity>
<activity
android:name=".TextPlay"
android:label="@string/title_activity_app" >
</activity>
<activity
android:name=".Email"
android:label="@string/title_activity_app" >
</activity>
<activity
android:name=".Camera"
android:label="@string/title_activity_app" >
</activity>
</application>
</manifest>
You did not show your layout file and the but is there (or its origin). What you do wrong is something like this: your layout element is for example
ImageViewbut you dofindViewById()and cast the result to i.e.ImageButton. This causes cast exception. So check all your layout elements and your code to see if there’s no said mismatch.And unless you are not need any special feature of certain view, but want to just attach listener to it, or change its visibility etc, then you should just cast result of
findViewById()toViewto not need to care if this is the correct cast or not (all UI elements are children of View).