我正在研究项目总线路线,我需要有关调试部分的帮助。我的表格不是在数据库中创建的,而且我遇到的问题就像点击我的图标时“不幸的是应用程序停止了”。
myactivity.java
public class MainActivity extends Activity {
SQLiteDatabase db;
DbHelper dbHelper;
Button route;
protected String[] columns;
String[] strings = {
"TAMBARAM",
"PERUNGULATHUR",
"VANDALUR",
"URAPAKKAM",
"GUDUVANCHERI",
"POTHERI",
"KATTANKULATHUR",
"M.M.NAGAR",
"CHENGALPET",
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = dbHelper.getReadableDatabase();
try {
} catch (SQLException e) {
e.getMessage();
}
route = (Button) findViewById(R.id.route);
try {
} catch (SQLException e) {
e.printStackTrace();
}
route.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
Cursor cursor = db.rawQuery("select busno from bus_detail);", null);
if (cursor != null && cursor.moveToFirst()) {
TextView places = (TextView) findViewById(R.id.show);
places.setText(cursor.getString(2));
}
}
});
db.close();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,
strings);
AutoCompleteTextView source = (AutoCompleteTextView) findViewById(R.id.source);
AutoCompleteTextView destination = (AutoCompleteTextView) findViewById(R.id.dest);
source.setThreshold(1);
source.setAdapter(adapter);
destination.setThreshold(1);
destination.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
databasehelper.java
public class DbHelper extends SQLiteOpenHelper {
// Database Name
private static final String DATABASE_NAME = "bus_routes.db";
private static final int DATABASE_VERSION = 1;
// table names
private static final String BUS_MASTER = "bus_master";
private static final String BUS_DETAIL = "bus_detail";
// Columns names
private static final String BUS_NO = "bus_no";
private static final String SOURCE = "source";
private static final String DESTINATION = "destination";
private static final String PLACE = "place";
private static final String ARRIVAL_TIME = "arrival_time";
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String CREATE_BUS_MASTER = "CREATE TABLE " + BUS_MASTER + "(" + BUS_NO + " INTEGER PRIMARY KEY," + SOURCE
+ " TEXT," + DESTINATION + " TEXT" + ")";
String CREATE_BUS_DETAIL = "CREATE TABLE " + BUS_DETAIL + "(" + BUS_NO
+ " INTEGER FOREIGN KEY REFERNCES BUS_MASTER(BUS_NO)," + PLACE + " TEXT," + ARRIVAL_TIME + " TEXT"
+ ")";
db.execSQL(CREATE_BUS_MASTER);
db.execSQL(CREATE_BUS_DETAIL);
}
public void onOpen(SQLiteDatabase DB) {
onOpen(DB);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + BUS_MASTER);
db.execSQL("DROP TABLE IF EXISTS " + BUS_DETAIL);
// Create tables again
onCreate(db);
}
public void addBus(int bus_no, String source, String destination) {
// TODO Auto-generated method stub
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("busno", BUS_NO);
values.put("source", SOURCE); // source
values.put("destination", DESTINATION); // destination
// Inserting Row
db.insert(BUS_MASTER, null, values);
}
public void addBus_detail(int bus_no, String place, String arrival_time) {
// TODO Auto-generated method stub
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("busno", BUS_NO);
values.put("place", PLACE);
values.put("arrival_time", ARRIVAL_TIME);
db.insert(BUS_DETAIL, null, values);
}
}
xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="top"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="57dp"
android:text="@string/source"
android:textAppearance="?android:attr/textAppearanceMedium" />
<AutoCompleteTextView
android:id="@+id/source"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView1"
android:layout_alignRight="@+id/dest"
android:layout_toRightOf="@+id/textView2"
android:ems="10"
android:labelFor="2"
android:text="@string/s" />
<requestFocus />
<AutoCompleteTextView
android:id="@+id/dest"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/route"
android:layout_alignParentRight="true"
android:layout_marginRight="16dp"
android:ems="10"
android:text="@string/d" />
<Button
android:id="@+id/route"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_marginTop="26dp"
android:layout_toRightOf="@+id/textView2"
android:text="@string/routes" />
<TextView
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/dest"
android:layout_below="@+id/route"
android:layout_marginTop="81dp"
android:text="@string/disp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="44dp"
android:text="@string/dest"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
logcat的:
04-04 14:14:43.700: D/AndroidRuntime(868): Shutting down VM 04-04 14:14:43.700: W/dalvikvm(868): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 04-04 14:14:43.720: E/AndroidRuntime(868): FATAL EXCEPTION: main 04-04 14:14:43.720: E/AndroidRuntime(868): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bus/com.example.bus.MainActivity}: java.lang.NullPointerException 04-04 14:14:43.720: E/AndroidRuntime(868): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 04-04 14:14:43.720: E/AndroidRuntime(868): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 04-04 14:14:43.720: E/AndroidRuntime(868): at android.app.ActivityThread.access$600(ActivityThread.java:141) 04-04 14:14:43.720: E/AndroidRuntime(868): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 04-04 14:14:43.720: E/AndroidRuntime(868): at android.os.Handler.dispatchMessage(Handler.java:99) 04-04 14:14:43.720: E/AndroidRuntime(868): at android.os.Looper.loop(Looper.java:137) 04-04 14:14:43.720: E/AndroidRuntime(868): at android.app.ActivityThread.main(ActivityThread.java:5041) 04-04 14:14:43.720: E/AndroidRuntime(868): at java.lang.reflect.Method.invokeNative(Native Method) 04-04 14:14:43.720: E/AndroidRuntime(868): at java.lang.reflect.Method.invoke(Method.java:511) 04-04 14:14:43.720: E/AndroidRuntime(868): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 04-04 14:14:43.720: E/AndroidRuntime(868): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 04-04 14:14:43.720: E/AndroidRuntime(868): at dalvik.system.NativeStart.main(Native Method) 04-04 14:14:43.720: E/AndroidRuntime(868): Caused by: java.lang.NullPointerException 04-04 14:14:43.720: E/AndroidRuntime(868): at com.example.bus.MainActivity.onCreate(MainActivity.java:44) 04-04 14:14:43.720: E/AndroidRuntime(868): at android.app.Activity.performCreate(Activity.java:5104) 04-04 14:14:43.720: E/AndroidRuntime(868): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 04-04 14:14:43.720: E/AndroidRuntime(868): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 04-04 14:14:43.720: E/AndroidRuntime(868): ... 11 more