Home > Mobile >  java.lang.RuntimeException: Unable to start activity ComponentInfo
java.lang.RuntimeException: Unable to start activity ComponentInfo

Time:12-20

**java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ardaak.nottutma/com.ardaak.nottutma.MainActivity}: android.database.sqlite.SQLiteException: near "TABLENot": syntax error (code 1 SQLITE_ERROR): , while compiling: CREATE TABLENot(Counter INTEGER PRIMARY KEY AUTOINCREMENT, id INT, baslik TEXT, aciklama TEXT, silinmisTEXT)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: android.database.sqlite.SQLiteException: near "TABLENot": syntax error (code 1 SQLITE_ERROR): , while compiling: CREATE TABLENot(Counter INTEGER PRIMARY KEY AUTOINCREMENT, id INT, baslik TEXT, aciklama TEXT, silinmisTEXT)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1045)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:652)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:61)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:33)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1919)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1841)
at com.ardaak.nottutma.SQLite.onCreate(SQLite.java:68)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:411)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:340)
# ```at com.ardaak.nottutma.SQLite.populateNotListArray(SQLite.java:101)
at com.ardaak.nottutma.MainActivity.VeritabanindanMemorye(MainActivity.java:45)
at com.ardaak.nottutma.MainActivity.onCreate(MainActivity.java:20)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:223) 
at android.app.ActivityThread.main(ActivityThread.java:7656) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
2022-12-19 03:24:15.800 9217-9217/com.ardaak.nottutma I/Process: Sending signal. PID: 9217 SIG: 9**

This is MainActivity.java**

package com.ardaak.nottutma;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity
{
    private ListView notListview;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initWidgets();
        VeritabanindanMemorye();
        setNotUyarlama();
    }

    private void setNotUyarlama()
    {
        NotUyarlama notUyarlama = new NotUyarlama(getApplicationContext(),Not.notArrayList);
        notListview.setAdapter(notUyarlama);
    }

    private void initWidgets()
    {
        notListview = findViewById(R.id.lstNotlarim);
    }

    public void YeniNot(View view)
    {
        Intent YeniNotIntent = new Intent(this,NotDuzenleme.class);
        startActivity(YeniNotIntent);
    }
    public void VeritabanindanMemorye()
    {
        SQLite sqLite = SQLite.instanceOfDatabase(this);
        sqLite.populateNotListArray();
    }
}

This is MainActivity.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/KoyuYesil"
        android:minHeight="?actionBarSize"
        android:theme="?actionBarTheme">

        <TextView
            android:id="@ id/txtNotlarim"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="NOTLARIM"
            android:textColor="@color/Gri"
            android:textSize="50sp"
            android:textStyle="bold" />
        <Button
            android:id="@ id/btnEkle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:onClick="YeniNot"
            android:text="Ekle  "
            android:textColor="@color/Sari" />


    </androidx.appcompat.widget.Toolbar>


    <ListView
        android:id="@ id/lstNotlarim"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:divider="@color/teal_700"
        android:dividerHeight="4dp" />

</LinearLayout>

NotDuzenleme class same again its NoteDetailActivity

    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;
    
    public class NotDuzenleme extends AppCompatActivity
    {
        private EditText baslikEditText,aciklamaEditText;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_not_duzenleme);
            initWidgets();
        }
    
        private void initWidgets()
        {
            baslikEditText = findViewById(R.id.editBaslik);
            aciklamaEditText = findViewById(R.id.editNot);
        }
    
        public void KaydetNot(View view)
        {
            SQLite sqLite = SQLite.instanceOfDatabase(this);
            String baslik = String.valueOf(baslikEditText.getText());
            String aciklama = String.valueOf(aciklamaEditText.getText());
    
            int id = Not.notArrayList.size();
            Not yeniNot = new Not(id,baslik,aciklama);
            Not.notArrayList.add(yeniNot);
            sqLite.DatabaseNotEkle(yeniNot);
            finish();
        }
    }

**This is the NotUyarlama class NoteAdapter  class in his video**

    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.TextView;
    
    import androidx.annotation.NonNull;
    import androidx.annotation.Nullable;
    
    import org.w3c.dom.Text;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class NotUyarlama extends ArrayAdapter<Not>
    {
        public NotUyarlama(Context context, List<Not> nots)
        {
            super(context, 0, nots);
    
        }
    
        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
        {
            Not not = getItem(position);
            if(convertView == null)
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.not_hucreleri,parent,false);
    
            TextView baslik = convertView.findViewById(R.id.txtBaslik);
            TextView aciklama = convertView.findViewById(R.id.txtAciklama);
    
            baslik.setText(not.getBaslik());
            aciklama.setText(not.getAciklama());
    
    
            return convertView;
        }
    }

