我认为我的代码可能写错了,这导致了我的问题。基本上我正在尝试创建多个“程序”按钮(这些按钮由相对视图、线性视图、2 个文本视图、2 个按钮组成)。你可以看到下面的代码。我在 XML 中创建了这个结构,当我想要将新程序添加到应用程序时,我正在扩展该 XML。然后,我根据数据库唯一 ID 添加 ID,并将其分配给此按钮的所有创建视图。
它似乎工作正常,用于单击按钮等。问题是当我试图销毁一个按钮时,我的应用程序不断崩溃。现在我开始怀疑我选择的方法是否存在根本错误。
public void buildprogramholders(String rowID, String title_text, String description_text) {
int dbRowID = Integer.parseInt(rowID);
totalTid++;
LinearLayout ll = (LinearLayout)findViewById(R.id.traininglist);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.trainingprogram, ll);
tp_container = new RelativeLayout(this);
tp_container = (RelativeLayout) findViewById(R.id.tp_container);
tp_container.setId(dbRowID);
tp_icon = new ImageView(this);
tp_icon = (ImageView) findViewById(R.id.tp_icon);
tp_icon.setId(dbRowID);
tp_textholder = new LinearLayout(this);
tp_textholder = (LinearLayout)findViewById(R.id.tp_textholder);
tp_textholder.setId(dbRowID);
//tp_title = new TextView(this);
tp_title = (TextView)findViewById(R.id.tp_title);
tp_title.setId(dbRowID);
tp_title.setText(title_text);
tp_description = new TextView(this);
tp_description = (TextView)findViewById(R.id.tp_description);
tp_description.setId(dbRowID);
tp_description.setText(description_text);
tp_button = new Button(this);
tp_button = (Button)findViewById(R.id.tp_button);
tp_button.setId(dbRowID);
tp_button.setTag(3);
tp_button.setOnClickListener(this);
tp_delete_button = new Button(this);
tp_delete_button = (Button)findViewById(R.id.tp_delete_button);
tp_delete_button.setId(dbRowID);
tp_delete_button.setTag(6);
tp_delete_button.setOnClickListener(this);
}
然后在 OnClick 我尝试删除该程序。我尝试注释掉整个 drop_tp_xx.setVisibitly(View.gone) 区域并且程序有效。如果我从那里恢复任何单行,我的程序就会变得不可靠。有时我可以删除一两个,然后它崩溃了?
public void onClick(View v) {
int rowId = v.getId();
RelativeLayout drop_tp_container = (RelativeLayout) tp_container.findViewById(rowId);
ImageView drop_tp_icon = (ImageView) tp_icon.findViewById(rowId);
LinearLayout drop_tp_textholder = (LinearLayout) tp_textholder.findViewById(rowId);
TextView drop_tp_title = (TextView) tp_title.findViewById(rowId);
TextView drop_tp_description = (TextView) tp_description.findViewById(rowId);
Button drop_tp_button = (Button) tp_button.findViewById(rowId);
Button drop_tp_delete_button = (Button) tp_delete_button.findViewById(rowId);
drop_tp_container.removeAllViews();
drop_tp_delete_button.setClickable(false);
drop_tp_button.setClickable(false);
drop_tp_title.setVisibility(View.GONE);
drop_tp_description.setVisibility(View.GONE);
drop_tp_delete_button.setVisibility(View.GONE);
drop_tp_textholder.setVisibility(View.GONE);
drop_tp_icon.setVisibility(View.GONE);
drop_tp_button.setVisibility(View.GONE);
drop_tp_container.setVisibility(View.GONE);
}
错误转储文本
04-04 14:20:06.145: E/AndroidRuntime(17242): java.lang.NullPointerException
04-04 14:20:06.145: E/AndroidRuntime(17242): at com.mediabarltd.digittrainer.HomePage.onClick(HomePage.java:153)
04-04 14:20:06.145: E/AndroidRuntime(17242): at android.view.View.performClick(View.java:2538)
04-04 14:20:06.145: E/AndroidRuntime(17242): at android.view.View$PerformClick.run(View.java:9152)
04-04 14:20:06.145: E/AndroidRuntime(17242): at android.os.Handler.handleCallback(Handler.java:587)
04-04 14:20:06.145: E/AndroidRuntime(17242): at android.os.Handler.dispatchMessage(Handler.java:92)
04-04 14:20:06.145: E/AndroidRuntime(17242): at android.os.Looper.loop(Looper.java:130)
04-04 14:20:06.145: E/AndroidRuntime(17242): at android.app.ActivityThread.main(ActivityThread.java:3691)
04-04 14:20:06.145: E/AndroidRuntime(17242): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 14:20:06.145: E/AndroidRuntime(17242): at java.lang.reflect.Method.invoke(Method.java:507)
04-04 14:20:06.145: E/AndroidRuntime(17242): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
04-04 14:20:06.145: E/AndroidRuntime(17242): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
04-04 14:20:06.145: E/AndroidRuntime(17242): at dalvik.system.NativeStart.main(Native Method)
感谢您的关注。