Frage

I have integrated a custom expandable list view I found online into a project that I am working on. The issue I am running into is calling a new Activity from within the Expandable List View Adapter. Below is part of the code for the adapter. When the context.startActivity() is called I get a null exception error.

Any ideas on how to call a new activity

package com.example.apple;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;

@SuppressWarnings("unchecked")
public class NewAdapter extends BaseExpandableListAdapter {

 public ArrayList<String> groupItem, tempChild;
 public ArrayList<String> childDateTime;
 public ArrayList<Object> Childtem = new ArrayList<Object>();
 public LayoutInflater minflater;
 public Activity activity;
 private Context context;

 public NewAdapter(ArrayList<String> grList, ArrayList<Object> childItem, ArrayList<String> childDT) {
  groupItem = grList;
  this.Childtem = childItem;

  childDateTime = childDT;
 }

 public NewAdapter(Context context) {
     this.context = context;     
}

 public void setInflater(LayoutInflater mInflater, Activity act) {
  this.minflater = mInflater;
  activity = act;
 }

 @Override
 public Object getChild(int groupPosition, int childPosition) {
  return null;
 }

 @Override
 public long getChildId(int groupPosition, int childPosition) {
  return 0;
 }

 @Override
 public View getChildView(int groupPosition, final int childPosition,
   boolean isLastChild, View convertView, ViewGroup parent) {
  tempChild = (ArrayList<String>) Childtem.get(groupPosition);
  TextView text = null;
  if (convertView == null) {
   convertView = minflater.inflate(R.layout.activity_main_child, null);
  }
  text = (TextView) convertView.findViewById(R.id.textView1);
  text.setText(tempChild.get(childPosition));

  TextView dateTime = null;
  dateTime = (TextView) convertView.findViewById(R.id.textDateTime);
  dateTime.setText(childDateTime.get(groupPosition));

  convertView.setOnClickListener(new OnClickListener() {

@Override
   public void onClick(View v) {

       Intent intent= new Intent(context, ViewSomeStuff.class);

       context.startActivity(intent);
   }
  });
  return convertView;
 }
War es hilfreich?

Lösung

public NewAdapter(Context context, ArrayList<String> grList, ArrayList<Object> childItem, ArrayList<String> childDT) {
  groupItem = grList;
  this.Childtem = childItem;

  childDateTime = childDT;
  this.context = context; 
 }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top