Android:强制关闭onclick按钮列表视图

时间:2012-03-22 08:57:45

标签: android

当我点击按钮时,我有一个exeption并强制关闭

适配器代码:

public class myAdapter extends BaseAdapter implements OnClickListener {

private ArrayList<produit> produits;
private LayoutInflater myInflater;

public myAdapter(Context context, ArrayList<produit> _produits) {
    this.myInflater = LayoutInflater.from(context);
    this.produits = _produits;

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return this.produits.size();
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return this.produits.get(arg0);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (null == convertView) {
        convertView = myInflater.inflate(R.layout.listitem, null);
    }

    Button btn = (Button) convertView.findViewById(R.id.button1);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(v.getContext(), Local.class);
            v.getContext().startActivity(i);

        }
    });
    // set the text
    TextView tv = (TextView) convertView.findViewById(R.id.textView1);
    tv.setText(produits.get(position).nom);
    return convertView;

}

@Override
public void onClick(View v) {
    Log.v("bonjour", "Row button clicked");
}

}

主要代码:

public class Main extends Activity {


 private ArrayList<produit>produits=new ArrayList<produit>();


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    produits.add(new produit("marlboro"));
    produits.add(new produit("sabrine"));

    ListView list = (ListView)findViewById(R.id.listView1);
    myAdapter adpt = new myAdapter (this,produits);

    list.setAdapter(adpt);

}
}

logcat的:

03-22 09:52:51.188:E / AndroidRuntime(480):错误:线程附着失败 03-22 09:52:52.259:E / AndroidRuntime(488):错误:线程附着失败 03-22 09:52:54.388:W / dalvikvm(495):threadid = 3:线程退出未捕获异常(组= 0x4001b188) 03-22 09:52:54.388:E / AndroidRuntime(495):未捕获的处理程序:由于未捕获的异常导致主线程退出 03-22 09:52:54.408:E / AndroidRuntime(495):android.content.ActivityNotFoundException:无法找到显式活动类{wassim.list / wassim.list.Local};你有没有在AndroidManifest.xml中声明这个活动? 03-22 09:52:54.408:E / AndroidRuntime(495):在android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404) 03-22 09:52:54.408:E / AndroidRuntime(495):在android.app.Instrumentation.execStartActivity(Instrumentation.java:1378) 03-22 09:52:54.408:E / AndroidRuntime(495):在android.app.Activity.startActivityForResult(Activity.java:2749) 03-22 09:52:54.408:E / AndroidRuntime(495):在android.app.Activity.startActivity(Activity.java:2855) 03-22 09:52:54.408:E / AndroidRuntime(495):at wassim.list.myAdapter $ 1.onClick(myAdapter.java:63) 03-22 09:52:54.408:E / AndroidRuntime(495):在android.view.View.performClick(View.java:2364) 03-22 09:52:54.408:E / AndroidRuntime(495):在android.view.View.onTouchEvent(View.java:4179) 03-22 09:52:54.408:E / AndroidRuntime(495):在android.widget.TextView.onTouchEvent(TextView.java:6541) 03-22 09:52:54.408:E / AndroidRuntime(495):在android.view.View.dispatchTouchEvent(View.java:3709) 03-22 09:52:54.408:E / AndroidRuntime(495):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 03-22 09:52:54.408:E / AndroidRuntime(495):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 03-22 09:52:54.408:E / AndroidRuntime(495):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 03-22 09:52:54.408:E / AndroidRuntime(495):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 03-22 09:52:54.408:E / AndroidRuntime(495):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 03-22 09:52:54.408:E / AndroidRuntime(495):在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 03-22 09:52:54.408:E / AndroidRuntime(495):at com.android.internal.policy.impl.PhoneWindow $ DecorView.superDispatchTouchEvent(PhoneWindow.java:1659) 03-22 09:52:54.408:E / AndroidRuntime(495):at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107) 03-22 09:52:54.408:E / AndroidRuntime(495):在android.app.Activity.dispatchTouchEvent(Activity.java:2061) 03-22 09:52:54.408:E / AndroidRuntime(495):at com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:1643) 03-22 09:52:54.408:E / AndroidRuntime(495):在android.view.ViewRoot.handleMessage(ViewRoot.java:1691) 03-22 09:52:54.408:E / AndroidRuntime(495):在android.os.Handler.dispatchMessage(Handler.java:99) 03-22 09:52:54.408:E / AndroidRuntime(495):在android.os.Looper.loop(Looper.java:123) 03-22 09:52:54.408:E / AndroidRuntime(495):在android.app.ActivityThread.main(ActivityThread.java:4363) 03-22 09:52:54.408:E / AndroidRuntime(495):at java.lang.reflect.Method.invokeNative(Native Method) 03-22 09:52:54.408:E / AndroidRuntime(495):at java.lang.reflect.Method.invoke(Method.java:521) 03-22 09:52:54.408:E / AndroidRuntime(495):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:860) 03-22 09:52:54.408:E / AndroidRuntime(495):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 03-22 09:52:54.408:E / AndroidRuntime(495):at dalvik.system.NativeStart.main(Native Method) 03-22 09:57:54.618:W / InputManagerService(51):得到RemoteException向pid发送setActive(false)通知495 uid 10030

我该如何做到这一点?

2 个答案:

答案 0 :(得分:1)

尝试:

private ArrayList<produit> produits;
private LayoutInflater myInflater;
private Context context

public myAdapter(Context context, ArrayList<produit> _produits) {
    this.myInflater = LayoutInflater.from(context);
    this.produits = _produits;
    this.context  = context;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return this.produits.size();
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return this.produits.get(arg0);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

if (null == convertView) {
    convertView = myInflater.inflate(R.layout.listitem, null);
}

Button btn = (Button) convertView.findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent i = new Intent(context, Local.class);
        context.startActivity(i);

    }
});
// set the text
TextView tv = (TextView) convertView.findViewById(R.id.textView1);
tv.setText(produits.get(position).nom);
return convertView;

}

@Override
public void onClick(View v) {
Log.v("bonjour", "Row button clicked");
}
}

答案 1 :(得分:0)

猜猜:你确定你在这里得到了正确的背景吗?

Intent i = new Intent(v.getContext(), Local.class);