1

Okay, I've got the makings of a really simple Android program here. I decided to start out with something simple but also relatively useful. Eventually, I hope to have an application that stores and displays study cards. I feel like not being able to get past a TextView is a bad omen. I've looked at several dozen examples and researched common problems new users have with setText(), but to no avail. Here are the relevant lines of code:

From my .java file:

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

public class MainActivity extends Activity {

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

TextView tv1 = (TextView) findViewById(R.id.TextView1);
tv1.setText("New text");

.... and so on

Eclipse is marking the line containing "setText" and giving me the following syntactical errors:

Syntax error on token(s), misplaced construct(s) //underlining the period after tv1

Syntax error on token ""New text"", delete this token

4

6 回答 6

3

将 textview 的初始化移动到onCreate.

TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.TextView1); 
// initialize after settting layout to activtiy
tv1.setText("New text");
// the above statement must be within a method 
}

您的初始化textview在任何方法之外。您应该仅在将布局设置为活动后初始化您的文本视图。所以 textview 在onCreate. 此 tv1.setText("New text")语句必须在方法内。

初始化你的textview内部活动onCreate会更好。

于 2013-11-08T07:20:27.423 回答
0
just because you initailize textview locally and set the textview 
outside onCreate method,you got this error.define textview globally solve ur issue;

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

public class MainActivity extends Activity {
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

tv1 = (TextView) findViewById(R.id.TextView1);
tv1.setText("New text");
}
于 2013-11-08T07:27:26.947 回答
0

将 TextView 对象移动到 onCreate() 方法中,然后对其调用 setText 方法。这是您所需的代码。

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   TextView tv1 = (TextView) findViewById(R.id.TextView1);
    tv1.setText("New text");
    } 
于 2013-11-08T07:46:15.583 回答
0
public class MainActivity extends Activity {

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

    TextView tv1 = (TextView) findViewById(R.id.TextView1);
    tv1.setText("New text");

}

将您的代码放在 oncreate 方法中。

于 2013-11-08T07:21:16.097 回答
0
// try this
public class MainActivity extends Activity {
        TextView tv1;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tv1 = (TextView) findViewById(R.id.TextView1);
            tv1.setText("New text");
        }
}
于 2013-11-08T07:22:47.530 回答
0
TextView tv1 = (TextView) findViewById(R.id.TextView1);
tv1.setText("New text");

总是在 onCreate , onresume 等方法中使用这种东西

于 2013-11-08T07:23:42.410 回答