Android 从另一个类中的 getter 方法设置文本
我已经查看了所有地方并采取了所有给出的提示,但我的应用程序仍然强制关闭。 该应用程序有两个类(Main 和 Next),“Next”类有一个 get 方法。当我尝试在“Main”类中使用 get 方法时,我的应用程序强制关闭。这是导致问题的代码行:
timesHit_txtview.setText(next.getTimesHit());
就像我之前提到的,我在 Eclipse 中没有收到任何错误。我从 log cat 得到的第一个错误是“未捕获的处理程序:线程主因未捕获的异常而退出”
有什么建议吗?提前致谢
编辑:
package com.whatever.main;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Next next = new Next();
TextView timesHit_txtview = (TextView) findViewById(R.id.textView2);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
timesHit_txtview.setText(5);
Button next_btn = (Button) findViewById(R.id.button1);
next_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent nextscreen_intent = new Intent(view.getContext(), Next.class);
startActivityForResult(nextscreen_intent, 0);
}
});
}
}
I've looked all over the place and taken all of the tips given, but my app still force closes.
The app has two classes (Main and Next) and "Next" class has a get method. When I try to use the get method in the "Main" class my app force closes. This is the line of code that causes the problems:
timesHit_txtview.setText(next.getTimesHit());
Like I mentioned before, I don't get any errors in eclipse. The First error I get from log cat is "Uncaught handler: thread main exiting due to uncaught exception"
Any suggestions? Thanks in advance
EDIT:
package com.whatever.main;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Next next = new Next();
TextView timesHit_txtview = (TextView) findViewById(R.id.textView2);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
timesHit_txtview.setText(5);
Button next_btn = (Button) findViewById(R.id.button1);
next_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent nextscreen_intent = new Intent(view.getContext(), Next.class);
startActivityForResult(nextscreen_intent, 0);
}
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试在
onCreate()
调用完成之前初始化小部件。在调用
setText
之前,在onCreate()
方法中初始化 TextView,如下所示:在当前代码中,当您尝试为其设置文本时,timesHit_txtview 为 null ,这会让你的 Activity 崩溃。
You're trying to initialize a widget before the
onCreate()
call has been completed.Initialize your TextView in the
onCreate()
method before you callsetText
on it, like so:In your current code, timesHit_txtview is null when you try to set a text for it, which makes you Activity crash.