显示图像& ListView中SQLite表中所有行的文本 - java.lang.NullPointerException:null对象引用

时间:2016-04-12 21:25:38

标签: android listview android-studio nullpointerexception

我是Android编程新手。我试图在ListView中显示SQLite中的表中的所有行。该表名为teams,包含idteam_nameimage。我怎么会收到以下错误。

Process: com.example.android.soccerleaguesapp, PID: 3938
 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
     at com.example.android.soccerleaguesapp.TeamImageAdapter.getView(TeamImageAdapter.java:62)
     at android.widget.AbsListView.obtainView(AbsListView.java:2344)
     at android.widget.ListView.makeAndAddView(ListView.java:1864)
     at android.widget.ListView.fillDown(ListView.java:698)
     at android.widget.ListView.fillFromTop(ListView.java:759)
     at android.widget.ListView.layoutChildren(ListView.java:1673)
     at android.widget.AbsListView.onLayout(AbsListView.java:2148)
     at android.view.View.layout(View.java:15691)
     at android.view.ViewGroup.layout(ViewGroup.java:4967)
     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077)
     at android.view.View.layout(View.java:15691)
     at android.view.ViewGroup.layout(ViewGroup.java:4967)
     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
     at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
     at android.view.View.layout(View.java:15691)
     at android.view.ViewGroup.layout(ViewGroup.java:4967)
     at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435)
     at android.view.View.layout(View.java:15691)
     at android.view.ViewGroup.layout(ViewGroup.java:4967)
     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
     at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
     at android.view.View.layout(View.java:15691)
     at android.view.ViewGroup.layout(ViewGroup.java:4967)
     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
     at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
     at android.view.View.layout(View.java:15691)
     at android.view.ViewGroup.layout(ViewGroup.java:4967)
     at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
     at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
     at android.view.View.layout(View.java:15691)
     at android.view.ViewGroup.layout(ViewGroup.java:4967)
     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2093)
     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1850)
     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1063)
     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5807)
     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
     at android.view.Choreographer.doCallbacks(Choreographer.java:580)
     at android.view.Choreographer.doFrame(Choreographer.java:550)
     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
     at android.os.Handler.handleCallback(Handler.java:739)
     at android.os.Handler.dispatchMessage(Handler.java:95)
     at android.os.Looper.loop(Looper.java:135)
     at android.app.ActivityThread.main(ActivityThread.java:5319)
     at java.lang.reflect.Method.invoke(Native Method)
     at java.lang.reflect.Method.invoke(Method.java:372)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)

getAllTeams方法

// Getting All Teams
public List < Team > getAllTeams() {
 List < Team > teamList = new ArrayList < Team > ();
 // Select All Query
 String selectQuery = "SELECT * FROM teams ORDER BY team_name";

 SQLiteDatabase db = this.getWritableDatabase();
 Cursor cursor = db.rawQuery(selectQuery, null);
 // looping through all rows and adding to list
 if (cursor.moveToFirst()) {
  do {
   Team team = new Team();
   team.setId(Integer.parseInt(cursor.getString(0)));
   team.setTeamName(cursor.getString(1));
   team.setPath(cursor.getString(2));
   // Adding team to list
   teamList.add(team);
  } while (cursor.moveToNext());
 }
 // close inserting data from database
 db.close();
 // return team list
 return teamList;

}

团队成员

public class Team {
 String team_name, path, league_name;
 int id, league_id;

 public Team(int keyId, String team_name, String path) {
  this.id = keyId;
  this.team_name = team_name;
  this.path = path;
 }

 public Team() {}

 public Team(int keyId) {
  this.id = keyId;

 }

 public int getId() {
  return id;
 }

 public void setId(int keyId) {
  this.id = keyId;
 }

 public int getLeagueId() {
  return league_id;
 }

 public String getTeamName() {
  return team_name;
 }

 public String getLeagueName() {
  return league_name;
 }

 public void setTeamName(String team_name) {
  this.team_name = team_name;
 }

 public void setLeague_id(int league_id) {
  this.league_id = league_id;
 }

 public void setPath(String path) {
  this.path = path;
 }
 public String getPath() {
  return path;
 }
}

TeamImageAdapter类

public class TeamImageAdapter extends ArrayAdapter < Team > {

  Context context;
  int layoutResourceId;
  ArrayList < Team > data = new ArrayList < Team > ();

  public TeamImageAdapter(Context context, int layoutResourceId, ArrayList < Team > data) {
   super(context, layoutResourceId, data);
   this.layoutResourceId = layoutResourceId;
   this.context = context;
   this.data = data;
  }
  private final int THUMBSIZE = 96;

  private static class ViewHolder {
   ImageView imgIcon;
   TextView teamName;
  }

  public View getView(int position, View convertView, ViewGroup parent) {
   View row = convertView;
   ViewHolder holder = null;
   if (row == null) {
    LayoutInflater inflater = ((Activity) context).getLayoutInflater();
    row = inflater.inflate(layoutResourceId, parent, false);
    holder = new ViewHolder();
    holder.teamName = (TextView) row.findViewById(R.id.item_img_infor);
    holder.imgIcon = (ImageView) row.findViewById(R.id.item_img_icon);
    row.setTag(holder);
   } else {
    holder = (ViewHolder) row.getTag();
   }
   Team team = data.get(position);
   holder.teamName.setText(team.getTeamName());
   holder.imgIcon.setImageBitmap(ThumbnailUtils
    .extractThumbnail(BitmapFactory.decodeFile(team.getPath()),
     THUMBSIZE, THUMBSIZE));
   return row;
  }

DisplayTeam Class

public class DisplayTeam extends AppCompatActivity {

 ArrayList < Team > imageArry = new ArrayList < Team > ();
 TeamImageAdapter adapter;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.display_team);

  DatabaseHelper myDb = new DatabaseHelper(this);

  // Reading all teams from database
  List < Team > team = myDb.getAllTeams();
  for (Team tm: team) {
   String log = "ID:" + tm.getId() + " Team Name: " + tm.getTeamName() + " ,Image: " + tm.getPath();

   // Writing teams to log
   Log.d("Result: ", log);
   //add teams data in arrayList
   imageArry.add(tm);
  }
  adapter = new TeamImageAdapter(this, R.layout.screen_list,
   imageArry);
  ListView dataList = (ListView) findViewById(R.id.main_list_view);
  dataList.setAdapter(adapter);
 }
}

我知道这是一个相当常见的错误,但我无法理解我出错的地方。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

您的TextView实例为null。 这可能是由于ID不正确或布局不匹配造成的。

另外,为什么不使用RecyclerView

答案 1 :(得分:1)

在尝试获取TextView时,您明显使用了错误的ID:

holder.teamName = (TextView) row.findViewById(R.id.item_img_infor);

您在扩充的布局中没有TextView元素,或者您根本没有具有此类ID的元素(这非常值得怀疑,因为您的IDE会暗示您这样做)。