Android 从另一个类中的 getter 方法设置文本

发布于 2025-01-03 06:53:28 字数 1248 浏览 0 评论 0原文

我已经查看了所有地方并采取了所有给出的提示,但我的应用程序仍然强制关闭。 该应用程序有两个类(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

亽野灬性zι浪 2025-01-10 06:53:28

您尝试在 onCreate() 调用完成之前初始化小部件。

在调用 setText 之前,在 onCreate() 方法中初始化 TextView,如下所示:

public class MainActivity extends Activity {
Next next;
TextView timesHit_txtview;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    next = new Next(); // Initialize Next

    timesHit_txtview = (TextView) findViewById(R.id.textView2); // Initialize Widget
    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);
        }

    });

}

在当前代码中,当您尝试为其设置文本时,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 call setText on it, like so:

public class MainActivity extends Activity {
Next next;
TextView timesHit_txtview;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    next = new Next(); // Initialize Next

    timesHit_txtview = (TextView) findViewById(R.id.textView2); // Initialize Widget
    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);
        }

    });

}

In your current code, timesHit_txtview is null when you try to set a text for it, which makes you Activity crash.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文