我正在为列表视图制作一个自定义适配器并获得NPE。我已经读过其他问题,但没有一个问题。 这是logcat
07-04 06:54:58.036: E/AndroidRuntime(1340): FATAL EXCEPTION: main
07-04 06:54:58.036: E/AndroidRuntime(1340): java.lang.NullPointerException
07-04 06:54:58.036: E/AndroidRuntime(1340): at com.example.amirkh.MainActivity$FastFoodAdapter.getView(MainActivity.java:166)
07-04 06:54:58.036: E/AndroidRuntime(1340): at android.widget.AbsListView.obtainView(AbsListView.java:2159)
07-04 06:54:58.036: E/AndroidRuntime(1340): at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
07-04 06:54:58.036: E/AndroidRuntime(1340): at android.widget.ListView.onMeasure(ListView.java:1158)
07-04 06:54:58.036: E/AndroidRuntime(1340): at android.view.View.measure(View.java:15518)
07-04 06:54:58.036: E/AndroidRuntime(1340): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-04 06:54:58.036: E/AndroidRuntime(1340): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
07-04 06:54:58.036: E/AndroidRuntime(1340): at android.widget.TableRow.measureChildBeforeLayout(TableRow.java:247)
代码以这种方式工作。有一个数据库处理程序,它组织数据库,快餐类和主类。在主类中,用户输入快餐名称,然后转到数据库。当用户点击“显示数据”选项卡时,应显示整个数据。 这是主要的课程:
package com.example.amirkh;
import java.util.ArrayList;
import java.util.List;
import android.R.integer;
import android.R.menu;
import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.ActionProvider;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem.OnActionExpandListener;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends TabActivity {
private TabHost myTabHost;
private FastFoodAdapter<String> adpter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTabHost = getTabHost();
TabHost.TabSpec spec;
// Adding First Tab
spec = myTabHost.newTabSpec("first");
spec.setContent(R.id.inputtab);
spec.setIndicator("Input data");
myTabHost.addTab(spec);
// Adding Second Tab
spec = myTabHost.newTabSpec("second");
spec.setContent(R.id.listView1);
spec.setIndicator("Show data");
myTabHost.addTab(spec);
myTabHost.setCurrentTab(0);
// final List<FastFood> fflist = new ArrayList<FastFood>();
final EditText etname = (EditText) findViewById(R.id.etname);
final EditText etaddress = (EditText) findViewById(R.id.etaddress);
final ListView lv = (ListView) findViewById(R.id.listView1);
final DataBaseHandeler dbh = new DataBaseHandeler(MainActivity.this);
final RadioGroup radiogroup = (RadioGroup) findViewById(R.id.radioGroup1);
final FastFood fastfood = new FastFood();
final RadioButton rbsit = (RadioButton) findViewById(R.id.rbsit);
final RadioButton rbdeli = (RadioButton) findViewById(R.id.rbdeliv);
final RadioButton rbtake = (RadioButton) findViewById(R.id.rbtake);
refreshComponents();
Button save = (Button) findViewById(R.id.btnsave);
Button remove = (Button) findViewById(R.id.btnremove);
// Create context menu
registerForContextMenu(lv);
// end of context menu
// start of save button
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
FastFood ffastfood = new FastFood("", "", "");
ffastfood.setName(etname.getText().toString());
ffastfood.setAddress(etaddress.getText().toString());
etname.setText("");
etaddress.setText("");
if (rbsit.isChecked()) {
ffastfood.setFlag("1");
} else if (rbtake.isChecked()) {
ffastfood.setFlag("2");
} else if (rbdeli.isChecked()) {
ffastfood.setFlag("3");
}
Toast toast = Toast.makeText(MainActivity.this,
"Fastfood is saved.", 2000);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
dbh.addFastFood(ffastfood);
refreshComponents();
}
}); // end of save
// remove
remove.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
EditText etrmvid = (EditText) findViewById(R.id.etrmvid);
dbh.removeFastFood(Integer.parseInt(etrmvid.getText()
.toString()));
etrmvid.setText("");
Toast toast = Toast.makeText(MainActivity.this,
"Fastfood is removed.", 2000);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
refreshComponents();
}
});// end of remove
Context context = getApplicationContext();
// Adapter
}
class FastFoodAdapter<String> extends ArrayAdapter<String> {
public FastFood fastfood = new FastFood();
public FastFoodAdapter(Context context, int textViewResourceId,
List objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.listview_item_row, parent,
false);
}
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.listview_item_row, parent,
false);
TextView tv = (TextView) row.findViewById(R.id.txtTitle23);
TextView tv2 = (TextView) row.findViewById(R.id.txtTitle2);
ImageView iv = (ImageView) row.findViewById(R.id.imgIcon);
tv.setText(fastfood.getName());
tv2.setText(fastfood.getAddress());
if (fastfood.getFlag().equals("1"))
iv.setImageResource(R.drawable.sit);
else if (fastfood.getFlag().equals("2"))
iv.setImageResource(R.drawable.ta);
else if (fastfood.getFlag().equals("3"))
iv.setImageResource(R.drawable.deli);
return row;
}
}// /end of adapter
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.contexmenu, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
return super.onContextItemSelected(item);
}
private void refreshComponents() {
ListView lv = (ListView) findViewById(R.id.listView1);
TextView lblCount = (TextView) findViewById(R.id.lblcount);
DataBaseHandeler dbh = new DataBaseHandeler(MainActivity.this);
List<FastFood> fflist = dbh.getAllFastFoods();
// lv.setAdapter(new FastFoodAdapter<String>(context,
// textViewResourceId, objects))
lv.setAdapter(new FastFoodAdapter<FastFood>(MainActivity.this,
android.R.layout.simple_list_item_1, fflist));
lblCount.setText("Count of all FastFoods are " + dbh.getFastFoodCount());
}
}
这里是快餐类
package com.example.amirkh;
public class FastFood {
private String name;
private String address;
private String flag;
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public FastFood(String name, String address, String phone) {
super();
this.name = name;
this.address = address;
}
public FastFood() {
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return (String.format("%s %s ", getName(), getAddress()));
}
}
和databasehandeler
package com.example.amirkh;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseHandeler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "FastFoodManager";
private static final String TABALE_FASTFOODS = "fastfoods";
private static final String KEY_ID = "_id";
private static final String KEY_NAME = "name";
private static final String KEY_ADDRESS = "address";
private static final String KEY_FLAG = "flag";
public DataBaseHandeler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS " + TABALE_FASTFOODS + " ("
+ KEY_ID + " INTEGER PRIMARY KEY, " + KEY_NAME + " TEXT, "
+ KEY_ADDRESS + " TEXT, " + KEY_FLAG + " TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABALE_FASTFOODS);
onCreate(db);
}
// insert a record
public void addFastFood(FastFood fastfood) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, fastfood.getName());
values.put(KEY_ADDRESS, fastfood.getAddress());
values.put(KEY_FLAG, fastfood.getFlag());
db.insert(TABALE_FASTFOODS, null, values);
db.close();
}
// remove a record by ID
public void removeFastFood(int id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABALE_FASTFOODS, KEY_ID + " =?",
new String[] { String.valueOf(id) });
db.close();
}
// count
public int getFastFoodCount() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABALE_FASTFOODS, null);
// db.close();
return cursor.getCount();
}
// get all contacts
public List<FastFood> getAllFastFoods() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABALE_FASTFOODS, null);
List<FastFood> fflist = new ArrayList<FastFood>();
if (cursor.moveToFirst()) {
do {
FastFood ff = new FastFood();
ff.setId(Integer.parseInt(cursor.getString(0)));
ff.setName(cursor.getString(1));
ff.setAddress(cursor.getString(2));
ff.setFlag(cursor.getString(3));
fflist.add(ff);
} while (cursor.moveToNext());
}
// db.close();
return fflist;
}
}
感谢您的帮助
答案 0 :(得分:0)
您的NPE来自:
if (fastfood.getFlag().equals("1"))
所以fastfood.getFlag()
似乎是空的。您应该先使用setFlag()
或尝试此比较:
if ("1".equals(fastfood.getFlag()))
答案 1 :(得分:0)
您收到此NullPointerException
是因为您试图比较此行中的null String
fastfood.getFlag().equals("1")
在适配器中,使用
创建Food
对象
public FastFood fastfood = new FastFood();
在Food
类的空构造函数中,没有任何成员分配给任何成员String
,所以当你尝试
fastfood.getFlag().equals("1")
您正尝试在equals
对象上调用null
方法。
所以你得到NullPointerException
。
您应该使用在Food
构造函数中传递的List
中的FastFoodAdapter
个对象。