我在将OnclickListener分配给动态创建的按钮时出现了一些错误
这是我的代码:
public void getTableList(LinearLayout layout, String s) {
Log.e("in getTableList", "in getTableList");
b = new Button(shuddhContext);
b.setText(s);
b.setTextSize(15);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.setMargins(5, 5, 5, 5);
b.setBackgroundResource(R.drawable.class_button);
b.setLayoutParams(params);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(
"com.shuddhatm.attendencemonitor.STUDENTLIST");
String className = b.getText().toString();
myIntent.putExtra("className", className);
try{
startActivity(myIntent);
}catch(Exception e){
Toast.makeText(shuddhContext, e.toString() + "\n" + b.getText(), Toast.LENGTH_LONG).show();
}
}
});
layout.addView(b);
}
单击按钮时的logcat( with try / catch block )
04-13 23:32:38.561: D/memalloc(7243): ion: Unmapping buffer base:0x5305a000 size:245760
04-13 23:32:38.561: D/memalloc(7243): ion: Unmapping buffer base:0x50281000 size:4096
04-13 23:32:41.331: E/button Click(7243): step1
04-13 23:32:41.331: E/button Click(7243): step2
04-13 23:32:41.331: E/button Click(7243): step3
04-13 23:32:41.331: E/button Click(7243): step4
04-13 23:32:41.381: D/memalloc(7243): ion: Mapped buffer base:0x5305a000 size:245760 offset:0 fd:74
04-13 23:32:41.391: D/memalloc(7243): ion: Mapped buffer base:0x50281000 size:4096 offset:0 fd:76
04-13 23:32:42.931: E/button Click(7243): step1
04-13 23:32:42.931: E/button Click(7243): step2
04-13 23:32:42.931: E/button Click(7243): step3
04-13 23:32:42.931: E/button Click(7243): step4
04-13 23:32:44.881: D/memalloc(7243): ion: Unmapping buffer base:0x5305a000 size:245760
04-13 23:32:44.881: D/memalloc(7243): ion: Unmapping buffer base:0x50281000 size:4096
04-13 23:32:44.931: D/memalloc(7243): ion: Mapped buffer base:0x5305a000 size:245760 offset:0 fd:74
04-13 23:32:44.931: D/memalloc(7243): ion: Mapped buffer base:0x50281000 size:4096 offset:0 fd:76
04-13 23:32:48.371: D/memalloc(7243): ion: Unmapping buffer base:0x5305a000 size:245760
04-13 23:32:48.371: D/memalloc(7243): ion: Unmapping buffer base:0x50281000 size:4096
单击按钮时04-13 23:48:50.901: D/memalloc(8869): ion: Mapped buffer base:0x53ef4000 size:1536000 offset:0 fd:65
04-13 23:48:50.901: D/memalloc(8869): ion: Mapped buffer base:0x4006f000 size:4096 offset:0 fd:68
04-13 23:48:50.981: E/button Click(8869): step1
04-13 23:48:50.981: E/button Click(8869): step2
04-13 23:48:50.981: E/button Click(8869): step3
04-13 23:48:50.981: E/button Click(8869): step4
04-13 23:48:50.981: W/dalvikvm(8869): threadid=1: thread exiting with uncaught exception (group=0x41275540)
04-13 23:48:51.041: E/AndroidRuntime(8869): FATAL EXCEPTION: main
04-13 23:48:51.041: E/AndroidRuntime(8869): java.lang.NullPointerException
04-13 23:48:51.041: E/AndroidRuntime(8869): at android.app.Activity.startActivityForResult(Activity.java:3409)
04-13 23:48:51.041: E/AndroidRuntime(8869): at android.app.Activity.startActivityForResult(Activity.java:3370)
04-13 23:48:51.041: E/AndroidRuntime(8869): at android.app.Activity.startActivity(Activity.java:3580)
04-13 23:48:51.041: E/AndroidRuntime(8869): at android.app.Activity.startActivity(Activity.java:3548)
04-13 23:48:51.041: E/AndroidRuntime(8869): at com.shuddhatm.attendancemonitor.AMData$1.onClick(AMData.java:142)
04-13 23:48:51.041: E/AndroidRuntime(8869): at android.view.View.performClick(View.java:4102)
04-13 23:48:51.041: E/AndroidRuntime(8869): at android.view.View$PerformClick.run(View.java:17085)
04-13 23:48:51.041: E/AndroidRuntime(8869): at android.os.Handler.handleCallback(Handler.java:615)
04-13 23:48:51.041: E/AndroidRuntime(8869): at android.os.Handler.dispatchMessage(Handler.java:92)
04-13 23:48:51.041: E/AndroidRuntime(8869): at android.os.Looper.loop(Looper.java:155)<br />
04-13 23:48:51.041: E/AndroidRuntime(8869): at android.app.ActivityThread.main(ActivityThread.java:5520)
04-13 23:48:51.041: E/AndroidRuntime(8869): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 23:48:51.041: E/AndroidRuntime(8869): at java.lang.reflect.Method.invoke(Method.java:511)
04-13 23:48:51.041: E/AndroidRuntime(8869): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
04-13 23:48:51.041: E/AndroidRuntime(8869): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
04-13 23:48:51.041: E/AndroidRuntime(8869): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
我认为这里的主要问题是尝试获取Button
点击的文字,而b
方法中的onClick
变量无法访问它如果它不是final
。为避免这种情况,您可以按如下方式检索单击的视图(因为此处为b == v
):
@Override
public void onClick(View v) {
// b == v
String className = v.getText().toString();
}
另一个问题可能是您的Intent
声明:
Intent myIntent = new Intent("com.shuddhatm.attendencemonitor.STUDENTLIST");
我猜“ com.shuddhatm ... STUDENTLIST ”是新Activity
,我建议您按照以下步骤操作:
Intent intent = new Intent(AMData.this, STUDENTLIST.class);
According to the documentation:
这里使用的构造函数有两个参数:
- Context 作为其第一个参数(因为Activity类是Context的子类,所以使用它)
- 系统应向其传递Intent的应用程序组件的类(在这种情况下,应该启动的活动)
然后,因为AMData
和STUDENTLIST
在同一个包中,您可以避免写出该类的全名。
最后,确保shuddhContext
返回正确的上下文。如果没有,您应该使用AMData.this
。你可以避免使用try/catch
Intent
方法,我认为这是没用的。
希望这有帮助。