2017-02-14 3 views
-2

Wenn ich meine app laufen funktioniert gut, aber wenn ich auf die Schaltfläche stoppt sie klicken und hält stoping und hier ist mein Code, Einstellungen Aktivität:Android Fehler, konnte nicht Methode für Android ausführen: onClick

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));*/ 
    } 
} 

und hier ist meine xML-Datei:

<?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> 

und hier ist der Fehler, der in DEBUG erscheint:

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) 

und hier ist DBConnections Java-Datei:

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; 
    } 
} 
+1

fügen Sie bitte die volle Fehlerausgabe von Logcat zu Ihrem Beitrag. –

+0

okay, ich habe meine Frage bearbeitet, Fehler hinzugefügt –

+0

Dies ist die Zeile, die Sie betrachten müssen: 'verursacht durch: android.database.sqlite.SQLiteException: in der Nähe von" allen ": Syntaxfehler (Code 1):, beim Kompilieren: Tabelle erstellen WENN NICHT EXISTEN Muster (ID INTEGER Primärschlüssel, Name TEXT, aktuelle INTEGER, alle INTEGER) 'Aber es scheint, dass der Code, den Sie veröffentlicht haben nicht alle notwendigen Details für uns zu debuggen –

Antwort

0

Das Problem kommt von SQL-Abfrage in DBConnections

create table IF NOT EXISTS Patterns (ID INTEGER primary key,name TEXT,current INTEGER,all INTEGER) 

all ein reserved word in SQLite ist, so empfehle ich unter einem anderen Namen für Ihre Spalte, um zukünftige Verwirrung zu vermeiden.

Sie können es auch mit Zitaten wie so entkommen: „alle“

+0

Vielen Dank Andrew –

0

Es ist ein Fehler mit Ihrem SQL-Anweisung erstellen. all ist ein Schlüsselwort in SQL, daher können Sie es nicht als Feldnamen verwenden. entweder etwas anderen Feldnamen oder fügen Sie Anführungszeichen um diesen Namen, die Fehler zu vermeiden

+0

ja, es war das, danke, A.A. –

Verwandte Themen