我正在开发一个应用程序,其中我有一个特定按钮的OnClickListener。当我按下按钮时,应该执行某种方法。 当我在onCreate块中编写onclicklistener时,一切正常。当我使用自定义OnClickListener在oncreate方法之外定义操作时,如果我点击Button,我的应用程序会崩溃!
以下是 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);
}
}
);
}
}
这个工作正常,只有我通过使用自定义侦听器更改它才会崩溃。使用自定义侦听器更改代码的代码如下:
.....
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);
}
};
我希望问题的描述有点可以理解......我觉得这与传递两个变量名称和gphArray有关...但我不明白为什么两者之间存在差异使用onClickListener的方法。
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)
提前感谢您的帮助! 干杯, 斯文
答案 0 :(得分:0)
这里有几个选项,但这可能是最简单的,应该可行:
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);
}
}