Question

My app has a first activity with a Listview and then 4 buttons. Initially, before the user has selected an item from the listview, I only want 2 of the buttons to be enabled, so I've got a little routine to do that and it works fine.

I have got a second activity (child to the first one) with exactly the same structure and I've cut and pasted the code from the first activity (changing names to make it clearer) BUT in my second activity, the findViewById call returns null for the buttons.

Lots of code so tried to just extract the relevant stuff (ellipses signify lines removed for clarity):

public class DisplayTripsActivity extends FirstScreenActivity {
...
    protected void onCreate(Bundle savedInstanceState) {
...
    setContentView(R.layout.activity_trip_display);
    togBtns(false);

public void togBtns(Boolean turnOn) {
    Button butt1 = (Button) findViewById(R.id.edTripBtn);
    Button butt2 = (Button) findViewById(R.id.delTripBtn);

    if (turnOn) {
        butt1.setEnabled(true);
        butt2.setEnabled(true);
    } else {
        butt1.setEnabled(false);  <--- null pointer exception here
        butt2.setEnabled(false);
    }

The XML file for the buttons is this:

    <Button
        android:id="@+id/edTripBtn"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:text="Edit"
        android:onClick="editTrip" />

    <Button
        android:id="@+id/delTripBtn"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:text="Delete"
        android:onClick="deleteTrip" />

Like I say, in the first activity, the exact same code works fine - is this because the second activity extends the first maybe?

Any ideas appreciated.

EDIT: LogCat extract:

05-13 11:21:37.872: E/AndroidRuntime(1426): FATAL EXCEPTION: main
05-13 11:21:37.872: E/AndroidRuntime(1426): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.DisplayTripsActivity}: java.lang.NullPointerException
05-13 11:21:37.872: E/AndroidRuntime(1426):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
05-13 11:21:37.872: E/AndroidRuntime(1426):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
....
05-13 11:21:37.872: E/AndroidRuntime(1426): Caused by: java.lang.NullPointerException
05-13 11:21:37.872: E/AndroidRuntime(1426):     at com.example.myfirstapp.DisplayTripsActivity.togBtns(DisplayTripsActivity.java:87)
Was it helpful?

Solution 4

OK, I think the problem is caused by the fact that my first activity extends ListActivity BUT my second activity (which extends the first and thus also ListActivity) does NOT have a ListView in it.

So even though the error is a NPE on my buttons, that is a red herring - the actual error is:

05-14 07:17:04.819: E/AndroidRuntime(1296): FATAL EXCEPTION: main
05-14 07:17:04.819: E/AndroidRuntime(1296): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.createNewVehicleActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

...which I get when I commented out the 'togBtns' function and tried using another button on the same activity.

Great - complete re-design coming up... :-(

Thank you all for your suggestions...

OTHER TIPS

Try:

 protected void onCreate(Bundle savedInstanceState) {
 ...
 Button butt1 = (Button) findViewById(R.id.edTripBtn);
 Button butt2 = (Button) findViewById(R.id.delTripBtn);
 setContentView(R.layout.activity_trip_display);
 togBtns(false);

public void togBtns(Boolean turnOn) {

if (turnOn) {
    butt1.setEnabled(true);
    butt2.setEnabled(true);
} else {
    butt1.setEnabled(false); 
    butt2.setEnabled(false);
}

try declaring the Buttons at the top of the class like Button butt1=null... and inside of OnCreate() butt1=(Button)findViewById(your button id), must work. regards

Are you inheriting a lot of data from your first activity to your second? You could just pass any data through your intent object unless it's a lot then you would probably want a central helper class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top