将 onLongClick 与 setText 一起使用时出现类文件错误

发布于 2024-11-14 18:41:30 字数 1755 浏览 6 评论 0原文

我创建了这个小程序来尝试学习按钮和文本视图。当尝试使用 onLongClick 来 setText 时,我强制关闭。当我查看调试器时,我在第 45 行看到一个 nullpointerException 持有MeAnswer.setText("Nope!");。

我在空指针下面看到一大堆“类文件错误:未找到源”错误。我尝试将附加源指向 Java src.zip 和 android.jar 文件,但似乎都没有修复任何问题。

代码是:

package com.PickSomeButtons;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Button;
import android.view.View.OnLongClickListener;

public class PickSomeButtons extends Activity {

    RadioButton myButton0;
    RadioButton myButton1;
    TextView myAnswer;
    TextView holdMeAnswer;
    Button longClickButton;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        myButton0=(RadioButton)findViewById(R.id.radio0);
        myButton1=(RadioButton)findViewById(R.id.radio1);
        myAnswer=(TextView)findViewById(R.id.textView1);
        longClickButton=(Button)findViewById(R.id.button1);

        myButton0.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                myAnswer.setText("Me!");                
            }
        });
        myButton1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                myAnswer.setText("Not me!");
            }
        });

        longClickButton.setOnLongClickListener(new Button.OnLongClickListener() {

            public boolean onLongClick(View v) {
                holdMeAnswer.setText("Nope!");
                return true;
            }
        });
    }
}

I created this little program to try and learn buttons and textviews. When attempting to use the onLongClick to setText, I get a force close. When I look at the debugger I see a nullpointerexception at line 45 holdMeAnswer.setText("Nope!");.

I see a whole bunch of "Class file error: Source not found" errors underneath the nullpointer. I've tried pointing the attached source to both the Java src.zip and the android.jar files but neither seems to fix anything.

Code is:

package com.PickSomeButtons;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Button;
import android.view.View.OnLongClickListener;

public class PickSomeButtons extends Activity {

    RadioButton myButton0;
    RadioButton myButton1;
    TextView myAnswer;
    TextView holdMeAnswer;
    Button longClickButton;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        myButton0=(RadioButton)findViewById(R.id.radio0);
        myButton1=(RadioButton)findViewById(R.id.radio1);
        myAnswer=(TextView)findViewById(R.id.textView1);
        longClickButton=(Button)findViewById(R.id.button1);

        myButton0.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                myAnswer.setText("Me!");                
            }
        });
        myButton1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                myAnswer.setText("Not me!");
            }
        });

        longClickButton.setOnLongClickListener(new Button.OnLongClickListener() {

            public boolean onLongClick(View v) {
                holdMeAnswer.setText("Nope!");
                return true;
            }
        });
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

︶ ̄淡然 2024-11-21 18:41:30

在 onCreate 方法中,您初始化 myAnswer 但不初始化holdMeAnswer。因此holdMeAnswer 为空。

您可以尝试在 Eclipse 中的 Preferences > 下启用更多警告。爪哇>编译器>错误/警告。我认为可能会对未初始化的私有成员发出警告(顺便说一句,这些成员可能应该是私有的)。

In your onCreate method you initialize myAnswer but not holdMeAnswer. Thus holdMeAnswer is null.

You might try enabling more of the warnings in Eclipse under Preferences > Java > Compiler > Errors/Warnings. I think there might be a warning for uninitialized private members (as an aside, those members should probably be private).

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