Note class

import java.util.ArrayList;
import java.util.Date;

public class Not {

    public static ArrayList<Not> notArrayList = new ArrayList<>();

    private int id;
    private String baslik;
    private String aciklama;
    private Date silinmis;

    public Not(int id, String baslik, String aciklama, Date silinmis) {
        this.id = id;
        this.baslik = baslik;
        this.aciklama = aciklama;
        this.silinmis = silinmis;
    }

    public Not(int id, String baslik, String aciklama) {
        this.id = id;
        this.baslik = baslik;
        this.aciklama = aciklama;
        silinmis =null;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getBaslik() {
        return baslik;
    }

    public void setBaslik(String baslik) {
        this.baslik = baslik;
    }

    public String getAciklama() {
        return aciklama;
    }

    public void setAciklama(String aciklama) {
        this.aciklama = aciklama;
    }

    public Date getSilinmis() {
        return silinmis;
    }

    public void setSilinmis(Date silinmis) {
        this.silinmis = silinmis;
    }
}

The SQLite class

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

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


public class SQLite extends SQLiteOpenHelper
{
    private static SQLite sqLite;
    private static final String DATABASE_NAME = "NotDB";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME = "NotList";
    private static final String COUNTER = "Counter";

    private static final String ID_FIELD = "id";
    private static final String BASLIK_FIELD = "baslik";
    private static final String ACIKLAMA_FIELD = "aciklama";
    private static final String SILINMIS_FIELD = "silinmis";

    @SuppressLint("SimpleDateFormat")
    private static final DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");


    public SQLite(Context context)
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public static SQLite instanceOfDatabase(Context context)
    {
        if(sqLite == null)
            sqLite = new SQLite(context);


        return sqLite;
    }




    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase)
    {
        StringBuilder sql;
        sql = new StringBuilder()
                .append("CREATE TABLE")
                .append(TABLE_NAME)
                .append("(")
                .append(COUNTER)
                .append(" INTEGER PRIMARY KEY AUTOINCREMENT, ")
                .append(ID_FIELD)
                .append(" INT ")
                .append(BASLIK_FIELD)
                .append(" TEXT, ")
                .append(ACIKLAMA_FIELD)
                .append(" TEXT, ")
                .append(SILINMIS_FIELD)
                .append("TEXT)");
        sqLiteDatabase.execSQL(sql.toString());

    }


    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion)
    {
//        switch (oldVersion)
//        {
//            case 1:
//               sqLiteDatabase.execSQL("ALTER TABLE "   TABLE_NAME   " ADD COLUMN "   NEW_COLUMN   " TEXT");
//            case 2:
//                sqLiteDatabase.execSQL("ALTER TABLE "   TABLE_NAME   " ADD COLUMN "   NEW_COLUMN   " TEXT");
//        }
    }
    public void DatabaseNotEkle(Not not)
    {
        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();

        ContentValues contentValues = new ContentValues();
        contentValues.put(ID_FIELD,not.getId());
        contentValues.put(BASLIK_FIELD,not.getBaslik());
        contentValues.put(ACIKLAMA_FIELD,not.getAciklama());
        contentValues.put(SILINMIS_FIELD, getStringFromDate(not.getSilinmis()));
        
        sqLiteDatabase.insert(TABLE_NAME,null,contentValues);


    }

        public void populateNotListArray()
        {
            SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();


         try  (Cursor result = sqLiteDatabase.rawQuery("SELECT * FROM "   TABLE_NAME, null))
         {
             if(result.getCount() != 0)
             {
                 while(result.moveToNext())
                 {
                     int id = result.getInt(1);
                     String baslik=result.getString(2);
                     String aciklama=result.getString(3);
                     String silinmisString=result.getString(4);
                     Date silinmis = getDateFromString(silinmisString);
                     Not not = new Not(id,baslik,aciklama,silinmis);
                     Not.notArrayList.add(not);
                 }
             }
         }
        }

    private void NotGuncelle (Not not)
    {
        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(ID_FIELD,not.getId());
        contentValues.put(BASLIK_FIELD,not.getBaslik());
        contentValues.put(ACIKLAMA_FIELD,not.getAciklama());
        contentValues.put(SILINMIS_FIELD,getStringFromDate(not.getSilinmis()));

        sqLiteDatabase.update(TABLE_NAME, contentValues, ID_FIELD   " =? ",new String[]{String.valueOf(not.getId())});


    }

    private String getStringFromDate(Date date)
    {
        if(date == null)
            return null;
        return dateFormat.format(date);
    }
    private Date getDateFromString(String string)
    {
        try{
            return dateFormat.parse(string);
        }
        catch (ParseException | NullPointerException e)
        {
            return null;
        }

    }
}

