1

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 enter image description here

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>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

0

Could not find method Save (MainActivity)(View) in a parent or ancestor Context for android:onClick

However

You've shown this

public void Save(View view) {

So, I'd suggest the solution is not to use XML and set the click listener from Java

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 1
    Both xml or onclicklistener in java are perfectly valid ways to use. Please look here: http://stackoverflow.com/questions/4153517/how-exactly-does-the-androidonclick-xml-attribute-differ-from-setonclicklistene – Totumus Maximus Mar 01 '17 at 13:56
  • @TotumusMaximus I know that, How do you explain the error, then? – OneCricketeer Mar 01 '17 at 17:08
0

Ok, I found your problem. It is the extension of AppCompatActivity in combination with API level 10. You cannot use this before api level 15 (or maybe 11). You need to extend from Activity directly. Or make the minimum API level a bit higher.

For more information read the source that I used:

Activity, AppCompatActivity, FragmentActivity, and ActionBarActivity: When to Use Which?

Community
  • 1
  • 1
Totumus Maximus
  • 7,543
  • 6
  • 45
  • 69
  • `AppCompatActivity` is a support class of API 7, so why can you not use it before API 15? – OneCricketeer Mar 01 '17 at 17:09
  • @cricket_007: AppCompatActivity extends from FragmentActivity which was implemented from APILevel 11. I think you misinterpret the Android documentation and that's probably because it is not as clearly written there. – Totumus Maximus Mar 02 '17 at 09:00