0

I have been going through the android tutorial by thenewboston and got stuck up on 41st tutorial. My app is force closing as soon as I open camera activity.

Camera.java

package com.thenewboston.akshit;

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.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

public class Camera extends Activity implements View.OnClickListener {

ImageButton ib;
Button b;
ImageView iv;
Intent i;
final static int cameraData=0;
Bitmap bmp;


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

}
private void initialize() {
    iv = (ImageView) findViewById(R.id.ivReturnedPic);
    b = (Button) findViewById(R.id.bSetWall);
    iv = (ImageView) findViewById(R.id.ivReturnedPic);
    b.setOnClickListener(this);
    ib.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()){
    case R.id.bSetWall:
        try{
        getApplicationContext().setWallpaper(bmp);}
        catch (IOException e) { e.printStackTrace(); }      
        break;

    case R.id.ibTakePic:
        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();
        bmp = (Bitmap) extras.get("data");
        iv.setImageBitmap(bmp); 

    }
}

}

Menu.java

package com.thenewboston.akshit;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Menu extends ListActivity {
String classes[] = {"StartingPoint", "TextPlay", "Email", "Camera"};


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes));

}


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    String cheese = classes[position];
    super.onListItemClick(l, v, position, id);  
    try{
    Class ourClass = Class.forName("com.thenewboston.akshit." + cheese);
    Intent ourIntent = new Intent(Menu.this, ourClass);
    startActivity(ourIntent);
    }catch(ClassNotFoundException e){
        e.printStackTrace();
    }


}

}

AndroidManifest.xml

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.SET_WALLPAPER"/>

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.thenewboston.akshit.StartingPoint"
        android:label="Starting Point" >
        <intent-filter>
            <action android:name="com.thenewboston.akshit.STARTINGPOINT" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.thenewboston.akshit.Menu"
        android:label="Starting Point" >
        <intent-filter>
            <action android:name="com.thenewboston.akshit.MENU" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.thenewboston.akshit.TextPlay"
        android:label="Starting Point" >
    </activity>
     <activity
        android:name="com.thenewboston.akshit.Email"
        android:label="Starting Point" >
    </activity>
     <activity
        android:name="com.thenewboston.akshit.Camera"
        android:label="Starting Point" >
    </activity>
</application>

log

06-28 03:23:03.080: D/dalvikvm(871): Not late-enabling CheckJNI (already on)
06-28 03:23:06.280: D/gralloc_goldfish(871): Emulator without GPU emulation detected.
06-28 03:23:09.520: D/dalvikvm(871): GC_FOR_ALLOC freed 55K, 5% free 3032K/3172K, paused 124ms, total 130ms
06-28 03:23:09.520: I/dalvikvm-heap(871): Grow heap (frag case) to 3.646MB for 635812-byte allocation
06-28 03:23:09.590: D/dalvikvm(871): GC_FOR_ALLOC freed 6K, 4% free 3646K/3796K, paused 63ms, total 63ms
06-28 03:23:10.210: I/Choreographer(871): Skipped 72 frames!  The application may be doing too much work on its main thread.
06-28 03:23:15.810: D/AndroidRuntime(871): Shutting down VM
06-28 03:23:15.810: W/dalvikvm(871): threadid=1: thread exiting with uncaught exception (group=0xb3a36b90)
06-28 03:23:15.850: E/AndroidRuntime(871): FATAL EXCEPTION: main
06-28 03:23:15.850: E/AndroidRuntime(871): Process: com.thenewboston.akshit, PID: 871
06-28 03:23:15.850: E/AndroidRuntime(871): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thenewboston.akshit/com.thenewboston.akshit.Camera}: java.lang.NullPointerException
06-28 03:23:15.850: E/AndroidRuntime(871):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
06-28 03:23:15.850: E/AndroidRuntime(871):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
06-28 03:23:15.850: E/AndroidRuntime(871):  at android.app.ActivityThread.access$700(ActivityThread.java:135)
06-28 03:23:15.850: E/AndroidRuntime(871):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
06-28 03:23:15.850: E/AndroidRuntime(871):  at android.os.Handler.dispatchMessage(Handler.java:102)
06-28 03:23:15.850: E/AndroidRuntime(871):  at android.os.Looper.loop(Looper.java:137)
06-28 03:23:15.850: E/AndroidRuntime(871):  at android.app.ActivityThread.main(ActivityThread.java:4998)
06-28 03:23:15.850: E/AndroidRuntime(871):  at java.lang.reflect.Method.invokeNative(Native Method)
06-28 03:23:15.850: E/AndroidRuntime(871):  at java.lang.reflect.Method.invoke(Method.java:515)
06-28 03:23:15.850: E/AndroidRuntime(871):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
06-28 03:23:15.850: E/AndroidRuntime(871):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
06-28 03:23:15.850: E/AndroidRuntime(871):  at dalvik.system.NativeStart.main(Native Method)
06-28 03:23:15.850: E/AndroidRuntime(871): Caused by: java.lang.NullPointerException
06-28 03:23:15.850: E/AndroidRuntime(871):  at com.thenewboston.akshit.Camera.initialize(Camera.java:37)
06-28 03:23:15.850: E/AndroidRuntime(871):  at com.thenewboston.akshit.Camera.onCreate(Camera.java:29)
06-28 03:23:15.850: E/AndroidRuntime(871):  at android.app.Activity.performCreate(Activity.java:5243)
06-28 03:23:15.850: E/AndroidRuntime(871):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-28 03:23:15.850: E/AndroidRuntime(871):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
06-28 03:23:15.850: E/AndroidRuntime(871):  ... 11 more
06-28 03:23:19.850: I/Process(871): Sending signal. PID: 871 SIG: 9
  • Look closely at your `initialize()` method. Provided your layout contains the expected `View`s, you're just missing an initialization there. The [stack trace](http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) tells you which line has the problem. – Mike M. Jun 28 '16 at 07:39

1 Answers1

0

You have to set a reference to ib on the intialize function before setting a click listener to it.

ib = (ImageButton) findViewById(R.id.ib) //Or the name that the ImageButton has
ib.setOnClickListener(this);
a.ras2002
  • 385
  • 2
  • 7
  • 21