我在列表视图中有两个“编辑”和“删除”按钮,当用户单击一个按钮时,我必须启动一个新活动“EditActivity 或 DeleteActivity”来编辑和删除按钮。
`公共类 DepartmentAdapter 扩展 ArrayAdapter 实现 OnClickListener {
int resource;
String response;
Context context;
//Initialize adapter
public DepartmentAdapter(Context _context, int resource, ArrayList<Department> items) {
super(_context, resource, items);
this.resource=resource;
context = _context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LinearLayout deptView;
//Get the current Department object
Department dept = getItem(position);
//Inflate the view
if(convertView==null)
{
deptView = new LinearLayout(getContext());
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater vi;
vi = (LayoutInflater)getContext().getSystemService(inflater);
vi.inflate(resource, deptView, true);
}
else
{
deptView = (LinearLayout) convertView;
}
//Get the text boxes from the listitem.xml file
TextView id =(TextView)deptView.findViewById(R.id.Id);
TextView sname =(TextView)deptView.findViewById(R.id.sname);
TextView name =(TextView)deptView.findViewById(R.id.name);
//Assign the appropriate data from our Department object above
id.setText(Integer.toString(dept.getID()));
sname.setText(dept.getDepartmentShortName());
name.setText(dept.getDepartmentName());
Button edit = (Button)deptView.findViewById(R.id.edit);
//del= (Button)deptListView.findViewById(R.id.del);
ShowDepertment d = new ShowDepertment();
edit.setOnClickListener(this);
}
public void onClick(View v) {
Intent myIntent = new Intent(context , AddDepertment.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}`
在 onclick() 中,无法启动活动并出现错误:
10-21 13:15:11.116: E/AndroidRuntime(11718): FATAL EXCEPTION: main
10-21 13:15:11.116: E/AndroidRuntime(11718): java.lang.NullPointerException
10-21 13:15:11.116: E/AndroidRuntime(11718): at my.mewappattendance.myAdapters.DepartmentAdapter.getView(DepartmentAdapter.java:80)
10-21 13:15:11.116: E/AndroidRuntime(11718): at android.widget.AbsListView.obtainView(AbsListView.java:2033)
10-21 13:15:11.116: E/AndroidRuntime(11718): at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
10-21 13:15:11.116: E/AndroidRuntime(11718): at android.widget.ListView.onMeasure(ListView.java:1155)
10-21 13:15:11.116: E/AndroidRuntime(11718): at android.view.View.measure(View.java:12727)
10-21 13:15:11.116: E/AndroidRuntime(11718): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
10-21 13:15:11.116: E/AndroidRuntime(11718): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
10-21 13:15:11.116: E/AndroidRuntime(11718): at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
10-21 13:15:11.116: E/AndroidRuntime(11718): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
10-21 13:15:11.116: E/AndroidRuntime(11718): at android.view.View.measure(View.java:12727)
10-21 13:15:11.116: E/AndroidRuntime(11718): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1196)
10-21 13:15:11.116: E/AndroidRuntime(11718): at android.widget.LinearLayout.onMeasure(LinearLayout.java:555)
10-21 13:15:11.116: E/AndroidRuntime(11718): at android.view.View.measure(View.java:12727)
10-21 13:15:11.116: E/AndroidRuntime(11718): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
10-21 13:15:11.116: E/AndroidRuntime(11718): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
10-21 13:15:11.116: E/AndroidRuntime(11718): at android.view.View.measure(View.java:12727)
10-21 13:15:11.116: E/AndroidRuntime(11718): at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
10-21 13:15:11.116: E/AndroidRuntime(11718): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
10-21 13:15:11.116: E/AndroidRuntime(11718): at android.view.View.measure(View.java:12727)
10-21 13:15:11.116: E/AndroidRuntime(11718): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
10-21 13:15:11.116: E/AndroidRuntime(11718): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
10-21 13:15:11.116: E/AndroidRuntime(11718): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
10-21 13:15:11.116: E/AndroidRuntime(11718): at android.view.View.measure(View.java:12727)
10-21 13:15:11.116: E/AndroidRuntime(11718): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1068)
10-21 13:15:11.116: E/AndroidRuntime(11718): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2446)
10-21 13:15:11.116: E/AndroidRuntime(11718): at android.os.Handler.dispatchMessage(Handler.java:99)
10-21 13:15:11.116: E/AndroidRuntime(11718): at android.os.Looper.loop(Looper.java:137)
10-21 13:15:11.116: E/AndroidRuntime(11718): at android.app.ActivityThread.main(ActivityThread.java:4424)
10-21 13:15:11.116: E/AndroidRuntime(11718): at java.lang.reflect.Method.invokeNative(Native Method)
10-21 13:15:11.116: E/AndroidRuntime(11718): at java.lang.reflect.Method.invoke(Method.java:511)
10-21 13:15:11.116: E/AndroidRuntime(11718): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-21 13:15:11.116: E/AndroidRuntime(11718): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-21 13:15:11.116: E/AndroidRuntime(11718): at dalvik.system.NativeStart.main(Native Method)