0

I'm newbie in android dev, n i'm starting to learn how to change one activity to another. I've learnt with different tool to implement it,like app inventor, android studio until eclipse. But, i have same error, such force close if i click button to do another activity. Here is my example code thats make me confuse. i use with eclipse.

this my main activity

package com.kontak.latihan;

import com.latihan.kontak.form.MenuForm;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;

public class KontakActivity extends Activity implements OnClickListener {

private MenuForm form;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_kontak);

    form = new MenuForm(this);

    form.getButtonBuatKontakBaru().setOnClickListener(this);
    form.getButtonDaftarKontak().setOnClickListener(this);
}

public void onClick(View v){
    if(v == form.getButtonBuatKontakBaru()){
        Intent intent = new Intent(this, BuatActivity.class);
        startActivity(intent);
    }

}


}

this is my other activity

package com.kontak.latihan;

import com.kontak.latihan.database.KontakDatabase;
import com.kontak.latihan.model.Kontak;
import com.latihan.kontak.form.BuatForm;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public  class BuatActivity extends Activity implements OnClickListener{

private BuatForm form;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.buat);

    form = new BuatForm(this);

    form.getButtonSimpan().setOnClickListener(this);
}



@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    Kontak kontak = new Kontak();
    kontak.nama = form.getEditTextNama().getText().toString();
    kontak.telepon = form.getEditTextTelepon().getText().toString();
    kontak.email = form.getEditTextEmail().getText().toString();

    KontakDatabase database = KontakDatabase.getInstance();
    database.add(kontak);

    // reset
    form.reset();
}





}

this is my manifest file

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.kontak.latihan.KontakActivity"
        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:label="Buat Kontak Baru"
        android:name=".BuatActivity">
    </activity>
     <activity 
        android:label="Buat Daftar Baru"
        android:name=".DaftarActivity">
    </activity>
</application>

</manifest>

this is my error log

12-13 10:09:13.706: E/AndroidRuntime(357): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kontak.latihan/com.kontak.latihan.KontakActivity}: java.lang.NullPointerException
12-13 10:09:13.706: E/AndroidRuntime(357):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-13 10:09:13.706: E/AndroidRuntime(357):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-13 10:09:13.706: E/AndroidRuntime(357):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-13 10:09:13.706: E/AndroidRuntime(357):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-13 10:09:13.706: E/AndroidRuntime(357):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-13 10:09:13.706: E/AndroidRuntime(357):  at android.os.Looper.loop(Looper.java:123)
12-13 10:09:13.706: E/AndroidRuntime(357):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-13 10:09:13.706: E/AndroidRuntime(357):  at java.lang.reflect.Method.invokeNative(Native Method)
12-13 10:09:13.706: E/AndroidRuntime(357):  at java.lang.reflect.Method.invoke(Method.java:507)
12-13 10:09:13.706: E/AndroidRuntime(357):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-13 10:09:13.706: E/AndroidRuntime(357):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-13 10:09:13.706: E/AndroidRuntime(357):  at dalvik.system.NativeStart.main(Native Method)
12-13 10:09:13.706: E/AndroidRuntime(357): Caused by: java.lang.NullPointerException
12-13 10:09:13.706: E/AndroidRuntime(357):  at com.kontak.latihan.KontakActivity.onCreate(KontakActivity.java:23)
12-13 10:09:13.706: E/AndroidRuntime(357):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-13 10:09:13.706: E/AndroidRuntime(357):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
12-13 10:09:13.706: E/AndroidRuntime(357):  ... 11 more
12-13 10:09:31.646: I/Process(357): Sending signal. PID: 357 SIG: 9

This is not work at first launch. Thanks for your attention. Very welcome for your help.

2 Answers2

0

form.getButtonDaftarKontak() seems to be returning a null. You should check that method.

Hint: always take a look at "Caused by" in the stack trace. That's the place where the real error is often signalized, while the top of the stack trace is just rest of the code failing because of the first exception.

David
  • 1,426
  • 17
  • 25
0

findViewById looks for a view with id mentioned in the current inflated layout. Your probably getting NPE while initializing of of your views.

I suggest you take a look at the docs.

http://developer.android.com/training/basics/firstapp/starting-activity.html

Button form;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_kontak);
    form = (Button)findViewById(R.id.buttonForm);
    // buttonForm is the id of button in activity_kontak
    form.setOnClickListener(this);

}

Also

public class KontakActivity extends Activity

and you have

Kontak kontak = new Kontak(); 

Check the answer by Raghav Sood in the below link. Also if you need to pass values from one Activity to another use intent.

Can i Create the object of a activity in other class?

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256