1

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>
4

3 回答 3

2

改成

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查找具有当前膨胀布局中提到的 id 的视图。所以你需要为活动设置布局,然后初始化你的视图。

也改变

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

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

编辑:

您的方法签名不同。它应该是

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

因为你有

android:onClick="getRandomNumber"

从文档中引用

public static final int onClick

单击视图时要在此视图的上下文中调用的方法的名称。此名称必须对应于只采用一个 View 类型参数的公共方法。例如,如果您指定 android:onClick="sayHello",则必须声明您的上下文(通常是您的 Activity)的 public void sayHello(View v) 方法

于 2013-11-12T15:58:23.050 回答
0

在 setText 之后设置文本颜色并为保证设置字体大小。有时它使颜色默认为白色。

于 2013-11-13T12:47:57.113 回答
0

将您的代码更改为此。

TextView text;

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

    text = (TextView) findViewById(R.id.number);
}

text.setText(String.valueOf(i)); 

注意: findViewByID 从内容中查找视图。所以基本上你必须在设置内容后使用该方法。

于 2013-11-12T15:58:52.887 回答