我是安卓新手。当我使用此代码时。我的模拟器收到“不幸的是 Myapp 已停止”。 [复制]

Posted

技术标签:

【中文标题】我是安卓新手。当我使用此代码时。我的模拟器收到“不幸的是 Myapp 已停止”。 [复制]【英文标题】:I'm new in Android. When I'm using this code. My Emulator is getting "Unfortunately Myapp has stopped." [duplicate] 【发布时间】: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_
    android:layout_>

    <TextView
        android:layout_
        android:layout_
        android:text="First Name:"
        android:id="@+id/textView" />

    <EditText
        android:id="@+id/name"
        android:layout_
        android:layout_
        />
    <TextView
        android:layout_
        android:layout_
        android:text="Last Name:"
        android:id="@+id/textlastname" />

    <EditText
        android:id="@+id/editlastname"
        android:layout_
        android:layout_
        />
    <TextView
        android:layout_
        android:layout_
        android:text="Gender:"
        android:id="@+id/textgender" />

    <EditText
        android:id="@+id/editgender"
        android:layout_
        android:layout_
        />
    <TextView
        android:layout_
        android:layout_
        android:text="Age:"
        android:id="@+id/textage" />

    <EditText
        android:id="@+id/editage"
        android:layout_
        android:layout_
        />
    <TextView
        android:layout_
        android:layout_
        android:text="Address:"
        android:id="@+id/textaddress" />

    <EditText
        android:id="@+id/editaddress"
        android:layout_
        android:layout_
        />

    <Button
        android:id="@+id/add"
        android:layout_
        android:layout_
        android:text="Add New Person"
        />
    <ListView
        android:id="@+id/list"
        android:layout_
        android:layout_
        />
</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);
    

【问题讨论】:

查看日志文件——那里应该有异常。 【参考方案1】:
 "+COL_AGE+" TEXT, "+COL_ADDRESS+" TEXT, "+COL_DATE+" DATE);";

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

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

【讨论】:

以上是关于我是安卓新手。当我使用此代码时。我的模拟器收到“不幸的是 Myapp 已停止”。 [复制]的主要内容,如果未能解决你的问题,请参考以下文章

我是Laravel的新手,我不断收到此错误,不知道如何解决

代码未在设备/模拟器上运行

获取位置(纬度和经度)

创建安卓模拟器

CLLocationManager 没有收到更新

Android 应用程序未安装在模拟器或移动设备中