This is for a school project where I press save and it gets saved and when I press the load Button it should load on the TextView (label)
The problem is that when I generate the apk and install it onto my phone, as soon as I click any Button, the program crashes.
It doesn't matter if I entered anything into the text field.
I have no idea about what's the problem, so please help!
I have to run the apk on my form, so running it on a virtual machine to test first is out of the question.
I tested it onto 2 devices and both give the same result
My code:
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;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class MainActivity extends AppCompatActivity {
private String file = "mydatabase";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Save(View view) {
EditText save = (EditText) findViewById(R.id.txt_save);
String data = save.getText().toString();
try {
FileOutputStream fOut = openFileOutput(file, MODE_PRIVATE);
fOut.write(data.getBytes());
fOut.close();
Toast.makeText(getBaseContext(), "File saved sucessfully", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
public void load (View view) {
TextView load = (TextView) findViewById(R.id.lbl_Display);
try {
FileInputStream fin = openFileInput(file);
int c;
String temp = "";
while ((c = fin.read()) != -1) {
temp = temp + Character.toString((char) c);
}
load.setText(temp);
Toast.makeText(getBaseContext(), "File Read Sucessfully", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
}
}}
Here is the UI I'm using where txt_save is the text field and lbl_Display is the TextView
Error:
W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb17c34f0)
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not find method Save (MainActivity)(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Application terminated.
I'm using GingerBread (API Level 10)
My manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.mydatabase">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>