Debugging help. Force Close. Very Simple app.


Last Updated:

  1. brade

    brade Member This Topic's Starter

    Joined:
    May 11, 2010
    Messages:
    6
    Likes Received:
    0
    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 Active Member

    Joined:
    Apr 17, 2010
    Messages:
    44
    Likes Received:
    10
    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 Member This Topic's Starter

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

Share This Page

Loading...