ActionBar 中的后退按钮 NullPointerException
Back-Button in ActionBar NullPointerException
当我在这些行的操作栏中单击后退按钮时,我得到了一个 NullPointerException:
public class CustomAdapterTasks extends BaseAdapter {
Base64EncodeDecode base64EncodeDecode = new Base64EncodeDecode();
private ArrayList<HashMap<String, String>> taskItems;
private static LayoutInflater inflater = null;
private Bitmap bm;
public CustomAdapterTasks(Context context, ArrayList<HashMap<String, String>> items) {
super();
Context contex1t = context;
this.taskItems = items;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
if (taskItems.size() != 0)
return taskItems.size();
return 0;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public TextView taskTitleTextView;
public TextView taskObjectTextView;
public TextView taskLocationTextView;
public ImageView taskImageImageView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if (convertView == null) {
vi = inflater.inflate(R.layout.list_item_tasks, null);
holder = new ViewHolder();
holder.taskTitleTextView = (TextView) vi.findViewById(R.id.taskTitle);
holder.taskObjectTextView = (TextView) vi.findViewById(R.id.taskObject);
holder.taskLocationTextView = (TextView) vi.findViewById(R.id.taskLocation);
holder.taskImageImageView = (ImageView) vi.findViewById(R.id.taskImage);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
if (taskItems.size() <= 0) {
holder.taskTitleTextView.setText("Kein Titel");
holder.taskObjectTextView.setText("Kein Objekt");
holder.taskLocationTextView.setText("Kein Standort");
} else {
bm = Bitmap.createBitmap(base64EncodeDecode.decodeBase64(taskItems.get(position).get("image")));
holder.taskTitleTextView.setText(taskItems.get(position).get("title"));
holder.taskObjectTextView.setText(taskItems.get(position).get("object"));
holder.taskLocationTextView.setText(taskItems.get(position).get("location"));
holder.taskImageImageView.setImageBitmap(bm);
}
return vi;
}
}
也在我的 ListTasksActivity(第二个 Activity,不是主要的):
public class ListTasksActivity extends ListActivity implements AdapterView.OnItemClickListener {
ArrayList<HashMap<String, String>> adapterItems;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = getIntent();
ListView lv = getListView();
lv.setOnItemClickListener(this);
ArrayList<HashMap<String, String>> taskItems = (ArrayList<HashMap<String, String>>) i.getSerializableExtra("taskItems");
String[] stringArr = i.getStringArrayExtra("adapterStringArray");
int[] intArr = i.getIntArrayExtra("adapterIntArray");
CustomAdapterTasks adapterTasks = new CustomAdapterTasks(this,taskItems);
setListAdapter(adapterTasks);
adapterItems = taskItems;
//CustomAdapterTasks customAdapterTasks = new CustomAdapterTasks((ListViewActivity) ListViewActivity.getAppContext(),);
}
在:setListAdapter(adapterTasks);
这是我对后退按钮的实现:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setDisplayHomeAsUpEnabled(true);
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.ueber) {
startActivity(new Intent(this, menu_main_Activity.class));
return true;
}
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
异常:
Caused by: java.lang.NullPointerException
at CustomAdapter.CustomAdapterTasks.getCount(CustomAdapterTasks.java:34)
at android.widget.ListView.setAdapter(ListView.java:480)
at android.app.ListActivity.setListAdapter(ListActivity.java:265)
at de.example.app.ListTasksActivity.onCreate(ListTasksActivity.java:35)
如果您需要更多代码,请告诉我!
您对 getCount 的实现是错误的。应该是
public int getCount() {
if (taskItems != null)
return taskItems.size();
return 0;
}
当我在这些行的操作栏中单击后退按钮时,我得到了一个 NullPointerException:
public class CustomAdapterTasks extends BaseAdapter {
Base64EncodeDecode base64EncodeDecode = new Base64EncodeDecode();
private ArrayList<HashMap<String, String>> taskItems;
private static LayoutInflater inflater = null;
private Bitmap bm;
public CustomAdapterTasks(Context context, ArrayList<HashMap<String, String>> items) {
super();
Context contex1t = context;
this.taskItems = items;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
if (taskItems.size() != 0)
return taskItems.size();
return 0;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public TextView taskTitleTextView;
public TextView taskObjectTextView;
public TextView taskLocationTextView;
public ImageView taskImageImageView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if (convertView == null) {
vi = inflater.inflate(R.layout.list_item_tasks, null);
holder = new ViewHolder();
holder.taskTitleTextView = (TextView) vi.findViewById(R.id.taskTitle);
holder.taskObjectTextView = (TextView) vi.findViewById(R.id.taskObject);
holder.taskLocationTextView = (TextView) vi.findViewById(R.id.taskLocation);
holder.taskImageImageView = (ImageView) vi.findViewById(R.id.taskImage);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
if (taskItems.size() <= 0) {
holder.taskTitleTextView.setText("Kein Titel");
holder.taskObjectTextView.setText("Kein Objekt");
holder.taskLocationTextView.setText("Kein Standort");
} else {
bm = Bitmap.createBitmap(base64EncodeDecode.decodeBase64(taskItems.get(position).get("image")));
holder.taskTitleTextView.setText(taskItems.get(position).get("title"));
holder.taskObjectTextView.setText(taskItems.get(position).get("object"));
holder.taskLocationTextView.setText(taskItems.get(position).get("location"));
holder.taskImageImageView.setImageBitmap(bm);
}
return vi;
}
}
也在我的 ListTasksActivity(第二个 Activity,不是主要的):
public class ListTasksActivity extends ListActivity implements AdapterView.OnItemClickListener {
ArrayList<HashMap<String, String>> adapterItems;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = getIntent();
ListView lv = getListView();
lv.setOnItemClickListener(this);
ArrayList<HashMap<String, String>> taskItems = (ArrayList<HashMap<String, String>>) i.getSerializableExtra("taskItems");
String[] stringArr = i.getStringArrayExtra("adapterStringArray");
int[] intArr = i.getIntArrayExtra("adapterIntArray");
CustomAdapterTasks adapterTasks = new CustomAdapterTasks(this,taskItems);
setListAdapter(adapterTasks);
adapterItems = taskItems;
//CustomAdapterTasks customAdapterTasks = new CustomAdapterTasks((ListViewActivity) ListViewActivity.getAppContext(),);
}
在:setListAdapter(adapterTasks);
这是我对后退按钮的实现:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setDisplayHomeAsUpEnabled(true);
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.ueber) {
startActivity(new Intent(this, menu_main_Activity.class));
return true;
}
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
异常:
Caused by: java.lang.NullPointerException
at CustomAdapter.CustomAdapterTasks.getCount(CustomAdapterTasks.java:34)
at android.widget.ListView.setAdapter(ListView.java:480)
at android.app.ListActivity.setListAdapter(ListActivity.java:265)
at de.example.app.ListTasksActivity.onCreate(ListTasksActivity.java:35)
如果您需要更多代码,请告诉我!
您对 getCount 的实现是错误的。应该是
public int getCount() {
if (taskItems != null)
return taskItems.size();
return 0;
}