Adapter.getview错误

时间:2013-11-14 16:45:33

标签: android eclipse

我是Eclipse / Android的新手。 我有很多错误,但无法抓住它们......请需要帮助。

package chintan.khetiya.sqlite.cursor;

import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Product_Screen extends Activity{

Button add_prod_btn;
ListView Product_listview;
ArrayList<Product> product_data = new ArrayList<Product>();
Product_Adapter pAdapter;

DatabaseHandler db;
String Toast_msg;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.products);
try {
    Product_listview = (ListView) findViewById(R.id.prod_list);
    Product_listview.setItemsCanFocus(false);
    add_prod_btn = (Button) findViewById(R.id.add_prod_btn);

    Set_Referash_Data();

} catch (Exception e) {
    // TODO: handle exception
    Log.e("Error", "" + e);
}
add_prod_btn.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent add_product = new Intent(Product_Screen.this,
        Add_Update_Prod.class);
    add_product.putExtra("called", "add");
    add_product.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
        | Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(add_product);
    finish();
    }
});

}

public void Set_Referash_Data() {
product_data.clear();
db = new DatabaseHandler(this);
ArrayList<Product> product_array_from_db = db.Get_Products();

for (int i = 0; i < product_array_from_db.size(); i++) {

    int tidno1 = product_array_from_db.get(i).get_prodID();
    String prod_name = product_array_from_db.get(i).get_prodName();
    String desc = product_array_from_db.get(i).get_prodDesc();
    Product prod = new Product();
    prod.set_prodID(tidno1);
    prod.set_prodName(prod_name);
    prod.set_prodDesc(desc);
    product_data.add(prod);
}
db.close();
pAdapter = new Product_Adapter(Product_Screen.this, R.layout.listview_row,
    product_data);
Product_listview.setAdapter(pAdapter);
pAdapter.notifyDataSetChanged();
}

public void Show_Toast(String msg) {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}

@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
Set_Referash_Data();

}

public class Product_Adapter extends ArrayAdapter<Product> {
Activity activity;
int layoutResourceId;
Product product;

ArrayList<Product> data = new ArrayList<Product>();

public Product_Adapter(Activity act, int layoutResourceId,
    ArrayList<Product> data) {
    super(act, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.activity = act;
    this.data = data;
    notifyDataSetChanged();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row1 = convertView;
    UserHolder holder = null;

    if (row1 == null) {
    LayoutInflater inflater = LayoutInflater.from(activity);

    row1 = inflater.inflate(layoutResourceId, parent, false);
    holder = new UserHolder();
    holder.prod = (TextView) row1.findViewById(R.id.user_prod_name_txt);
    holder.desc = (TextView) row1.findViewById(R.id.user_prod_desc_txt);
    holder.edit = (Button) row1.findViewById(R.id.btn_prod_update);
    holder.delete = (Button) row1.findViewById(R.id.btn_prod_delete);
    row1.setTag(holder);
    } else {
    holder = (UserHolder) row1.getTag();
    }
    product = data.get(position);
    holder.edit.setTag(product.get_prodID());
    holder.delete.setTag(product.get_prodID());
    holder.prod.setText(product.get_prodName());
    holder.desc.setText(product.get_prodDesc());

    holder.edit.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Log.i("Edit Button Clicked", "**********");

        Intent update_product = new Intent(activity,
            Add_Update_Prod.class);
        update_product.putExtra("called", "update");
        update_product.putExtra("PROD_ID", v.getTag().toString());
        activity.startActivity(update_product);

    }

    });
    holder.delete.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(final View v) {
        // TODO Auto-generated method stub

        // show a message while loader is loading

        AlertDialog.Builder adb = new AlertDialog.Builder(activity);
        adb.setTitle("Alerta !");
        adb.setMessage("Está a punto de eliminarlo ");
        final int prod_id = Integer.parseInt(v.getTag().toString());
        adb.setNegativeButton("Cancelar", null);
        adb.setPositiveButton("Eliminar",
            new AlertDialog.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog,
                int which) {
                // MyDataObject.remove(positionToRemove);
                DatabaseHandler dBHandler = new DatabaseHandler(
                    activity.getApplicationContext());
                dBHandler.Delete_Product(prod_id);
                Product_Screen.this.onResume();

            }
            });
        adb.show();
    }

    });
    return row1;

}

class UserHolder {
    TextView prod;
    TextView desc;
    Button edit;
    Button delete;
}

}

}

然后是错误。

E/AndroidRuntime(788): FATAL EXCEPTION: main
E/AndroidRuntime(788): java.lang.NullPointerException
E/AndroidRuntime(788):  at    chintan.khetiya.sqlite.cursor.Product_Screen$Product_Adapter.getView(Product_Screen.java:141)
E/AndroidRuntime(788):  at android.widget.AbsListView.obtainView(AbsListView.java:2159)
E/AndroidRuntime(788):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
E/AndroidRuntime(788):  at android.widget.ListView.onMeasure(ListView.java:1158)
E/AndroidRuntime(788):  at android.view.View.measure(View.java:15518)
E/AndroidRuntime(788):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
E/AndroidRuntime(788):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
E/AndroidRuntime(788):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
E/AndroidRuntime(788):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
E/AndroidRuntime(788):  at android.view.View.measure(View.java:15518)
E/AndroidRuntime(788):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
E/AndroidRuntime(788):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
E/AndroidRuntime(788):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
E/AndroidRuntime(788):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
E/AndroidRuntime(788):  at android.view.View.measure(View.java:15518)
E/AndroidRuntime(788):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
E/AndroidRuntime(788):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
E/AndroidRuntime(788):  at android.view.View.measure(View.java:15518)
E/AndroidRuntime(788):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
E/AndroidRuntime(788):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
E/AndroidRuntime(788):  at android.view.View.measure(View.java:15518)
E/AndroidRuntime(788):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
E/AndroidRuntime(788):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
E/AndroidRuntime(788):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
E/AndroidRuntime(788):  at android.view.View.measure(View.java:15518)
E/AndroidRuntime(788):  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
E/AndroidRuntime(788):  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
E/AndroidRuntime(788):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
E/AndroidRuntime(788):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
E/AndroidRuntime(788):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
E/AndroidRuntime(788):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
E/AndroidRuntime(788):  at android.view.Choreographer.doCallbacks(Choreographer.java:562)
E/AndroidRuntime(788):  at android.view.Choreographer.doFrame(Choreographer.java:532)
E/AndroidRuntime(788):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
E/AndroidRuntime(788):  at android.os.Handler.handleCallback(Handler.java:725)
E/AndroidRuntime(788):  at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(788):  at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(788):  at android.app.ActivityThread.main(ActivityThread.java:5041)
E/AndroidRuntime(788):  at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(788):  at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(788):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
E/AndroidRuntime(788):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
E/AndroidRuntime(788):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您忘记在使用数据库之前使用use db.open();