TextView failure Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of TextView failure without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m programming my first android app. I want to set text on a TextView. (I already searched here and found out, how to do it…But it still don’t work)

Do you know, what the problem is?

This is my Java code.

package com.example.randomcraps;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

import com.example.*;

public class MainActivity extends Activity {

    TextView text = (TextView) findViewById(R.layout.number);

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


    @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;
    }

    public void getRandomNumber(){
        int i;
        double savePoint;
        savePoint = Math.random();
        savePoint = savePoint*100+1;
        i = (int)savePoint;
        text.setText(i);
    }

}

This is my XML Code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="26dp"
        android:text="@string/Button1"
        android:onClick="getRandomNumber" />

    <TextView
        android:id="@+id/number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/RandomNumber" />

</RelativeLayout>

Answer

Change to

TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   text = (TextView) findViewById(R.id.number); // id of textview in activity_main.xml
 }

findViewById looks for a view with the id mentioned in the current inflated layout. So you need to set layout to the activity and then initialize your views.

Also change

text.setText(i); 
// looks for a resource with the id mentioned if not found you get ResourceNotFoundException

To

text.setText(String.valueOf(i)); 
// int i; i is an int value. Use String.valueOf(intvalue)

Edit:

Your method signature is different. It should be

public void getRandomNumber(View V){ // missing View as param
        ... //rest of the code
    }

Coz you have

android:onClick="getRandomNumber"

Quoting from docs

public static final int onClick

Name of the method in this View’s context to invoke when the view is clicked. This name must correspond to a public method that takes exactly one parameter of type View. For instance, if you specify android:onClick=”sayHello”, you must declare a public void sayHello(View v) method of your context (typically, your Activity).

We are here to answer your question about TextView failure - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji