Android错误,无法执行android的方法:onClick

时间:2017-02-14 16:33:38

标签: android

当我运行我的应用程序工作正常,但当我点击按钮它停止并继续停止 这是我的代码,设置活动:

package sayed.mohamed.apps.sebha;

import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;

import java.util.ArrayList;

public class Settings extends AppCompatActivity {

    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    public void btn_save(View view) {
        EditText txtname = (EditText) findViewById(R.id.editText);
        /*TextView all_count=(TextView)findViewById(R.id.all_count);
        TextView current_count=(TextView)findViewById(R.id.current);*/
        ListView ls = (ListView) findViewById(R.id.delete_list);
        /*ListView ls2 = (ListView) findViewById(R.id.patterns_list);*/
        DBConnections db = new DBConnections(this);
        db.InsertRowAdmin(txtname.getText().toString());

        ArrayList<String> array_list = db.getAllrecord();
        ls.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, array_list));
        /*ls2.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, array_list));*/
    }
}

这是我的xml文件:

<?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:id="@+id/activity_settings"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    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="sayed.mohamed.apps.sebha.Settings"
    android:background="@drawable/gradient"
    android:orientation="vertical"
    android:duplicateParentState="false"
    android:focusable="false"
    android:hapticFeedbackEnabled="false"
    android:isScrollContainer="false"
    android:keepScreenOn="false"
    android:clipToPadding="false"
    android:clipChildren="false">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_alignWithParentIfMissing="false"
        android:id="@+id/add_layout">

        <TextView
            android:text="@string/add_above"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView2"
            android:layout_weight="0.09"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:textColor="@color/White"/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:id="@+id/editText"
            android:layout_weight="0.09"
            android:inputType="text"
            android:hint="@string/add_hint"
            android:textColorHint="@color/colorPrimaryDark"
            android:layout_marginTop="20dp"
            android:textColor="@color/White"/>

        <Button
            android:text="@string/add_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/add_Button"
            android:layout_weight="0"
            android:paddingTop="0dp"
            android:layout_marginTop="20dp"
            android:layout_gravity="center"
            android:layout_margin="20dp"
            android:onClick="btn_save" />
    </LinearLayout>

    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/delete_layout"
        android:layout_below="@+id/add_layout"
        android:layout_marginTop="50dp">

        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="150dp"
            android:id="@+id/delete_list"
            android:textDirection="rtl"/>

        <Button
            android:text="@string/zero"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/zero_Button"
            android:layout_alignBottom="@+id/delete_list"
            android:layout_gravity="left" />

        <Button
            android:text="@string/delete_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/delete_Button"
            android:layout_marginTop="20dp"
            android:layout_gravity="end"
            android:layout_alignRight="@+id/delete_list"
            android:layout_alignBottom="@+id/delete_list" />

    </RelativeLayout>

</RelativeLayout>

以下是DEBUG中出现的错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: sayed.mohamed.apps.sebha, PID: 2623
                  java.lang.IllegalStateException: Could not execute method for android:onClick
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                      at android.view.View.performClick(View.java:5610)
                      at android.view.View$PerformClick.run(View.java:22265)
                      at android.os.Handler.handleCallback(Handler.java:751)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                   Caused by: java.lang.reflect.InvocationTargetException
                      at java.lang.reflect.Method.invoke(Native Method)
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                      at android.view.View.performClick(View.java:5610) 
                      at android.view.View$PerformClick.run(View.java:22265) 
                      at android.os.Handler.handleCallback(Handler.java:751) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6077) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
                   Caused by: android.database.sqlite.SQLiteException: near "all": syntax error (code 1): , while compiling: create table IF NOT EXISTS Patterns (ID INTEGER primary key,name TEXT,current INTEGER,all INTEGER)
                      at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                      at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                      at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                      at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                      at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                      at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                      at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1677)
                      at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1608)
                      at sayed.mohamed.apps.sebha.DBConnections.onCreate(DBConnections.java:25)
                      at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
                      at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                      at sayed.mohamed.apps.sebha.DBConnections.InsertRowAdmin(DBConnections.java:36)
                      at sayed.mohamed.apps.sebha.Settings.btn_save(Settings.java:44)
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                      at android.view.View.performClick(View.java:5610) 
                      at android.view.View$PerformClick.run(View.java:22265) 
                      at android.os.Handler.handleCallback(Handler.java:751) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6077) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

这是DBConnections java文件:

package sayed.mohamed.apps.sebha;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;

/**
 * Created by M.Sayed on 2/14/2017.
 */

public class DBConnections extends SQLiteOpenHelper {

    public static final String name="patterns.db";
    public static final int Version=1;
    public DBConnections(Context context) {
        super(context, name, null, Version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table IF NOT EXISTS Patterns (ID INTEGER primary key,name TEXT,current INTEGER,all INTEGER)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("Drop table if EXISTS Patterns");
        onCreate(db);
    }

    public void InsertRowAdmin(String name){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();

        contentValues.put("name",name);
        /*contentValues.put("current",current);
        contentValues.put("all",all);*/
        db.insert("Patterns",null, contentValues);
    }
    public ArrayList getAllrecord(){
        ArrayList array_list = new ArrayList();

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res = db.rawQuery("select * from Patterns",null);
        res.moveToFirst();
        while (res.isAfterLast() == false) {
            array_list.add(res.getColumnIndex("name"));
            res.moveToNext();
        }
        return array_list;
    }
}

2 个答案:

答案 0 :(得分:0)

您的SQL create语句出错。 all是SQL中的关键字,因此您不能将其用作此类字段名称。使用一些不同的字段名称或在该名称周围添加引号以避免该错误

答案 1 :(得分:0)

问题来自DBConnections

中的SQL查询
create table IF NOT EXISTS Patterns (ID INTEGER primary key,name TEXT,current INTEGER,all INTEGER)

allreserved word in SQLite,因此我强烈建议您为列使用其他名称,以避免将来出现混淆。

你也可以用引号来逃避它:&#34; all&#34;