我正在创建一个数据库中保存数据的应用程序。当我尝试运行代码时,它工作正常,但当我点击打开按钮“我的应用程序崩溃”。我发布下面的代码请看看它
MainActivity.java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Message;
import java.util.HashMap;
/**
* Created by Akash on 3/13/2016.
*/
public class VivzHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "hit_lib1";
private static final String TABLE_NAME = "books";
private static final int DATABASE_VERSION = 7;
private static final String UID = "_id";
private static final String Name = "Name";
private static final String ADDRESS = "Address";
private static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+" ("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+Name+" VARCHAR(25), "+ADDRESS+" VARCHAR(255));";
private static final String DROP_TABLE = "DROP TABLE IF EXISTS "+TABLE_NAME;
private Context context;
public VivzHelper(Context context)
{
super(context,DATABASE_NAME,null,DATABASE_VERSION);
this.context=context;
appscreator.com.mydatabase.Message.message(context, "Constructor Called");
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_TABLE);
appscreator.com.mydatabase.Message.message(context, "onCreate Called");
}
catch (SQLException e) {
appscreator.com.mydatabase.Message.message(context, "" + e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
try {
db.execSQL(DROP_TABLE);
appscreator.com.mydatabase.Message.message(context, "onUpgrade Called");
onCreate(db);
} catch (SQLException e) {
appscreator.com.mydatabase.Message.message(context, "" + e);
}
}
public void insertEntry()
{
try
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
ContentValues values1 = new ContentValues();
// values.put("Messsage", msg);
values1.put("Name", "Aka");
values1.put("Address", "Pu");
values.put("Name", "Pra");
values.put("Address", "Nid");
db.insert("books ", null, values1);
db.insert("books ", null, values);
appscreator.com.mydatabase.Message.message(context, "Inserted Successfully");
}
catch (Exception e)
{
appscreator.com.mydatabase.Message.message(context,""+e);
}
}
public int deleteAll(){
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME, null, null);
}
public Cursor getDetails()
{
SQLiteDatabase db = getReadableDatabase();
return db.rawQuery("select * from books", null);
}
}
VivzHelper.java
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;
/**
* Created by Akash on 3/13/2016.
*/
public class TodoCursorAdapter extends CursorAdapter {
public TodoCursorAdapter(Context context, Cursor cursor, int flags) {
super(context, cursor, 0);
}
// The newView method is used to inflate a new view and return it,
// you don't bind any data to the view at this point.
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.list_view, parent, false);
}
// The bindView method is used to bind all data to a given view
// such as setting the text on a TextView.
@Override
public void bindView(View view, Context context, Cursor cursor) {
// Find fields to populate in inflated template
TextView tvBody = (TextView) view.findViewById(R.id.id);
TextView tvPriority = (TextView) view.findViewById(R.id.name);
TextView address = (TextView) view.findViewById(R.id.add);
// Extract properties from cursor
int body = cursor.getInt(cursor.getColumnIndexOrThrow("body"));
String priority = cursor.getString(cursor.getColumnIndexOrThrow("priority"));
String add = cursor.getString(cursor.getColumnIndexOrThrow("address"));
// Populate fields with extracted properties
tvPriority.setText(String.valueOf(body));
tvBody.setText(priority);
address.setText(add);
}
}
TodoCursorAdapter.java
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
/**
* Created by Akash on 3/13/2016.
*/
public class ShowData extends ListActivity {
private VivzHelper helper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
VivzHelper empClick = new VivzHelper(getApplicationContext());
Cursor cursor = empClick.getDetails();
if (cursor != null)
getListView().setAdapter(
new android.support.v4.widget.SimpleCursorAdapter(this, R.layout.display, cursor, new String[]{
"uid", "Name", "Address"
}, new int[]{R.id.id, R.id.name, R.id.add}, 0));
}
}
ShowData.java
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open"
android:onClick="openinfo"
android:id="@+id/button"
android:layout_below="@+id/textView"
android:layout_alignParentStart="true"
android:layout_marginTop="52dp" />
</RelativeLayout>
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Study cursors"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="3"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="3"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
display.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
list_view.xml
03-13 14:37:29.896 2419-2419/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: appscreator.com.mydatabase, PID: 2419
java.lang.RuntimeException: Unable to start activity ComponentInfo{appscreator.com.mydatabase/appscreator.com.mydatabase.ShowData}: java.lang.IllegalArgumentException: column 'uid' does not exist
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.IllegalArgumentException: column 'uid' does not exist
at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
at android.support.v4.widget.SimpleCursorAdapter.findColumns(SimpleCursorAdapter.java:317)
at android.support.v4.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:92)
at appscreator.com.mydatabase.ShowData.onCreate(ShowData.java:19)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
03-13 14:37:29.904 763-1044/? W/ActivityManager﹕ Force finishing activity 1 appscreator.com.mydatabase/.ShowData
03-13 14:37:29.916 763-1044/? W/ActivityManager﹕ Force finishing activity 2 appscreator.com.mydatabase/.MainActivity
03-13 14:37:29.963 763-784/? W/ViewRootImpl﹕ Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ALT_RIGHT, scanCode=100, metaState=META_ALT_ON|META_ALT_RIGHT_ON, flags=0x8, repeatCount=48208, eventTime=3114859, downTime=610179, deviceId=1, source=0x301 }
03-13 14:37:29.974 763-784/? W/ViewRootImpl﹕ Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ALT_RIGHT, scanCode=100, metaState=META_ALT_ON|META_ALT_RIGHT_ON, flags=0x8, repeatCount=48209, eventTime=3115057, downTime=610179, deviceId=1, source=0x301 }
03-13 14:37:30.058 763-1931/? I/OpenGLRenderer﹕ Initialized EGL, version 1.4
03-13 14:37:30.165 763-1931/? W/EGL_emulation﹕ eglSurfaceAttrib not implemented
03-13 14:37:30.165 763-1931/? W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0x9e824820, error=EGL_SUCCESS
03-13 14:37:30.230 763-784/? W/ViewRootImpl﹕ Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ALT_RIGHT, scanCode=100, metaState=META_ALT_ON|META_ALT_RIGHT_ON, flags=0x8, repeatCount=48210, eventTime=3115112, downTime=610179, deviceId=1, source=0x301 }
03-13 14:37:30.760 763-784/? W/ActivityManager﹕ Activity pause timeout for ActivityRecord{14c9567f u0 appscreator.com.mydatabase/.ShowData t334 f}
03-13 14:37:31.172 1031-1311/? W/EGL_emulation﹕ eglSurfaceAttrib not implemented
03-13 14:37:31.172 1031-1311/? W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa083cf40, error=EGL_SUCCESS
03-13 14:37:32.023 862-862/? W/ResourceType﹕ No package identifier when getting value for resource number 0x00000000
03-13 14:37:32.024 862-862/? W/PackageManager﹕ Failure retrieving resources for appscreator.com.mydatabase: Resource ID #0x0
03-13 14:37:32.025 1031-1311/? W/OpenGLRenderer﹕ Incorrectly called buildLayer on View: ShortcutAndWidgetContainer, destroying layer...
03-13 14:37:32.551 763-821/? W/AudioTrack﹕ AUDIO_OUTPUT_FLAG_FAST denied by client
03-13 14:37:32.598 2419-2419/? I/Process﹕ Sending signal. PID: 2419 SIG: 9
03-13 14:37:32.632 763-803/? W/InputDispatcher﹕ channel '1b803d87 appscreator.com.mydatabase/appscreator.com.mydatabase.MainActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x9
03-13 14:37:32.632 763-803/? E/InputDispatcher﹕ channel '1b803d87 appscreator.com.mydatabase/appscreator.com.mydatabase.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
03-13 14:37:32.632 763-803/? W/InputDispatcher﹕ channel '3de688dd Toast (server)' ~ Consumer closed input channel or an error occurred. events=0x9
03-13 14:37:32.632 763-803/? E/InputDispatcher﹕ channel '3de688dd Toast (server)' ~ Channel is unrecoverably broken and will be disposed!
03-13 14:37:32.634 763-779/? I/ActivityManager﹕ Process appscreator.com.mydatabase (pid 2419) has died
03-13 14:37:32.635 763-780/? I/WindowState﹕ WIN DEATH: Window{1b803d87 u0 appscreator.com.mydatabase/appscreator.com.mydatabase.MainActivity}
03-13 14:37:32.635 763-780/? W/InputDispatcher﹕ Attempted to unregister already unregistered input channel '1b803d87 appscreator.com.mydatabase/appscreator.com.mydatabase.MainActivity (server)'
03-13 14:37:32.644 763-1089/? I/WindowState﹕ WIN DEATH: Window{3de688dd u0 Toast}
03-13 14:37:32.644 763-1089/? W/InputDispatcher﹕ Attempted to unregister already unregistered input channel '3de688dd Toast (server)'
03-13 14:37:32.693 763-1931/? D/OpenGLRenderer﹕ endAllStagingAnimators on 0xa1b9e300 (RippleDrawable) with handle 0x9fa1a9f0
03-13 14:37:32.737 1031-1031/? W/ViewRootImpl﹕ Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ALT_RIGHT, scanCode=100, metaState=META_ALT_ON|META_ALT_RIGHT_ON, flags=0x8, repeatCount=48247, eventTime=3117775, downTime=610179, deviceId=1, source=0x301 }
03-13 14:37:32.738 1031-1031/? W/ViewRootImpl﹕ Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ALT_RIGHT, scanCode=100, metaState=META_ALT_ON|META_ALT_RIGHT_ON, flags=0x8, repeatCount=48248, eventTime=3117834, downTime=610179, deviceId=1, source=0x301 }
03-13 14:37:32.789 1031-1031/? W/ViewRootImpl﹕ Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_ALT_RIGHT, scanCode=100, metaState=META_ALT_ON|META_ALT_RIGHT_ON, flags=0x8, repeatCount=48249, eventTime=3117886, downTime=610179, deviceId=1, source=0x301 }
03-13 14:37:32.924 763-780/? W/InputMethodManagerService﹕ Got RemoteException sending setActive(false) notification to pid 2419 uid 10064
03-13 14:37:33.891 763-763/? W/NotificationService﹕ Object died trying to hide notification android.app.ITransientNotification$Stub$Proxy@2ebdb9bd in package appscreator.com.mydatabase
logcat详细信息
data = ['the', 'way', 'you', 'see', 'people', 'is', 'the', 'way',
'you', 'treat', 'them', 'and', 'the', 'way', 'you', 'treat',
'them', 'is', 'what', 'they', 'become']
sorted_data = sorted(data, key=len)
result = [list(set(group[1])) for group in groupby(sorted_data, key=len)]
'''
[['is'],
['and', 'the', 'see', 'you', 'way'],
['them', 'what', 'they'],
['treat'],
['become', 'people']]
'''
答案 0 :(得分:1)
您需要将View
对象作为参数传递。写下这样的方法:
public void openinfo(View view)
{
Intent intent = new Intent(this, ShowData.class);
startActivity(intent);
}
答案 1 :(得分:-1)
问题已经解决。在判断字段时,我在ShowData.java中犯了一个错误。我声明了UID而不是_id。
谢谢大家