-3

I am creating a mobile app on Eclipse Juno, there are no errors in the code of the different classes and it will run on the emulator. However when The first screen loads on the emulator and I select either of the buttons the app stops running and I get an error in the log cat about a NullPointerException but can't seem to find where this problem is. Does anyone have any ideas what I am doing wrong or what is causing the app to stop?

The purpose of the buttons on the first screen is to take the user to different areas of the app so without getting to select an area to go I can't do much with the app.

This is the code for my first screen: package com.example.independentretailers; import com.example.independentretailers.R; import android.support.v7.app.ActionBarActivity; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.content.*;

public class welcome extends Activity {


public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
addListenerOnButton();
addListenerOnButton1();
}

//to continue from welcome screen to customer login screen 
//when customer button is clicked 
public void addListenerOnButton() { 
Button CustomerButton = (Button) findViewById(R.id.button1);

CustomerButton.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v){
        Intent intent = new Intent   (getApplicationContext(),customerlogin.class);       
            startActivity(intent);

        }
    });
}

//to continue from welcome screen to retailer login screen 
//when retailer button is clicked 
public void addListenerOnButton1() { 
    Button RetailerButton = (Button) findViewById(R.id.button2);

    RetailerButton.setOnClickListener(new OnClickListener(){    

        @Override
        public void onClick(View v){
            Intent intent = new Intent (getApplicationContext(),retailerlogin.class);       
            startActivity(intent);

        }
    });

}
}

My Log Cat Error is:

03-30 18:56:19.716: E/ResourceType(536): Style contains key with bad entry: 0x01010479
03-30 18:56:19.806: I/dalvikvm(536): threadid=3: reacting to signal 3
03-30 18:56:19.876: I/dalvikvm(536): Wrote stack traces to '/data/anr/traces.txt'
03-30 18:56:19.996: D/gralloc_goldfish(536): Emulator without GPU emulation detected.
03-30 18:58:31.936: E/ResourceType(536): Style contains key with bad entry: 0x01010479
03-30 18:58:31.966: D/AndroidRuntime(536): Shutting down VM
03-30 18:58:31.966: W/dalvikvm(536): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
03-30 18:58:32.016: E/AndroidRuntime(536): FATAL EXCEPTION: main
03-30 18:58:32.016: E/AndroidRuntime(536): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.independentretailers/com.example.independentretailers.customerlogin}: java.lang.NullPointerException
03-30 18:58:32.016: E/AndroidRuntime(536):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
03-30 18:58:32.016: E/AndroidRuntime(536):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
03-30 18:58:32.016: E/AndroidRuntime(536):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
03-30 18:58:32.016: E/AndroidRuntime(536):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
03-30 18:58:32.016: E/AndroidRuntime(536):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-30 18:58:32.016: E/AndroidRuntime(536):  at android.os.Looper.loop(Looper.java:137)
03-30 18:58:32.016: E/AndroidRuntime(536):  at android.app.ActivityThread.main(ActivityThread.java:4424)
03-30 18:58:32.016: E/AndroidRuntime(536):  at java.lang.reflect.Method.invokeNative(Native Method)
03-30 18:58:32.016: E/AndroidRuntime(536):  at java.lang.reflect.Method.invoke(Method.java:511)
03-30 18:58:32.016: E/AndroidRuntime(536):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-30 18:58:32.016: E/AndroidRuntime(536):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-30 18:58:32.016: E/AndroidRuntime(536):  at dalvik.system.NativeStart.main(Native Method)
03-30 18:58:32.016: E/AndroidRuntime(536): Caused by: java.lang.NullPointerException
03-30 18:58:32.016: E/AndroidRuntime(536):  at android.view.ViewGroup.addViewInner(ViewGroup.java:3336)
03-30 18:58:32.016: E/AndroidRuntime(536):  at android.view.ViewGroup.addView(ViewGroup.java:3208)
03-30 18:58:32.016: E/AndroidRuntime(536):  at android.view.ViewGroup.addView(ViewGroup.java:3188)
03-30 18:58:32.016: E/AndroidRuntime(536):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
03-30 18:58:32.016: E/AndroidRuntime(536):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:260)
03-30 18:58:32.016: E/AndroidRuntime(536):  at android.app.Activity.setContentView(Activity.java:1855)
03-30 18:58:32.016: E/AndroidRuntime(536):  at com.example.independentretailers.customerlogin.onCreate(customerlogin.java:28)
03-30 18:58:32.016: E/AndroidRuntime(536):  at android.app.Activity.performCreate(Activity.java:4465)
03-30 18:58:32.016: E/AndroidRuntime(536):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-30 18:58:32.016: E/AndroidRuntime(536):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
03-30 18:58:32.016: E/AndroidRuntime(536):  ... 11 more
03-30 18:58:32.405: I/dalvikvm(536): threadid=3: reacting to signal 3
03-30 18:58:32.445: I/dalvikvm(536): Wrote stack traces to '/data/anr/traces.txt'
03-30 18:58:32.625: I/dalvikvm(536): threadid=3: reacting to signal 3
03-30 18:58:32.655: I/dalvikvm(536): Wrote stack traces to '/data/anr/traces.txt'

Any help at all would be greatly appreciated.

kenju
  • 5,866
  • 1
  • 41
  • 41
Tmcc
  • 1
  • 1
  • 5

1 Answers1

0

I think you don't have to nest one button click listener into another button's onClickListener. Also you declared buttons as instance variables and didn't initialize them, then you declared local buttons with same names inside your methods. Remove either of them to get them work. Try the below code.

public class welcome extends Activity {
    //Button CustomerButton; No need to declare these here.
    //Button RetailerButton;

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);
        addListenerOnButton();
        addListenerOnButton1();
    }

    //to continue from welcome screen to customer login screen 
    //when customer button is clicked 
    public void addListenerOnButton() { 
        Button CustomerButton = (Button) findViewById(R.id.button1);

        CustomerButton.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v){
                Intent intent = new Intent (getApplicationContext(),customerlogin.class);       
                startActivity(intent);

            }
        });
    }

    //to continue from welcome screen to retailer login screen 
    //when retailer button is clicked 
    public void addListenerOnButton1() { //This method was inside the first method earlier.
        Button RetailerButton = (Button) findViewById(R.id.button2);

        RetailerButton.setOnClickListener(new OnClickListener(){    

            @Override
            public void onClick(View v){
                Intent intent = new Intent (getApplicationContext(),retailerlogin.class);       
                startActivity(intent);

            }
        });

    }
}
Prudhvi
  • 2,276
  • 7
  • 34
  • 54