我正在尝试将一些名称加载到列表中,但我似乎并没有走得太远。每当我尝试运行我的代码时,它就会给我这个:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.gijbelssiemen.herexamen.Model.Artist.getNaam()' on a null object reference
at com.gijbelssiemen.herexamen.TabFragment1.addArtistsToList(TabFragment1.java:97)
at com.gijbelssiemen.herexamen.TabFragment1.onViewCreated(TabFragment1.java:66)
我的代码如下所示:
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.AdapterView;
import com.gijbelssiemen.herexamen.ArtistInfoActivity;
import com.gijbelssiemen.herexamen.Adapter.ArtistListAdapter;
import com.gijbelssiemen.herexamen.Database.DatabaseHelper;
import com.gijbelssiemen.herexamen.Model.Artist;
import java.util.ArrayList;
public class TabFragment1 extends Fragment {
View rootView;
DatabaseHelper db;
Artist artist;
Integer[] imageId = {
R.drawable.asa_moto,
R.drawable.bea1991,
R.drawable.easter,
R.drawable.he4rtbroken,
R.drawable.kablam,
R.drawable.le1f,
R.drawable.pruikduif,
R.drawable.sheniqua,
R.drawable.syracuse
};
ArrayList<String> artists;
ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db = new DatabaseHelper(getActivity());
artist = new Artist();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.tab_fragment_1, container, false);
db.deleteArtist(artist);
addArtists();
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
addArtistsToList();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
long ids = parent.getItemIdAtPosition(position);
int artistId = (int) ids;
Log.d("ID", Long.toString(ids));
Intent artistInfo = new Intent(getActivity(), ArtistInfoActivity.class);
artistInfo.putExtra("Naam", db.getArtist(artistId + 1).getNaam());
artistInfo.putExtra("Beschrijving", db.getArtist(artistId + 1).getBeschrijving());
artistInfo.putExtra("Image", db.getArtist(artistId + 1).getImage());
startActivity(artistInfo);
}
});
}
private void addArtists() {
Log.d("Insert: ", "Inserting ..");
db.addArtist(new Artist("ASA MOTO", R.string.asa_moto_description, R.drawable.asa_moto));
db.addArtist(new Artist("BEA1991", R.string.bea1991_description, R.drawable.bea1991));
db.addArtist(new Artist("EASTER", R.string.easter_description, R.drawable.easter));
db.addArtist(new Artist("HE4RTBROKEN", R.string.he4rtbroken_description, R.drawable.he4rtbroken));
db.addArtist(new Artist("KABLAM", R.string.kablam_description, R.drawable.kablam));
db.addArtist(new Artist("LE1F", R.string.le1f_description, R.drawable.le1f));
db.addArtist(new Artist("PRUIKDUIF", R.string.pruikduif_description, R.drawable.pruikduif));
db.addArtist(new Artist("SHENIQUA WORLD TOUR", R.string.sheniqua_world_tour_description, R.drawable.sheniqua));
db.addArtist(new Artist("SYRACUSE", R.string.syracuse_description, R.drawable.syracuse));
}
private void addArtistsToList() {
artists = new ArrayList<String>();
artists.add(db.getArtist(1).getNaam());
artists.add(db.getArtist(2).getNaam());
artists.add(db.getArtist(3).getNaam());
artists.add(db.getArtist(4).getNaam());
artists.add(db.getArtist(5).getNaam());
artists.add(db.getArtist(6).getNaam());
artists.add(db.getArtist(7).getNaam());
artists.add(db.getArtist(8).getNaam());
artists.add(db.getArtist(9).getNaam());
ArtistListAdapter adapter = new ArtistListAdapter(getActivity(), artists, imageId);
listView = (ListView) rootView.findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
}
如果有人能指出我正确的方向,那将是值得赞赏的。提前谢谢!
错误第97行:
artists.add(db.getArtist(1).getNaam());
和错误第66行:
addArtistsToList();
这是DatabaseHelper.java中的getArtist方法:
public Artist getArtist(int artistId) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(DatabaseContract.ArtistEntry.TABLE_NAME, new String[]{DatabaseContract.ArtistEntry._ID,
DatabaseContract.ArtistEntry.COLUMN_ARTIST_NAAM, DatabaseContract.ArtistEntry.COLUMN_BESCHRIJVING, DatabaseContract.ArtistEntry.COLUMN_IMAGE}, DatabaseContract.ArtistEntry._ID + "=?",
new String[]{String.valueOf(artistId)}, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
artist = new Artist(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), Integer.parseInt(cursor.getString(2)), Integer.parseInt(cursor.getString(3)));
}
return artist;
}
答案 0 :(得分:0)
问题是db.getArtist(1)为null。您需要查看数据库并确保数据存在。设置断点并调试代码。
看起来您的代码是在创建视图时创建艺术家。你确定他们正在把它一直带到数据库吗?