I have created a small application to do CRUD operations on SQLite but when i'm running it, it closes automatically in emulator and shows FATAL Exception in main.
And this thing happened with every application which i was tried to create for my practice. I'm new in android development plz help.
Thanks!
MainActivity.java
import android.content.DialogInterface;
import android.database.sqlite.SQLiteException;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText firstname, lastname;
TextView textView;
Db_Controller controller;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstname = (EditText)findViewById(R.id.firstname);
lastname = (EditText)findViewById(R.id.lastname);
textView = (TextView)findViewById(R.id.textView);
controller = new Db_Controller(this,"",null,1);
}
public void btn_click(View view) {
switch (view.getId()){
case R.id.btn_add:
try {
controller.insert_info(firstname.getText().toString(),lastname.getText().toString());
}catch (SQLiteException e) {
Toast.makeText(MainActivity.this, "ALREADY EXISTS", Toast.LENGTH_SHORT).show();
}
break;
case R.id.btn_delete:
controller.delete_info(firstname.getText().toString());
break;
case R.id.btn_update:
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("ENTER NEW FIRSTNAME");
final EditText new_firstname = new EditText(this);
dialog.setView(new_firstname);
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
controller.update_info(firstname.getText().toString(),new_firstname.getText().toString());
}
});
dialog.show();
break;
case R.id.btn_list:
controller.list_all(textView);
break;
}
}
}
Db_Controller.java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.TextView;
public class Db_Controller extends SQLiteOpenHelper {
public Db_Controller(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, "TEST.db", null , 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("CREATE TABLE INFO (ID INTEGER PRIMARY KEY AUTOINCREMENT, FIRSTNAME TEXT UNIQUE, LASTNAME TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS INFO");
onCreate(sqLiteDatabase);
}
public void insert_info(String firstname, String lastname) {
ContentValues contentValues = new ContentValues();
contentValues.put("FIRSTNAME", firstname);
contentValues.put("LASTNAME", lastname);
this.getWritableDatabase().insertOrThrow("INFO","", contentValues);
}
public void delete_info(String firstname) {
this.getWritableDatabase().delete("INFO","FIRSTNAME= '" +firstname+ "'", null);
}
public void update_info(String old_firstname, String new_firstname) {
this.getWritableDatabase().execSQL("UPDATE INFO SET FIRSTNAME='" +new_firstname+ "' WHERE FIRSTNAME='" +old_firstname+ "'");
}
public void list_all(TextView textView) {
Cursor cursor= this.getReadableDatabase().rawQuery("SELECT * FROM INFO", null);
textView.setText("");
while (cursor.moveToNext()) {
textView.append(cursor.getString(1)+" "+cursor.getString(2)+"\n");
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.balashanti.rushikeshkotule.database.MainActivity">
<EditText
android:id="@+id/firstname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="First_Name"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/lastname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="17dp"
android:ems="10"
android:hint="Last name"
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="@+id/firstname"
app:layout_constraintTop_toBottomOf="@+id/firstname" />
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="@color/colorPrimaryDark"
android:onClick="btn_click"
android:shadowColor="@color/colorAccent"
android:text="Add"
app:layout_constraintBottom_toBottomOf="@+id/btn_delete"
app:layout_constraintStart_toStartOf="@+id/lastname"
app:layout_constraintTop_toTopOf="@+id/btn_delete" />
<Button
android:id="@+id/btn_update"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_marginTop="75dp"
android:background="@color/colorPrimaryDark"
android:onClick="btn_click"
android:text="Update"
app:layout_constraintStart_toStartOf="@+id/btn_add"
app:layout_constraintTop_toBottomOf="@+id/lastname" />
<Button
android:id="@+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="38dp"
android:layout_marginBottom="21dp"
android:layout_marginTop="16dp"
android:background="@color/colorPrimaryDark"
android:onClick="btn_click"
android:text="Delete"
app:layout_constraintBottom_toTopOf="@+id/btn_list"
app:layout_constraintEnd_toEndOf="@+id/lastname"
app:layout_constraintTop_toBottomOf="@+id/lastname" />
<Button
android:id="@+id/btn_list"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:background="@color/colorPrimaryDark"
android:onClick="btn_click"
android:text="list"
app:layout_constraintBottom_toBottomOf="@+id/btn_update"
app:layout_constraintStart_toStartOf="@+id/btn_delete"
app:layout_constraintTop_toTopOf="@+id/btn_update" />
<TextView
android:id="@+id/textView"
android:layout_width="238dp"
android:layout_height="176dp"
android:layout_marginBottom="66dp"
android:layout_marginTop="69dp"
android:text="textView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/btn_list"
app:layout_constraintVertical_bias="1.0" />
</android.support.constraint.ConstraintLayout>
LogCat
Split APKs installed
$ adb shell am start -n "com.balashanti.rushikeshkotule.database/com.balashanti.rushikeshkotule.database.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Waiting for process to come online
Connected to process 6551 on device emulator-5554
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
I/InstantRun: starting instant run server: is main process
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.balashanti.rushikeshkotule.database, PID: 6551
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.balashanti.rushikeshkotule.database/com.balashanti.rushikeshkotule.database.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.balashanti.rushikeshkotule.database.MainActivity" on path: DexPathList[[zip file "/data/app/com.balashanti.rushikeshkotule.database-vwEPGAOgImQOJqYT9taLMg==/base.apk"],nativeLibraryDirectories=[/data/app/com.balashanti.rushikeshkotule.database-vwEPGAOgImQOJqYT9taLMg==/lib/x86, /system/lib, /vendor/lib]]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.balashanti.rushikeshkotule.database.MainActivity" on path: DexPathList[[zip file "/data/app/com.balashanti.rushikeshkotule.database-vwEPGAOgImQOJqYT9taLMg==/base.apk"],nativeLibraryDirectories=[/data/app/com.balashanti.rushikeshkotule.database-vwEPGAOgImQOJqYT9taLMg==/lib/x86, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.app.Instrumentation.newActivity(Instrumentation.java:1174)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Application terminated.
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.balashanti.rushikeshkotule.database">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.balashanti.rushikeshkotule.database.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>