• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Debugging help. Force Close. Very Simple app.

brade

Lurker
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:orientation="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?!!​
 
You need to call setContentView() before you call findViewById(). Otherwise, findViewById() returns null and you get a NullPointerException when you try to call setOnClickListener().
 
Back
Top Bottom