0

Hi guys, I started to learn developing app for android today. My guide is New Boston: https://buckysroom.org/videos.php?cat=6&video=16731 I passed every tutorial until the 10th because: I did every step, but the app crashes. Here is the XML code:

    <Button
        android:id="@+id/bAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvDisplay"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="Add here" />

    <TextView
        android:id="@+id/tvDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:text="The total is: " />

</RelativeLayout>

And here is the JAVA code:

package com.example.firstpro;



public class MainActivity extends ActionBarActivity {

int c = 0;
Button add, sub;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    add = (Button) findViewById(R.id.bAdd);
    display = (TextView) findViewById(R.id.tvDisplay);
    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            c+=1;
            display.setText("You're total is: " + c);
            setContentView(R.layout.activity_main);
        }
    });

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }
}

}

I really don't know what's the problem here. There is no error even warning. I hope for answers. Thanks.

azurefrog
  • 10,785
  • 7
  • 42
  • 56
iYonatan
  • 916
  • 3
  • 10
  • 26
  • 5
    "The app crashes." with what message? – Kon Jun 26 '14 at 20:27
  • Smells like http://stackoverflow.com/questions/23653778/nullpointerexception-accessing-views-in-oncreate – laalto Jun 26 '14 at 20:29
  • Hey Kon, check this out: http://prntscr.com/3wt2pl. – iYonatan Jun 26 '14 at 20:34
  • @iYonatan That's not the message he's talking about. He wants to know what the Logcat says. As to the issue, this is the big problem with using tutorial videos for learning new(ish) technologies. Wait 5 minutes and the video is out of date. This is the case here. The tutorial does not account for Android's recent template changes that put Fragments into everything. I suspect your issue has to do with the Fragment, but without seeing the Logcat, I cannot answer any more specifically. – Stephen S Jun 26 '14 at 20:56
  • Buckysroom link no longer works. – Kenster May 27 '15 at 17:00

1 Answers1

0

Your button and textview are in fragment layout, not in activity layout so you have to move your code to onCreateView:

public class MainActivity extends ActionBarActivity {

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

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    int c=0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
            false);

        Button add = (Button) rootView.findViewById(R.id.bAdd);
        TextView display = (TextView) rootView.findViewById(R.id.tvDisplay);

        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                c+=1;
                display.setText("You're total is: " + c);
                //you don't need to set same content view again
                //setContentView(R.layout.activity_main);
            }
        });

        return rootView;
    }
}
}
Zoran
  • 1,484
  • 1
  • 10
  • 13