【问题标题】:I'm new in Android. When I'm using this code. My Emulator is getting "Unfortunately Myapp has stopped." [duplicate]我是安卓新手。当我使用此代码时。我的模拟器收到“不幸的是 Myapp 已停止”。 [复制]
【发布时间】:2015-11-19 00:23:44
【问题描述】:

我是 Android 新手。当我使用此代码时。我的模拟器收到“不幸的是 Myapp 已停止”。 在这段代码中,我试图在 Sqlite 中添加一些普通人的详细信息,并从 Sqlite 中选择和删除。 这是我正在使用的 XML 文件。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="First Name:"
        android:id="@+id/textView" />

    <EditText
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Last Name:"
        android:id="@+id/textlastname" />

    <EditText
        android:id="@+id/editlastname"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Gender:"
        android:id="@+id/textgender" />

    <EditText
        android:id="@+id/editgender"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Age:"
        android:id="@+id/textage" />

    <EditText
        android:id="@+id/editage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Address:"
        android:id="@+id/textaddress" />

    <EditText
        android:id="@+id/editaddress"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

    <Button
        android:id="@+id/add"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Add New Person"
        />
    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />
</LinearLayout>

这是我正在使用的 MainActivity.java 文件。

    package com.example.gaurang.myhomes;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

class MyDbHelper extends SQLiteOpenHelper {

    private static final String DB_NAME = "mydb";
    private static final int DB_VERSION = 1;

    public static final String TABLE_NAME = "students";
    public static final String COL_NAME = "pName";
    public static final String COL_LAST = "pLast";
    public static final String COL_GENDER = "pGender";
    public static final String COL_AGE = "pAge";
    public static final String COL_ADDRESS = "pAddress";
    public static final String COL_DATE = "pDate";
    private static final String STRING_CREATE = "CREATE TABLE "+TABLE_NAME+"             (_id INTEGER PRIMARY KEY AUTOINCREMENT, "
            +COL_NAME+" TEXT, "+COL_LAST+" TEXT, "+COL_GENDER+" TEXT,     "+COL_AGE+" TEXT, "+COL_ADDRESS+" TEXT, "+COL_DATE+" DATE);";

    public MyDbHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(STRING_CREATE);
        ContentValues cv = new ContentValues(6);
        cv.put(COL_NAME, "New Entry");
        cv.put(COL_LAST, "New Entry");
        cv.put(COL_GENDER, "New Entry");
        cv.put(COL_AGE, "New Entry");
        cv.put(COL_ADDRESS, "New Entry");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd     HH:mm:ss");
        cv.put(COL_DATE, dateFormat.format(new Date()));
        db.insert(TABLE_NAME, null, cv);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)         {
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
        onCreate(db);
    }
}

public class MainActivity extends AppCompatActivity implements     View.OnClickListener, AdapterView.OnItemClickListener {
    EditText mText;
    EditText mLast;
    EditText mGender;
    EditText mAge;
    EditText mAddress;
    Button mAdd;
    ListView mList;

    MyDbHelper mHelper;
    SQLiteDatabase mDb;
    Cursor mCursor;
    SimpleCursorAdapter mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mText = (EditText)findViewById(R.id.name);
        mLast = (EditText)findViewById(R.id.editlastname);
        mGender = (EditText)findViewById(R.id.editgender);
        mAge = (EditText)findViewById(R.id.editage);
        mAddress = (EditText)findViewById(R.id.editaddress);
        mAdd = (Button)findViewById(R.id.add);
        mAdd.setOnClickListener(this);
        mList = (ListView)findViewById(R.id.list);
        mList.setOnItemClickListener(this);

        mHelper = new MyDbHelper(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        mDb = mHelper.getWritableDatabase();
        String[] columns = new String[] {"_id",     MyDbHelper.COL_NAME,MyDbHelper.COL_LAST,MyDbHelper.COL_GENDER,MyDbHelper.COL_AGE    ,MyDbHelper.COL_ADDRESS, MyDbHelper.COL_DATE};
        mCursor = mDb.query(MyDbHelper.TABLE_NAME, columns, null, null,     null, null, null, null);
        String[] headers = new String[]     {MyDbHelper.COL_NAME,MyDbHelper.COL_LAST,MyDbHelper.COL_GENDER,MyDbHelper.COL_AG    E,MyDbHelper.COL_ADDRESS, MyDbHelper.COL_DATE};
        mAdapter = new SimpleCursorAdapter(this,     android.R.layout.two_line_list_item,
                mCursor, headers, new int[]{android.R.id.text1,     android.R.id.text2});
        mList.setAdapter(mAdapter);
    }

    @Override
    public void onPause() {
        super.onPause();
        mDb.close();
        mCursor.close();
    }
    @Override
    public void onClick(View v) {
        ContentValues cv = new ContentValues(6);
        cv.put(MyDbHelper.COL_NAME, mText.getText().toString());
        cv.put(MyDbHelper.COL_LAST, mLast.getText().toString());
        cv.put(MyDbHelper.COL_GENDER, mGender.getText().toString());
        cv.put(MyDbHelper.COL_AGE, mAge.getText().toString());
        cv.put(MyDbHelper.COL_ADDRESS, mAddress.getText().toString());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd     HH:mm:ss");
        cv.put(MyDbHelper.COL_DATE, dateFormat.format(new Date())); //Insert     'now' as the date
        mDb.insert(MyDbHelper.TABLE_NAME, null, cv);
        mCursor.requery();
        mAdapter.notifyDataSetChanged();
        mText.setText(null);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position,     long id) {
        mCursor.moveToPosition(position);
        String rowId = mCursor.getString(0); //Column 0 of the cursor is the     id
        mDb.delete(MyDbHelper.TABLE_NAME, "_id = ?", new String[]{rowId});
        mCursor.requery();
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is     present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

    //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

【问题讨论】:

  • 查看日志文件——那里应该有异常。

标签: java android eclipse sqlite import


【解决方案1】:
 "+COL_AGE+" TEXT, "+COL_ADDRESS+" TEXT, "+COL_DATE+" DATE);";

Android 上的 SQL Lite 不直接支持 DATE 列。您可以使用 TEXT 列或 INTEGER 列来存储它。

更多详情:what are Datatypes in SQLite supporting android

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 2016-12-12
    • 1970-01-01
    相关资源
    最近更新 更多