我是Android开发的新手,我正在学习使用SQLite数据库,当我创建数据库并从中检索数据时,此错误会显示出来。 如果您需要查看其他任何代码,请告诉我。
这是我的mainActivity:
public static final String LOGTAG = "EMPLOYEES";
public static final String Name = "name";
public static final String Pic = "pic";
public static final String Position = "position";
public static final String Qualification = "qualification";
public static final String Expertise = "expertise";
public static final String Contact = "contact";
public static final int DETAIL_REQUEST_CODE = 1001;
protected List<TeamDetails> data;
private SharedPreferences settings;
private SharedPreferences.OnSharedPreferenceChangeListener listener;
EmployeeDataSource datasource;
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
datasource = new EmployeeDataSource(this);
datasource.open();
List<TeamDetails> teamDetails = datasource.findAll();
if (teamDetails.size() == 0) {
createData();
teamDetails = datasource.findAll();
}
ArrayAdapter<TeamDetails> courseArrayAdapter =
new nameArrayAdapter(this, 0, data);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(courseArrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TeamDetails teamDetails = data.get(position);
displayDetail(teamDetails);
}
});
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
@Override
protected void onResume() {
super.onResume();
datasource.open();
}
@Override
protected void onPause() {
super.onPause();
datasource.close();
}
private void createData() {
TeamDetails teamDetails = new TeamDetails();
teamDetails.setName("M Jaleed");
teamDetails.setPosition("Team Lead");
teamDetails.setQualities("Masters");
teamDetails.setExpertise("Android Development");
teamDetails.setContact(03331234567);
teamDetails.setImage("");
teamDetails = datasource.create(teamDetails);
Log.i(LOGTAG, "Details has been added" + teamDetails.getId());
teamDetails = new TeamDetails();
teamDetails.setName("Sajawal Nawaz");
teamDetails.setPosition("Internee");
teamDetails.setQualities("Bachelors");
teamDetails.setExpertise("Android Development");
teamDetails.setContact(03331234567);
teamDetails.setImage("");
teamDetails = datasource.create(teamDetails);
Log.i(LOGTAG, "Details has been added" + teamDetails.getId());
teamDetails = new TeamDetails();
teamDetails.setName("Waqas Khan");
teamDetails.setPosition("Internee");
teamDetails.setQualities("Bachelors");
teamDetails.setExpertise("Android Development");
teamDetails.setContact(03331234567);
teamDetails.setImage("");
teamDetails = datasource.create(teamDetails);
Log.i(LOGTAG, "Details has been added" + teamDetails.getId());
teamDetails = new TeamDetails();
teamDetails.setName("Waqas Khan Swat");
teamDetails.setPosition("Internee");
teamDetails.setQualities("Bachelors");
teamDetails.setExpertise("Android Development");
teamDetails.setContact(03331234567);
teamDetails.setImage("");
teamDetails = datasource.create(teamDetails);
Log.i(LOGTAG, "Details has been added" + teamDetails.getId());
teamDetails = new TeamDetails();
teamDetails.setName("Arslan Shah");
teamDetails.setPosition("Internee");
teamDetails.setQualities("Bachelors");
teamDetails.setExpertise("Android Development");
teamDetails.setContact(03331234567);
teamDetails.setImage("");
teamDetails = datasource.create(teamDetails);
Log.i(LOGTAG, "Details has been added" + teamDetails.getId());
}
private void displayDetail(TeamDetails teamDetails) {
Intent intent = new Intent(this, DetailActivity.class);
intent.putExtra(Name, teamDetails.getName());
intent.putExtra(Pic, teamDetails.getImage());
intent.putExtra(Position, teamDetails.getPosition());
intent.putExtra(Qualification, teamDetails.getQualities());
intent.putExtra(Expertise, teamDetails.getExpertise());
intent.putExtra(Contact, teamDetails.getContact());
startActivityForResult(intent, DETAIL_REQUEST_CODE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onStart() {
super.onStart();
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW,
"Main Page",
Uri.parse("http://host/path"),
Uri.parse("android-app://com.example.sj.dgapps3/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}
@Override
public void onStop() {
super.onStop();
Action viewAction = Action.newAction(
Action.TYPE_VIEW,
"Main Page",
Uri.parse("http://host/path"),
Uri.parse("android-app://com.example.sj.dgapps3/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
class nameArrayAdapter extends ArrayAdapter<TeamDetails> {
Context context;
List<TeamDetails> objects;
public nameArrayAdapter(Context context, int resource, List<TeamDetails> objects) {
super(context, resource, objects);
this.context = context;
this.objects = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TeamDetails teamDetails = objects.get(position);
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.list, null);
TextView tv = (TextView) view.findViewById(R.id.name);
tv.setText(teamDetails.getName());
ImageView iv = (ImageView) view.findViewById(R.id.empPic);
int res = context.getResources().getIdentifier(teamDetails.getImage(), "drawable", context.getPackageName()
);
iv.setImageResource(res);
return view;
}
}
这是错误日志
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sj.dgapps3, PID: 1641
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sj.dgapps3/com.example.sj.dgapps3.MainActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:337)
at android.widget.ListView.setAdapter(ListView.java:491)
at com.example.sj.dgapps3.MainActivity.onCreate(MainActivity.java:104)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
这是我的数据源类
package DgApps3.db;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.example.sj.dgapps3.TeamDetails;
import java.util.ArrayList;
import java.util.List;
/**
* Created by SJ on 4/14/2016.
*/
public class EmployeeDataSource {
public static final String LOGTAG = "EMPLOYEES";
SQLiteOpenHelper dbhelper;
SQLiteDatabase database;
private static final String[] allColumns = {
DgApps3DBOpenHelper.COLUMN_ID,
DgApps3DBOpenHelper.COLUMN_NAME,
DgApps3DBOpenHelper.COLUMN_POSITION,
DgApps3DBOpenHelper.COLUMN_PIC,
DgApps3DBOpenHelper.COLUMN_EXPERTISE,
DgApps3DBOpenHelper.COLUMN_QUALIFICATION,
DgApps3DBOpenHelper.COLUMN_CONTACT
};
public EmployeeDataSource(Context context) {
dbhelper = new DgApps3DBOpenHelper(context);
//database = dbhelper.getWritableDatabase();
}
public void open() {
Log.i(LOGTAG, "Database opened");
database = dbhelper.getWritableDatabase();
}
public void close() {
Log.i(LOGTAG, "Database closed");
dbhelper.close();
}
public TeamDetails create(TeamDetails teamDetails) {
ContentValues values = new ContentValues();
values.put(DgApps3DBOpenHelper.COLUMN_NAME, teamDetails.getName());
values.put(DgApps3DBOpenHelper.COLUMN_POSITION, teamDetails.getPosition());
values.put(DgApps3DBOpenHelper.COLUMN_PIC, teamDetails.getImage());
values.put(DgApps3DBOpenHelper.COLUMN_QUALIFICATION, teamDetails.getQualities());
values.put(DgApps3DBOpenHelper.COLUMN_EXPERTISE, teamDetails.getExpertise());
values.put(DgApps3DBOpenHelper.COLUMN_CONTACT, teamDetails.getContact());
long insertid = database.insert(DgApps3DBOpenHelper.TABLE_EMPLOYEES, null, values);
teamDetails.setId(insertid);
return teamDetails;
}
public List<TeamDetails> findAll() {
List<TeamDetails> teamDetails = new ArrayList<TeamDetails>();
Cursor cursor = database.query(DgApps3DBOpenHelper.TABLE_EMPLOYEES, allColumns,
null, null, null, null, null);
Log.i(LOGTAG, "Returned" + cursor.getCount() + " rows ");
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
TeamDetails teamDetail = new TeamDetails();
teamDetail.setId(cursor.getLong(cursor.getColumnIndex(DgApps3DBOpenHelper.COLUMN_ID)));
teamDetail.setName(cursor.getString(cursor.getColumnIndex(DgApps3DBOpenHelper.COLUMN_NAME)));
teamDetail.setPosition(cursor.getString(cursor.getColumnIndex(DgApps3DBOpenHelper.COLUMN_POSITION)));
teamDetail.setImage(cursor.getString(cursor.getColumnIndex(DgApps3DBOpenHelper.COLUMN_PIC)));
teamDetail.setQualities(cursor.getString(cursor.getColumnIndex(DgApps3DBOpenHelper.COLUMN_QUALIFICATION)));
teamDetail.setExpertise(cursor.getString(cursor.getColumnIndex(DgApps3DBOpenHelper.COLUMN_EXPERTISE)));
teamDetail.setContact(cursor.getDouble(cursor.getColumnIndex(DgApps3DBOpenHelper.COLUMN_CONTACT)));
teamDetails.add(teamDetail);
}
}
return teamDetails;
}
}
答案 0 :(得分:0)
如果你仔细阅读了堆栈跟踪,你会看到在List上调用size方法时会发生错误。你可能会说,你永远不会调用size方法。没错,但Android确实......
看看这段代码
protected List<TeamDetails> data;
你有没有初始化那个名单?不,所以它是空的。那么,你觉得这个怎么样?
ArrayAdapter<TeamDetails> courseArrayAdapter =
new nameArrayAdapter(this, 0, data);
它不会,因此错误。要修复它,请使用适配器中的正确列表,或在将其放入和适配器
之前初始化该列表