https://www.youtube.com/watch?v=4k1ZMpO9Zn0&t=732s I'm trying to do this app by doing everything he does in a video i made it to 14:12 then he tests the save data to database i guess he runs the device but when I try to run the device I had problems and my app keeps stopping I cant run it I'm using SQLite like he does and its on android studio.

CodePudding user response:

There is one issue with your code.

You are missing a space in SQL when creating a table. So let's assume you rename the table to NotEntity and then the code would look something like this:

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

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


public class SQLite extends SQLiteOpenHelper
{
    private static SQLite sqLite;
    private static final String DATABASE_NAME = "NotDB";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME = "NotList";
    private static final String COUNTER = "Counter";

    private static final String ID_FIELD = "id";
    private static final String BASLIK_FIELD = "baslik";
    private static final String ACIKLAMA_FIELD = "aciklama";
    private static final String SILINMIS_FIELD = "silinmis";

    @SuppressLint("SimpleDateFormat")
    private static final DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");


    public SQLite(Context context)
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public static SQLite instanceOfDatabase(Context context)
    {
        if(sqLite == null)
            sqLite = new SQLite(context);


        return sqLite;
    }




    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase)
    {
        StringBuilder sql;
        sql = new StringBuilder()
                .append("CREATE TABLE ")
                .append(TABLE_NAME)
                .append(" (")
                .append(COUNTER)
                .append(" INTEGER PRIMARY KEY AUTOINCREMENT, ")
                .append(ID_FIELD)
                .append(" INT ")
                .append(BASLIK_FIELD)
                .append(" TEXT, ")
                .append(ACIKLAMA_FIELD)
                .append(" TEXT, ")
                .append(SILINMIS_FIELD)
                .append("TEXT)");
        sqLiteDatabase.execSQL(sql.toString());

    }


    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion)
    {
//        switch (oldVersion)
//        {
//            case 1:
//               sqLiteDatabase.execSQL("ALTER TABLE "   TABLE_NAME   " ADD COLUMN "   NEW_COLUMN   " TEXT");
//            case 2:
//                sqLiteDatabase.execSQL("ALTER TABLE "   TABLE_NAME   " ADD COLUMN "   NEW_COLUMN   " TEXT");
//        }
    }
    public void DatabaseNotEkle(Not not)
    {
        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();

        ContentValues contentValues = new ContentValues();
        contentValues.put(ID_FIELD,not.getId());
        contentValues.put(BASLIK_FIELD,not.getBaslik());
        contentValues.put(ACIKLAMA_FIELD,not.getAciklama());
        contentValues.put(SILINMIS_FIELD, getStringFromDate(not.getSilinmis()));
        
        sqLiteDatabase.insert(TABLE_NAME,null,contentValues);


    }

        public void populateNotListArray()
        {
            SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();


         try  (Cursor result = sqLiteDatabase.rawQuery("SELECT * FROM "   TABLE_NAME, null))
         {
             if(result.getCount() != 0)
             {
                 while(result.moveToNext())
                 {
                     int id = result.getInt(1);
                     String baslik=result.getString(2);
                     String aciklama=result.getString(3);
                     String silinmisString=result.getString(4);
                     Date silinmis = getDateFromString(silinmisString);
                     Not not = new Not(id,baslik,aciklama,silinmis);
                     Not.notArrayList.add(not);
                 }
             }
         }
        }

    private void NotGuncelle (Not not)
    {
        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(ID_FIELD,not.getId());
        contentValues.put(BASLIK_FIELD,not.getBaslik());
        contentValues.put(ACIKLAMA_FIELD,not.getAciklama());
        contentValues.put(SILINMIS_FIELD,getStringFromDate(not.getSilinmis()));

        sqLiteDatabase.update(TABLE_NAME, contentValues, ID_FIELD   " =? ",new String[]{String.valueOf(not.getId())});


    }

    private String getStringFromDate(Date date)
    {
        if(date == null)
            return null;
        return dateFormat.format(date);
    }
    private Date getDateFromString(String string)
    {
        try{
            return dateFormat.parse(string);
        }
        catch (ParseException | NullPointerException e)
        {
            return null;
        }

    }
}
  • Related