0

I can not run my Android program because of button click function. Can anyone help me?

I have this code:

public class MainActivity extends ActionBarActivity {

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

        Button btn1 = (Button) findViewById(R.id.button1);
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {

        }       
        }); 

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }
}

when I comment out the Button code everything works

 public class MainActivity extends ActionBarActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            /*
            Button btn1 = (Button) findViewById(R.id.button1);
            btn1.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v)
                {

            }       
            }); */

            if (savedInstanceState == null) {
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.container, new PlaceholderFragment()).commit();
            }
        }
    }

If it is not in commentary program did not execute.

error message: Unfortunately, "programname" has stopped.

logcat errors:

06-06 06:21:11.950: E/AndroidRuntime(1866):     at android.os.Looper.loop(Looper.java:136)
06-06 08:37:02.183: E/AndroidRuntime(2380): FATAL EXCEPTION: main
06-06 08:37:02.183: E/AndroidRuntime(2380): Process: com.example.searchpersons, PID: 2380
06-06 08:37:02.183: E/AndroidRuntime(2380): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.searchpersons/com.example.searchpersons.MainActivity}: java.lang.NullPointerException
06-06 08:37:02.183: E/AndroidRuntime(2380):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-06 08:37:02.183: E/AndroidRuntime(2380):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-06 08:37:02.183: E/AndroidRuntime(2380):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-06 08:37:02.183: E/AndroidRuntime(2380):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-06 08:37:02.183: E/AndroidRuntime(2380):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-06 08:37:02.183: E/AndroidRuntime(2380):     at android.os.Looper.loop(Looper.java:136)
06-06 08:37:02.183: E/AndroidRuntime(2380):     at android.app.ActivityThread.main(ActivityThread.java:5017)
06-06 08:37:02.183: E/AndroidRuntime(2380):     at java.lang.reflect.Method.invokeNative(Native Method)
06-06 08:37:02.183: E/AndroidRuntime(2380):     at java.lang.reflect.Method.invoke(Method.java:515)
06-06 08:37:02.183: E/AndroidRuntime(2380):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-06 08:37:02.183: E/AndroidRuntime(2380):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-06 08:37:02.183: E/AndroidRuntime(2380):     at dalvik.system.NativeStart.main(Native Method)
06-06 08:37:02.183: E/AndroidRuntime(2380): Caused by: java.lang.NullPointerException
06-06 08:37:02.183: E/AndroidRuntime(2380):     at com.example.searchpersons.MainActivity.onCreate(MainActivity.java:41)
06-06 08:37:02.183: E/AndroidRuntime(2380):     at android.app.Activity.performCreate(Activity.java:5231)
06-06 08:37:02.183: E/AndroidRuntime(2380):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-06 08:37:02.183: E/AndroidRuntime(2380):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-06 08:37:02.183: E/AndroidRuntime(2380):     ... 11 more

I fix it ^_^. I insert my code in this function:

 public static class PlaceholderFragment extends Fragment { 
          public PlaceholderFragment() { } 
          @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
                 final View rootView = inflater.inflate(R.layout.fragment_main, container, false);          
                 Button btn1 = (Button) rootView.findViewById(R.id.button1);          
                 btn1.setOnClickListener(new View.OnClickListener() 
                 { 
                        public void onClick(View v) 
                        { }    
                 }       
                 return rootView;
           }
    } 

And everything works fine :)))

ann
  • 576
  • 1
  • 10
  • 19
  • What error are you seeing in logcat? – gwin003 Jun 06 '14 at 12:47
  • 1
    Likely buttton belongs to fragment layout. Initialize it in onCreate view of Fragment – Raghunandan Jun 06 '14 at 12:47
  • 2
    It's quite likely activity_main.xml does not have Button with id button1. Leaving you with a null pointer exception where you try to set onClickListener. – harism Jun 06 '14 at 12:49
  • Post the entire stacktrace. Also, it's probably because you are loading a fragment on top of the button, I'm not even sure if that button is loaded into your activity. You should post the activity_main.xml and the logcat. – EpicPandaForce Jun 06 '14 at 12:54
  • Is R.id.button1 in R.layout.activity_main or in some other file? – Chris Stratton Jun 06 '14 at 13:36

1 Answers1

1

Open up your activity_main.xml layout file, which you are setting as the layout in setContentView(R.layout.activity_main). Ensure that there is a button1 in that layout file, such as:

<Button
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:id="@+id/button1"
  android:text="Button"
/>

What is occuring is that the findViewById(R.id.button1) cannot find that View in your layout. So, make sure it is there. If you could post your activity_main.xml file, that would help us dianose the problem.

krodmannix
  • 845
  • 10
  • 30