[英]Trouble with custom OnClickListener vs. in inLine OnClickListener
I am developing an app where I have an OnClickListener for a specific button. 我正在开发一个应用程序,其中有一个用于特定按钮的OnClickListener。 When i hit the button a certain method should be executed.
当我按下按钮时,应执行某种方法。 When i program the onclicklistener within the onCreate block everything works fine.
当我对onCreate块中的onclicklistener进行编程时,一切正常。 When I define the action outside of the oncreate method with a custom OnClickListener my app crashs if i hit the Button!
当我使用自定义的OnClickListener在oncreate方法之外定义动作时,如果我按下Button,我的应用程序将崩溃!
Heres the code for inLine Version: 以下是内联版本的代码:
public class OrcStopWatchActivity extends Activity {
public String[] names;
public String[][] gphArray;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_orc_stop_watch);
//set up the tabhost
TabHost th = (TabHost) findViewById (R.id.tabhost);
th.setup();
TabSpec specs = th.newTabSpec("tag1");
specs.setContent(R.id.tab1);
specs.setIndicator("Start");
th.addTab(specs);
specs = th.newTabSpec("tag2");
specs.setContent(R.id.tab2);
specs.setIndicator("Race");
th.addTab(specs);
specs = th.newTabSpec("tag3");
specs.setContent(R.id.tab3);
specs.setIndicator("Results");
th.addTab(specs);
stopBoat5Button = (Button)findViewById(R.id.btnStopBoat5);
textResult = (TextView)findViewById(R.id.textResult);
////////////////////////////////////////////////////////////////////////////////////////////////
String[][] gesamtArray = SvenMethods.makeArray(werteRow.txt);
//erstelle Vektor names
names = SvenMethods.extractColumn(gesamtArray,0);
// displayStringArray(names);
//erstelle Array mit den gph Werten als double Werte ohne Namen
gphArray = SvenMethods.deleteColumn(gesamtArray, 0);
// display2dStringArray(gphArray);
//on click action of the stop buttons
stopBoat5Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0){
stopBoat5Button.setEnabled(false);
String resultString=SvenMethods.calcCorrectedTimesTest(names, gphArray);
Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)",
Toast.LENGTH_LONG).show();
textResult.setText(resultString);
}
}
);
}
}
This one works fine, only if i change it by using a custom listener it crashs. 仅当我使用自定义侦听器更改它时,它才能崩溃。 The code changed code with custom listener is this:
使用自定义侦听器将代码更改为以下代码:
.....
stopBoat5Button.setOnClickListener(mStopListener5);
}
OnClickListener mStopListener5 = new OnClickListener() {
@Override
public void onClick(final View arg0) {
String resultString=SvenMethods.calcCorrectedTimesTest(names, gphArray);
Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)",
Toast.LENGTH_LONG).show();
textResult.setText(resultString);
}
};
I hope the description of the problem is somewhat understandable...i have the feeling that is has something to do with passing of the two variables names and gphArray...but I dont understand why there is a difference between the two methods of using the onClickListener. 我希望问题的描述是可以理解的...我感觉这与传递两个变量名称和gphArray有关...但是我不明白为什么两种使用方法之间存在差异onClickListener。
Logcat: Logcat:
01-28 11:22:38.229: D/TextLayoutCache(7668): Using debug level: 0 - Debug Enabled: 0
01-28 11:22:38.279: D/libEGL(7668): loaded /system/lib/egl/libGLES_android.so
01-28 11:22:38.289: D/libEGL(7668): loaded /system/lib/egl/libEGL_adreno200.so
01-28 11:22:38.309: D/libEGL(7668): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
01-28 11:22:38.309: D/libEGL(7668): loaded /system/lib/egl/libGLESv2_adreno200.so
01-28 11:22:38.399: D/OpenGLRenderer(7668): Enabling debug mode 0
01-28 11:22:38.469: D/dalvikvm(7668): GC_CONCURRENT freed 198K, 4% free 6648K/6919K, paused 4ms+4ms
01-28 11:22:46.770: D/AndroidRuntime(7668): Shutting down VM
01-28 11:22:46.770: W/dalvikvm(7668): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
01-28 11:22:46.779: E/AndroidRuntime(7668): FATAL EXCEPTION: main
01-28 11:22:46.779: E/AndroidRuntime(7668): java.lang.NullPointerException
01-28 11:22:46.779: E/AndroidRuntime(7668): at methods.SvenMethods.extractColumn(SvenMethods.java:442)
01-28 11:22:46.779: E/AndroidRuntime(7668): at methods.SvenMethods.lowestValue(SvenMethods.java:355)
01-28 11:22:46.779: E/AndroidRuntime(7668): at methods.SvenMethods.calcCorrectedTimesTest(SvenMethods.java:32)
01-28 11:22:46.779: E/AndroidRuntime(7668): at com.orcCalculation.OrcStopWatchActivity$7.onClick(OrcStopWatchActivity.java:328)
01-28 11:22:46.779: E/AndroidRuntime(7668): at android.view.View.performClick(View.java:3534)
01-28 11:22:46.779: E/AndroidRuntime(7668): at android.view.View$PerformClick.run(View.java:14263)
01-28 11:22:46.779: E/AndroidRuntime(7668): at android.os.Handler.handleCallback(Handler.java:605)
01-28 11:22:46.779: E/AndroidRuntime(7668): at android.os.Handler.dispatchMessage(Handler.java:92)
01-28 11:22:46.779: E/AndroidRuntime(7668): at android.os.Looper.loop(Looper.java:137)
01-28 11:22:46.779: E/AndroidRuntime(7668): at android.app.ActivityThread.main(ActivityThread.java:4441)
01-28 11:22:46.779: E/AndroidRuntime(7668): at java.lang.reflect.Method.invokeNative(Native Method)
01-28 11:22:46.779: E/AndroidRuntime(7668): at java.lang.reflect.Method.invoke(Method.java:511)
01-28 11:22:46.779: E/AndroidRuntime(7668): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-28 11:22:46.779: E/AndroidRuntime(7668): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-28 11:22:46.779: E/AndroidRuntime(7668): at dalvik.system.NativeStart.main(Native Method)
Thanks for any help in advance! 感谢您的任何帮助! Cheers, Sven
欢呼声,斯文
There are several options here, but this is likely the simplest and should work: 这里有几个选项,但这可能是最简单的,应该可以使用:
View.OnClickListener mStopListener5 = new View.OnClickListener() {
@Override
public void onClick(final View arg0) {
String resultString=SvenMethods.calcCorrectedTimesTest(names, gphArray);
Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)",
Toast.LENGTH_LONG).show();
textResult.setText(resultString);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.