1

私は最初のAndroidアプリをプログラミングしています。にテキストを設定したいTextView。(私はすでにここで検索して、それを行う方法を見つけました...しかし、それでも機能しません)

何が問題なのか知っていますか?

これは私のJavaコードです。

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

}

これは私の XML コードです

<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 型のパラメーターを 1 つだけ取る public メソッドに対応している必要があります。たとえば、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 に答える