当我单击按钮删除 TextView、Button 和 LinearLayout 时,我的应用程序崩溃。这是 logcat 报告。当我想在第 118 行删除视图时,它就崩溃了:
tasklayout.removeView(taskbutton);
05-19 14:35:58.222: E/AndroidRuntime(632): java.lang.NullPointerException
05-19 14:35:58.222: E/AndroidRuntime(632): at com.com.com.addtask.deltask(addtask.java:118)
05-19 14:35:58.222: E/AndroidRuntime(632): at com.com.com.addtask$2.onClick(addtask.java:65)
05-19 14:35:58.222: E/AndroidRuntime(632): at android.view.View.performClick(View.java:3511)
05-19 14:35:58.222: E/AndroidRuntime(632): at android.view.View$PerformClick.run(View.java:14105)
05-19 14:35:58.222: E/AndroidRuntime(632): at android.os.Handler.handleCallback(Handler.java:605)
这是 onClickListner 和 deltask void
Button delt = (Button) findViewById(R.id.btndelete1);
delt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
deltask(v);
}
});
public void deltask(View v) {
SharedPreferences datatask;
datatask = getSharedPreferences(filename, 0);
int tnum = datatask.getInt("tasknumber", 1);
int layoutnum = 1000 + tnum;
int textviewnum = 1100 + tnum;
int buttonviewnum = 1200 + tnum;
LinearLayout layout = (LinearLayout) findViewById(R.id.taskll);
LinearLayout tasklayout = (LinearLayout) findViewById(layoutnum);
TextView tasktv = (TextView) findViewById(textviewnum);
Button taskbutton = (Button) findViewById(buttonviewnum);
tasklayout.removeView(taskbutton);
tasklayout.removeView(tasktv);
layout.removeView(tasklayout);
}
有人帮忙!