1. Download our Official Android App: Forums for Android!

Apps Debugging help. Force Close. Very Simple app.

Discussion in 'Android Development' started by brade, May 11, 2010.

  1. brade

    brade Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    6
    Joined:
    May 11, 2010

    May 11, 2010
    6
    0
    5
    I'm just starting out, trying to get my feet wet...

    Here's my xml:


    <?xmlversion="1.0"encoding="utf-8"?>

    <
    LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    android:eek:rientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >



    <TextViewandroid:text="Press the Button"

    android:id="@+id/text01"
    android:layout_marginLeft = "90dip"
    android:layout_marginTop = "150dip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    </TextView>


    <Button
    android:text="The Button"

    android:id="@+id/Button01"
    android:layout_margin="100dip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    </Button>

    </LinearLayout>


    And here's my java:



    package com.brian.first_try;


    import android.app.Activity;

    import android.os.Bundle;

    import android.view.View;

    import android.widget.Button;

    import android.widget.TextView;


    public class first_try extends Activity

    {
    /** Called when the activity is first created. */
    @Override
    publicvoid onCreate(Bundle savedInstanceState)
    {
    final TextView myText = (TextView) findViewById(R.id.text01);
    final Button myButton = (Button) findViewById(R.id.Button01);
    super.onCreate(savedInstanceState);
    // myButton.setOnClickListener(new View.OnClickListener()
    // {
    // public void onClick(View v)
    // {
    // myText.setText("Great Job!");
    // }
    // });



    setContentView(R.layout.


    main);

    }
    }

    It runs fine until I remove the comment //'s. What Am I doing wrong?!!​
     

    Advertisement

  2. dmitriyk

    dmitriyk Member
    Rank:
    None
    Points:
    18
    Posts:
    44
    Joined:
    Apr 17, 2010

    Apr 17, 2010
    44
    10
    18
    Software Developer
    Salt Lake City, UT
    You need to call setContentView() before you call findViewById(). Otherwise, findViewById() returns null and you get a NullPointerException when you try to call setOnClickListener().
     
    brade likes this.
  3. brade

    brade Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    6
    Joined:
    May 11, 2010

    May 11, 2010
    6
    0
    5
    Thank you so much. I was freaking out for hours.... lol. thanks again. (Yay, my button works!)
     

Share This Page

Loading...