显示和插入安卓数据库中的数据时发生java错误
我在安卓数据库中显示和插入数据时遇到问题。我已经阅读了stackoverflow线程,但仍然无法更正错误。这里有人能帮我吗
记录。爪哇
import java.util.ArrayList;
import java.util.List;
import 安卓.app.Activity;
import 安卓.content.Intent;
import 安卓.database.Cursor;
import 安卓.database.sqlite.SQLiteDatabase;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.AdapterView;
import 安卓.widget.ArrayAdapter;
import 安卓.widget.Button;
import 安卓.widget.ListAdapter;
import 安卓.widget.ListView;
import 安卓.widget.Toast;
public class Record extends Activity{
private ListView uGraduateNamesListView;
private Button addNewUndergraduateButton;
// We need some kind of Adapter to made the connection between ListView UI component and SQLite data set.
private ListAdapter uGraduateListAdapter;
// We need this while we read the query using Cursor and pass data
private ArrayList<Mile> mile;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record);
// Initialize UI components
uGraduateNamesListView = (ListView) findViewById(R.id.list);
mile = new ArrayList<Mile>();
// For the third argument, we need a List that contains Strings.
//We decided to display undergraduates names on the ListView.
//Therefore we need to create List that contains undergraduates names
uGraduateListAdapter = new ArrayAdapter<String>(this, 安卓.R.layout.simple_list_item_1, populateList());
uGraduateNamesListView.setAdapter(uGraduateListAdapter);
}
public void showAddForm(View v) {
Intent addNewUndergraduateIntent = new Intent(this, Add.class);
startActivity(addNewUndergraduateIntent);
}
//We are going to do it in the separate method
public List<String> populateList(){
// We have to return a List which contains only String values. Lets create a List first
List<String> uGraduateNamesList = new ArrayList<String>();
// First we need to make contact with the database we have created using the DbHelper class
MySQLiteHelper openHelperClass = new MySQLiteHelper(this);
// Then we need to get a readable database
SQLiteDatabase sqliteDatabase = openHelperClass.getReadableDatabase();
//We need a a guy to read the database query. Cursor interface will do it for us
//(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)
Cursor cursor = sqliteDatabase.query(MySQLiteHelper.TABLE_NAME_Mile, null, null, null, null, null, null);
// Above given query, read all the columns and fields of the table
startManagingCursor(cursor);
// Cursor object read all the fields. So we make sure to check it will not miss any by looping through a while loop
while (cursor.moveToNext()) {
// In one loop, cursor read one undergraduate all details
// Assume, we also need to see all the details of each and every undergraduate
// What we have to do is in each loop, read all the values, pass them to the POJO class
//and create a ArrayList of undergraduates
String date = cursor.getString(cursor.getColumnIndex(MySQLiteHelper.COLUMN_NAME_DATE));
Double miles = cursor.getDouble(cursor.getColumnIndex(MySQLiteHelper.COLUMN_NAME_MILE_TRAVELLED));
String kiosk = cursor.getString(cursor.getColumnIndex(MySQLiteHelper.COLUMN_NAME_KIOSK_COMPANY));
String petrol = cursor.getString(cursor.getColumnIndex(MySQLiteHelper.COLLUMN_NAME_PETROL_TYPE));
double rate = cursor.getDouble(cursor.getColumnIndex(MySQLiteHelper.COLUMN_NAME_RATE));
// Finish reading one raw, now we have to pass them to the POJO
Mile ugPojoClass = new Mile();
ugPojoClass.setDate(date);
ugPojoClass.setMile(miles);
ugPojoClass.setKiosk(kiosk);
ugPojoClass.setPetrol(petrol);
ugPojoClass.setRate(rate);
// Lets pass that POJO to our ArrayList which contains undergraduates as type
mile.add(ugPojoClass);
// But we need a List of String to display in the ListView also.
//That is why we create "uGraduateNamesList"
uGraduateNamesList.add(date);
}
// If you don't close the database, you will get an error
sqliteDatabase.close();
return uGraduateNamesList;
}
// If you don't write the following code, you wont be able to see what you have just insert to the database
@Override
protected void onResume() {
super.onResume();
mile = new ArrayList<Mile>();
uGraduateListAdapter = new ArrayAdapter<String>(this, 安卓.R.layout.simple_list_item_1, populateList());
uGraduateNamesListView.setAdapter(uGraduateListAdapter);
}
@Override
protected void onStart() {
super.onStart();
mile = new ArrayList<Mile>();
uGraduateListAdapter = new ArrayAdapter<String>(this, 安卓.R.layout.simple_list_item_1, populateList());
uGraduateNamesListView.setAdapter(uGraduateListAdapter);
}
// On ListView you just see the name of the undergraduate, not any other details
// Here we provide the solution to that. When the user click on a list item, he will redirect to a page where
//he can see all the details of the undergraduate
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Toast.makeText(getApplicationContext(), "Clicked on :" + arg2, Toast.LENGTH_SHORT).show();
// We want to redirect to another Activity when the user click an item on the ListView
Intent updateDeleteUgraduateIntent = new Intent(this, Update.class);
// We have to identify what object, does the user clicked, because we are going to pass only clicked object details to the next activity
// What we are going to do is, get the ID of the clicked item and get the values from the ArrayList which has
//same array id.
Mile clickedObject = mile.get(arg2);
// We have to bundle the data, which we want to pass to the other activity from this activity
Bundle dataBundle = new Bundle();
dataBundle.putString("clickedDate", clickedObject.getDate());
dataBundle.putDouble("clickedMile", clickedObject.getMile());
dataBundle.putString("clickedKiosk", clickedObject.getKiosk());
dataBundle.putString("clickedPetrol", clickedObject.getPetrol());
dataBundle.putDouble("clickedRate", clickedObject.getRate());
// Attach the bundled data to the intent
updateDeleteUgraduateIntent.putExtras(dataBundle);
// Start the Activity
startActivity(updateDeleteUgraduateIntent);
}
}
我有以下错误
05-23 09:29:03.818: D/gralloc_goldfish(1218): Emulator without GPU emulation detected.
05-23 09:29:58.728: I/Choreographer(1218): Skipped 145 frames! The application may be doing too much work on its main thread.
05-23 09:30:00.778: D/AndroidRuntime(1218): Shutting down VM
05-23 09:30:00.778: W/dalvikvm(1218): threadid=1: thread exiting with uncaught exception (group=0xb2a62ba8)
05-23 09:30:01.038: E/AndroidRuntime(1218): FATAL EXCEPTION: main
05-23 09:30:01.038: E/AndroidRuntime(1218): Process: com.example.sgdriverdiary, PID: 1218
05-23 09:30:01.038: E/AndroidRuntime(1218): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sgdriverdiary/com.example.sgdriverdiary.Record}: java.lang.NullPointerException
05-23 09:30:01.038: E/AndroidRuntime(1218): at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-23 09:30:01.038: E/AndroidRuntime(1218): at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-23 09:30:01.038: E/AndroidRuntime(1218): at 安卓.app.ActivityThread.access$800(ActivityThread.java:135)
05-23 09:30:01.038: E/AndroidRuntime(1218): at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-23 09:30:01.038: E/AndroidRuntime(1218): at 安卓.os.Handler.dispatchMessage(Handler.java:102)
05-23 09:30:01.038: E/AndroidRuntime(1218): at 安卓.os.Looper.loop(Looper.java:136)
05-23 09:30:01.038: E/AndroidRuntime(1218): at 安卓.app.ActivityThread.main(ActivityThread.java:5017)
05-23 09:30:01.038: E/AndroidRuntime(1218): at java.lang.reflect.Method.invokeNative(Native Method)
05-23 09:30:01.038: E/AndroidRuntime(1218): at java.lang.reflect.Method.invoke(Method.java:515)
05-23 09:30:01.038: E/AndroidRuntime(1218): at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-23 09:30:01.038: E/AndroidRuntime(1218): at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-23 09:30:01.038: E/AndroidRuntime(1218): at dalvik.system.NativeStart.main(Native Method)
05-23 09:30:01.038: E/AndroidRuntime(1218): Caused by: java.lang.NullPointerException
05-23 09:30:01.038: E/AndroidRuntime(1218): at com.example.sgdriverdiary.Record.onCreate(Record.java:50)
05-23 09:30:01.038: E/AndroidRuntime(1218): at 安卓.app.Activity.performCreate(Activity.java:5231)
05-23 09:30:01.038: E/AndroidRuntime(1218): at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-23 09:30:01.038: E/AndroidRuntime(1218): at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-23 09:30:01.038: E/AndroidRuntime(1218): ... 11 more
碎片记录。xml
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:paddingBottom="@dimen/activity_vertical_margin"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.sgdriverdiary.Record$PlaceholderFragment"
安卓:background="@drawable/layout_border" >
<RelativeLayout 安卓:id="@+id/relativeLayout1"
安卓:layout_width="fill_parent"
安卓:layout_height="40dp"
安卓:background="#000000"
安卓:orientation="vertical" >
<TextView 安卓:id="@+id/textView1"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_marginLeft="5dp"
安卓:text="Student"
安卓:textAppearance="?安卓:attr/textAppearanceLarge"
安卓:textColor="#FFFFFF" />
<Button 安卓:id="@+id/button1"
安卓:layout_width="41dp"
安卓:layout_height="40dp"
安卓:layout_alignParentRight="true"
安卓:layout_alignParentTop="true"
安卓:background="#454545"
安卓:onClick="showAddForm"
安卓:text="+"
安卓:textColor="#FFFFFF"
安卓:textSize="30sp" />
</RelativeLayout>
<RelativeLayout 安卓:id="@+id/relativeLayout1"
安卓:layout_width="fill_parent"
安卓:layout_height="match_parent"
安卓:layout_alignParentLeft="true"
安卓:layout_below="@+id/relativeLayout1"
安卓:orientation="vertical"
安卓:layout_marginTop="40dp">
<ListView 安卓:id="@+id/list"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:layout_alignParentLeft="true">
</ListView>
</RelativeLayout>
</RelativeLayout>
舱单。xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
package="com.example.sgdriverdiary"
安卓:versionCode="1"
安卓:versionName="1.0" >
<uses-sdk
安卓:minSdkVersion="8"
安卓:targetSdkVersion="19" />
<uses-permission 安卓:name="安卓.permission.INTERNET" />
<uses-permission 安卓:name="安卓.permission.SET_WALLPAPER" />
<uses-permission 安卓:name="安卓.permission.VIBRATE" />
<application
安卓:allowBackup="true"
安卓:icon="@drawable/ic_launcher"
安卓:label="@string/app_name"
安卓:theme="@style/AppTheme" >
<activity
安卓:name="com.example.sgdriverdiary.MainActivity"
安卓:label="@string/app_name" >
<intent-filter>
<action 安卓:name="安卓.intent.action.MAIN" />
<category 安卓:name="安卓.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
安卓:name="com.example.sgdriverdiary.Rate"
安卓:label="@string/title_activity_rate" >
</activity>
<activity
安卓:name="com.example.sgdriverdiary.Cal"
安卓:label="@string/title_activity_cal" >
</activity>
<activity
安卓:name="com.example.sgdriverdiary.Search"
安卓:label="@string/title_activity_search" >
</activity>
<activity
安卓:name="com.example.sgdriverdiary.CustomOnItemSelectedListener"
安卓:label="@string/title_activity_custom_on_item_selected_listener" >
</activity>
<activity
安卓:name="com.example.sgdriverdiary.Record"
安卓:label="@string/title_activity_record" >
</activity>
<activity
安卓:name="com.example.sgdriverdiary.MySQLiteHelper"
安卓:label="@string/title_activity_my_sqlite_helper" >
</activity>
<activity
安卓:name="com.example.sgdriverdiary.Recorddb"
安卓:label="@string/title_activity_recorddb" >
</activity>
<activity
安卓:name="com.example.sgdriverdiary.Update"
安卓:label="@string/title_activity_update" >
</activity>
<activity
安卓:name="com.example.sgdriverdiary.Add"
安卓:label="@string/title_activity_add" >
</activity>
</application>
</manifest>
# 1 楼答案
您的}中的唯一解释
activity_record
布局不包含id为list
的视图。这是NPE在Record
{编辑:您发布的包含
list
的布局被标记为fragment_record.xml
-它不是activity_record
。将listview设置移动到片段onCreateView()
,在那里处理膨胀的rootView
,或者将ListView
移动到活动布局