将 onLongClick 与 setText 一起使用时出现类文件错误
我创建了这个小程序来尝试学习按钮和文本视图。当尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 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).