模擬器運行后界面顯示正常:

但是點擊按鈕就任務結束:

這是activity_main:
<?xml version="1.0" encoding="utf-8"?>
<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:background="@drawable/bg"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/ll_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/ll_phone"
android:layout_alignLeft="@+id/ll_btn"
android:layout_alignStart="@+id/ll_btn">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="姓 名:"/>
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:hint="請輸入姓名"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ll_phone"
android:layout_marginBottom="10dp"
android:layout_above="@+id/ll_btn"
android:layout_alignLeft="@+id/ll_name"
android:layout_alignStart="@+id/ll_name">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="電 話:"/>
<EditText
android:id="@+id/et_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:hint="請輸入手機號碼"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ll_btn"
android:layout_centerVertical="true">
<Button
android:id="@+id/btn_add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18sp"
android:background="#B9B9FF"
android:layout_marginRight="2dp"
android:text="添加"/>
<Button
android:id="@+id/btn_query"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18sp"
android:background="#DCB5FF"
android:layout_marginRight="2dp"
android:text="查詢"/>
<Button
android:id="@+id/btn_update"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18sp"
android:background="#E6CAFF"
android:layout_marginRight="2dp"
android:text="修改"/>
<Button
android:id="@+id/btn_delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18sp"
android:background="#ACD6FF"
android:text="洗掉"/>
</LinearLayout>
<TextView
android:id="@+id/tv_show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_below="@+id/ll_btn"
android:textSize="20sp"/>
</RelativeLayout>
這是MainActivity:
package cn.itcast.directory;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
MyHelper myHelper;
private EditText mEtName;
private EditText mEtPhone;
private TextView mTvShow;
private Button mBtnAdd;
private Button mBtnQuery;
private Button mBtnUpdate;
private Button mBtnDelate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myHelper = new MyHelper(this);
init();
}
private void init() {
mEtName = (EditText) findViewById(R.id.et_name);
mEtPhone = (EditText) findViewById(R.id.et_phone);
mTvShow = (TextView) findViewById(R.id.tv_show);
mBtnAdd = (Button) findViewById(R.id.btn_add);
mBtnQuery = (Button) findViewById(R.id.btn_query);
mBtnUpdate = (Button) findViewById(R.id.btn_update);
mBtnDelate = (Button) findViewById(R.id.btn_delete);
mBtnAdd.setOnClickListener(this);
mBtnQuery.setOnClickListener(this);
mBtnUpdate.setOnClickListener(this);
mBtnDelate.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String name;
String phone;
SQLiteDatabase db;
ContentValues values;
switch (v.getId()) {
case R.id.btn_add:
name = mEtName.getText().toString();
phone = mEtPhone.getText().toString();
db = myHelper.getWritableDatabase();
values = new ContentValues();
values.put("name",name);
db.insert("information",null,values);
Toast.makeText(this,"資訊已添加",Toast.LENGTH_SHORT).show();
db.close();
break;
case R.id.btn_query:
db = myHelper.getReadableDatabase();
Cursor cursor = db.query("information",null,null,null,null,null,null);
if (cursor.getCount() == 0) {
mTvShow.setText("");
Toast.makeText(this,"沒有資料",Toast.LENGTH_SHORT).show();
} else {
cursor.moveToFirst();
mTvShow.setText("Name :" + cursor.getString(1)+" : Tel :" + cursor.getString(2));
}
cursor.close();
db.close();
break;
case R.id.btn_update:
db = myHelper.getWritableDatabase();
values = new ContentValues();
values.put("phone",phone = mEtPhone.getText().toString());
db.update("information",values,"name=?",new String[]{mEtName.getText().toString()});
Toast.makeText(this,"資訊已修改",Toast.LENGTH_SHORT).show();
db.close();
break;
case R.id.btn_delete:
db = myHelper.getWritableDatabase();
db.delete("information",null,null);
Toast.makeText(this,"資訊已洗掉",Toast.LENGTH_SHORT).show();
mTvShow.setText("");
db.close();
break;
}
}
class MyHelper extends SQLiteOpenHelper {
public MyHelper(Context context) {
super(context,"itcast.db",null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE information(_id INTEGER PRIMARY KEY AUTOTNCREMENT,name VARCHAR(20),phone VARCHAR(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
}
}
}
uj5u.com熱心網友回復:
求大佬幫忙看看怎么解決!uj5u.com熱心網友回復:
用真機除錯下 抓下adb loguj5u.com熱心網友回復:
貼logvat里面的錯誤日志uj5u.com熱心網友回復:
log cat(一直都是這段話回圈):05-21 11:23:16.980 2267-3473/com.google.android.googlequicksearchbox:search I/AudioController: internalShutdown
05-21 11:23:16.982 2267-2267/com.google.android.googlequicksearchbox:search I/MicroDetector: Keeping mic open: false
05-21 11:23:16.982 2267-2267/com.google.android.googlequicksearchbox:search I/MicroDetectionWorker: #onError(false)
05-21 11:23:16.982 2267-3472/com.google.android.googlequicksearchbox:search I/DeviceStateChecker: DeviceStateChecker cancelled
05-21 11:23:20.759 2460-2883/com.google.android.gms I/Icing: IndexChimeraService.getServiceInterface callingPackage=com.google.android.music componentName=null serviceId=SEARCH_CORPORA
05-21 11:23:20.769 2460-3334/com.google.android.gms I/Icing: Usage reports ok 0, Failed Usage reports 0, indexed 0, rejected 0, imm upload false
05-21 11:23:20.790 2460-2883/com.google.android.gms I/Icing: IndexChimeraService.getServiceInterface callingPackage=com.google.android.music componentName=null serviceId=SEARCH_CORPORA
05-21 11:23:20.798 2460-3334/com.google.android.gms I/Icing: Usage reports ok 0, Failed Usage reports 0, indexed 0, rejected 0, imm upload false
05-21 11:23:20.820 2460-2946/com.google.android.gms I/Icing: IndexChimeraService.getServiceInterface callingPackage=com.google.android.music componentName=null serviceId=SEARCH_CORPORA
05-21 11:23:20.830 2460-3334/com.google.android.gms I/Icing: Usage reports ok 0, Failed Usage reports 0, indexed 0, rejected 0, imm upload false
05-21 11:23:20.836 2460-2946/com.google.android.gms I/Icing: IndexChimeraService.getServiceInterface callingPackage=com.google.android.music componentName=null serviceId=SEARCH_CORPORA
05-21 11:23:20.853 2460-3334/com.google.android.gms I/Icing: Usage reports ok 0, Failed Usage reports 0, indexed 0, rejected 0, imm upload false
05-21 11:23:20.860 2460-2883/com.google.android.gms I/Icing: IndexChimeraService.getServiceInterface callingPackage=com.google.android.music componentName=null serviceId=SEARCH_CORPORA
05-21 11:23:20.870 2460-3334/com.google.android.gms I/Icing: Usage reports ok 0, Failed Usage reports 0, indexed 0, rejected 0, imm upload false
05-21 11:23:20.876 2460-2946/com.google.android.gms I/Icing: IndexChimeraService.getServiceInterface callingPackage=com.google.android.music componentName=null serviceId=SEARCH_CORPORA
05-21 11:23:20.884 2460-3334/com.google.android.gms I/Icing: Usage reports ok 0, Failed Usage reports 0, indexed 0, rejected 0, imm upload false
05-21 11:23:20.889 2460-2883/com.google.android.gms I/Icing: IndexChimeraService.getServiceInterface callingPackage=com.google.android.music componentName=null serviceId=SEARCH_CORPORA
05-21 11:23:20.901 2460-3334/com.google.android.gms I/Icing: Usage reports ok 0, Failed Usage reports 0, indexed 0, rejected 0, imm upload false
05-21 11:23:21.989 2267-2267/com.google.android.googlequicksearchbox:search I/MicroDetectionWorker: Micro detection mode: [mDetectionMode: [1]].
05-21 11:23:21.989 2267-2267/com.google.android.googlequicksearchbox:search I/AudioController: Using mInputStreamFactoryBuilder
05-21 11:23:21.992 2267-3481/com.google.android.googlequicksearchbox:search I/MicroRecognitionRunner: Starting detection.
05-21 11:23:21.993 2267-2346/com.google.android.googlequicksearchbox:search I/MicrophoneInputStream: mic_starting com.google.android.apps.gsa.staticplugins.z.c@3abd3bb
05-21 11:23:21.996 1407-3484/? I/AudioFlinger: AudioFlinger's thread 0xb2703d00 ready to run
05-21 11:23:22.005 2267-2346/com.google.android.googlequicksearchbox:search I/MicrophoneInputStream: mic_started com.google.android.apps.gsa.staticplugins.z.c@3abd3bb
05-21 11:23:22.006 2267-2346/com.google.android.googlequicksearchbox:search E/ActivityThread: Failed to find provider info for com.google.android.apps.gsa.testing.ui.audio.recorded
05-21 11:23:22.006 2267-2267/com.google.android.googlequicksearchbox:search I/MicroDetectionWorker: onReady
05-21 11:23:22.009 2267-2346/com.google.android.googlequicksearchbox:search I/MicrophoneInputStream: mic_close com.google.android.apps.gsa.staticplugins.z.c@3abd3bb
05-21 11:23:22.010 2267-3481/com.google.android.googlequicksearchbox:search I/MicroRecognitionRunner: Detection finished
05-21 11:23:22.010 2267-3481/com.google.android.googlequicksearchbox:search W/ErrorReporter: reportError [type: 211, code: 524300]: Error reading from input stream
05-21 11:23:22.010 2267-2448/com.google.android.googlequicksearchbox:search I/MicroRecognitionRunner: Stopping hotword detection.
05-21 11:23:22.010 2267-3481/com.google.android.googlequicksearchbox:search W/ErrorProcessor: onFatalError, processing error from engine(4)
com.google.android.apps.gsa.shared.speech.a.g: Error reading from input stream
at com.google.android.apps.gsa.staticplugins.recognizer.i.a.a(SourceFile:342)
at com.google.android.apps.gsa.staticplugins.recognizer.i.a$1.run(SourceFile:1367)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at com.google.android.apps.gsa.shared.util.concurrent.a.ak.run(SourceFile:66)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
at com.google.android.apps.gsa.shared.util.concurrent.a.ad$1.run(SourceFile:85)
Caused by: com.google.android.apps.gsa.shared.exception.GsaIOException: Error code: 393238 | Buffer overflow, no available space.
at com.google.android.apps.gsa.speech.audio.Tee.g(SourceFile:2531)
at com.google.android.apps.gsa.speech.audio.ap.read(SourceFile:555)
at java.io.InputStream.read(InputStream.java:101)
at com.google.android.apps.gsa.speech.audio.al.run(SourceFile:362)
at com.google.android.apps.gsa.speech.audio.ak$1.run(SourceFile:471)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at com.google.android.apps.gsa.shared.util.concurrent.a.ak.run(SourceFile:66)
at com.google.android.apps.gsa.shared.util.concurrent.a.ax.run(SourceFile:139)
at com.google.android.apps.gsa.shared.util.concurrent.a.ax.run(SourceFile:139)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
at com.google.android.apps.gsa.shared.util.concurrent.a.ad$1.run(SourceFile:85)
uj5u.com熱心網友回復:
閃退沒Log嗎,不會過濾就去看看版主大神的文章https://blog.csdn.net/weimingjue/article/details/87921494
uj5u.com熱心網友回復:
Android操作資料庫需要在子執行緒中執行。你直接在主執行緒中操作資料庫當然報錯誤了。修改一下資料庫操作就可以了。uj5u.com熱心網友回復:
錯誤日志發的不對,再看看教程。idea的黃色提示或者紅線提示要仔細看看,如你的單詞AUTOTNCREMENT,這都能打錯
uj5u.com熱心網友回復:
只能在子執行緒中執行?我咋沒聽說過這個說法,一般是單純的非耗時資料庫操作,我都是在主執行緒操作的,除了占用了資源之外,并未報錯過,我只知道,資料庫不能做并發寫入操作,還真不知道不能在主執行緒操作。
uj5u.com熱心網友回復:
uj5u.com熱心網友回復:
Android操作資料庫需要在子執行緒中執行。你直接在主執行緒中操作資料庫當然報錯誤了。修改一下資料庫操作就可以了。
只能在子執行緒中執行?我咋沒聽說過這個說法,一般是單純的非耗時資料庫操作,我都是在主執行緒操作的,除了占用了資源之外,并未報錯過,我只知道,資料庫不能做并發寫入操作,還真不知道不能在主執行緒操作。
我還真沒注意過這個,我做的全域資料庫管理,專門給資料庫的創建和更新預留了執行緒,但是資料庫的讀取和寫入都是同步操作,從不用子執行緒異步操作
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/30511.html
標籤:Android
上一篇:Qt Creator構建失敗
