Evorlor
Evorlor

Reputation: 7553

Android Text Display for Beginner

This is a very basic question, I know, but I cannot seem to figure it out on my own. While the Android tutorial will help you make a practice app, it does not really tell you what you are doing. (I have already successfully completed their tutorial.) All of my searches have come up with less newbish questions without answering my question. So here is my beginner's question:

I want to display the text "Sup World." What am I doing wrong? I am sure the null is wrong, but I cannot figure out what should actually be there. "this" does not work. And with null in there, setContextView does not exist.

package com.evorlor.testcode;

import android.widget.TextView;

public class SupWorld {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        String message = "Sup world.";

        TextView textView = new TextView(null);
        textView.setTextSize(40);
        textView.setText(message);

        setContentView(textView);

    }

}

My issue is not in getting to my SupWorld class, is it?:

package com.evorlor.testcode;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends Activity {

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

        Intent intent = new Intent(this, SupWorld.class);
        startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

Upvotes: 1

Views: 259

Answers (3)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

Change your Code as:

public class SupWorld extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

         String message = "Sup world.";

        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        setContentView(textView);

    }

}

Declares above activity class in AndroidManifest.xml :

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:label="@string/app_name"
        android:name=".MainActivity" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:label="@string/app_name"
        android:name=".SupWorld" >
    </activity>
</application>

apart this you will to look some good tutorials for Creating your first application in android see these

http://www.mkyong.com/android/android-activity-from-one-screen-to-another-screen/

http://developer.android.com/training/basics/firstapp/index.html

Upvotes: 2

jtt
jtt

Reputation: 13541

Instead of being given the answer, I think its best to explain why its failing. Basically this line is your culprit:

TextView textView = new TextView(null);

When looking at the constructor of a TextView, you'll see that it requires Context. Context is used in android for Attaching content the the respective application. In this case, Context is required for using the TextView as this TextView belongs to this application.

The correct way is passing context:

TextView textView = new TextView(this);

Or

TextView textView = new TextView(getApplicationContext());

Once you attach the respective Context, then it will work. null is not a valid context, and will throw a null pointer exception (NPE).

Try reading the documentation on TextView to learn more.

Also

the Other error that could have been caused is the Activity is not registered in your AndroidManifest.xml as ρяσѕρєя K pointed out. This is required so that the application knows to register that Activity underneath the Application you are trying to register it under.

Upvotes: 0

Android Killer
Android Killer

Reputation: 18489

First answer SEARCH THIS SIMPLE QUESTION

Second answer :

   public class SupWorld extends Activity {

    /**
     * @param args
     */
    public void onCreate(Bundle l) {
        // TODO Auto-generated method stub
        super.onCreate(l);

        String message = "Sup world.";

        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        setContentView(textView);

    }

  }

Add this activity to menifest and run

Upvotes: 0

Related Questions