Java Android在TextView中更改文本
我正试图使用setText()更改文本视图中的文本。 当我尝试执行此操作时,会出现以下错误:Java Android在TextView中更改文本,java,android,nullpointerexception,textview,settext,Java,Android,Nullpointerexception,Textview,Settext,我正试图使用setText()更改文本视图中的文本。 当我尝试执行此操作时,会出现以下错误: E/AndroidRuntime﹕ 致命异常:主 java.lang.RuntimeException:无法启动活动组件信息{}:java.lang.NullPointerException 原因:java.lang.NullPointerException 在com.training.mytraining.test.MainActivity.display上(MainActivity.java:35)
E/AndroidRuntime﹕ 致命异常:主
java.lang.RuntimeException:无法启动活动组件信息{}:java.lang.NullPointerException
原因:java.lang.NullPointerException
在com.training.mytraining.test.MainActivity.display上(MainActivity.java:35)
MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView tvCountry;
private Server theServer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvCountry = (TextView) findViewById(R.id.countryTextView);
theServer = new Server(this);
theServer.weatherFromYahoo();
}
public void display() {
tvCountry.setText("USA");
}
}
public class Server extends AppCompatActivity {
MainActivity theMainActivity;
//Constructor
public Server (Context context){
this.theMainActivity = new MainActivity();
}
public void weatherFromYahoo() {
theMainActivity.display();
}
Server.java
public class MainActivity extends AppCompatActivity {
TextView tvCountry;
private Server theServer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvCountry = (TextView) findViewById(R.id.countryTextView);
theServer = new Server(this);
theServer.weatherFromYahoo();
}
public void display() {
tvCountry.setText("USA");
}
}
public class Server extends AppCompatActivity {
MainActivity theMainActivity;
//Constructor
public Server (Context context){
this.theMainActivity = new MainActivity();
}
public void weatherFromYahoo() {
theMainActivity.display();
}
}
活动\u main.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:id="@+id/countryTextView"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
这只是我真实代码的一个示例。我不想把所有的代码都放进去,所以我就针对我遇到的问题做了一个新的代码。我必须通过从另一个类调用MainActivity来设置文本
请帮帮我,我已经被困在这里好几个小时了。改变这个:
public Server (Context context){
this.theMainActivity = new MainActivity();
}
为此:
public Server (Context context){
this.theMainActivity = (MainActivity)context;
}
这样onCreate仍然可以正确调用。如果您正在新建一个活动,它将无法正确设置视图并遵循正确的生命周期。正如@commonware所说,您永远不应该通过其构造函数创建活动。永远不应该通过其构造函数创建活动(例如,
new MainActivity()
)。示例中没有MainActivity.java:35
。可能重复: