开始活动时出错

时间:2014-01-28 05:58:39

标签: java android

在我的阵列适配器类上获取错误。错误在Startavtivity线上我不知道为什么它在其他pahes上工作但它在这个页面上显示错误。所以你可以帮我解决

  

错误“构造函数Intent(MyArrayAdapter,Class<Add_new_employee >undefined)

package com.example.employeemanager;

import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class MyArrayAdapter extends ArrayAdapter<Student> {

    Context context;
    int layoutResourceId;
    ArrayList<Student> students = new ArrayList<Student>();

    public MyArrayAdapter(Context context, int layoutResourceId,ArrayList<Student> studs)
    {
        super(context, layoutResourceId, studs);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.students = studs;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        View item = convertView;
        StudentWrapper StudentWrapper = null;

        if (item == null)
        {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            item = inflater.inflate(layoutResourceId, parent, false);
            StudentWrapper = new StudentWrapper();
            StudentWrapper.name = (TextView) item.findViewById(R.id.textName);
            StudentWrapper.age = (TextView) item.findViewById(R.id.textAge);
            StudentWrapper.address = (TextView) item.findViewById(R.id.textAddr);
            StudentWrapper.edit = (Button) item.findViewById(R.id.btnEdit);
            StudentWrapper.delete = (Button) item.findViewById(R.id.btnDelete);
            StudentWrapper.checkBox = (CheckBox) item.findViewById(R.id.checkBox1);         
            item.setTag(StudentWrapper);
        } 
        else
        {
            StudentWrapper = (StudentWrapper) item.getTag();
        }

        Student student = students.get(position);
        StudentWrapper.name.setText(student.getName());
        StudentWrapper.age.setText(student.getAge());
        StudentWrapper.address.setText(student.getAddress());

        StudentWrapper.edit.setOnClickListener(new OnClickListener() {
            public void onClick(View v) 
            {
                startActivity(new Intent   
                            (MyArrayAdapter.this,Add_new_employee.class) );
            }
        });

        StudentWrapper.delete.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View v) {
                    // TODO Auto-generated method stub
                }
            });

        StudentWrapper.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                Toast.makeText(context, "Checkox", Toast.LENGTH_LONG).show();
            }
        });
        return item;
    }

    protected void startActivity(Intent intent) {
        // TODO Auto-generated method stub
    }

    static class StudentWrapper
    {
        TextView name;
        TextView age;
        TextView address;
        Button edit;
        Button delete;
        CheckBox checkBox;
    }
}

2 个答案:

答案 0 :(得分:2)

而不是这个

startActivity(new Intent   

                   (MyArrayAdapter.this,Add_new_employee.class) );

使用此

context.startActivity(new Intent   

                   (context,Add_new_employee.class) );

注意:确保您已在android manifest.xml中声明Add_new_employee

答案 1 :(得分:0)

尝试

startActivity(new Intent   
                   (getBaseContext(),Add_new_employee.class) );