mardi 29 mars 2016

Android app SQLite addButtonClicked IllegalStateException

Hi im trying to create a simple SQLite database in a fragment in which an individual can store memos. there is no error obvious errors on the app but once a user tries to add anything and clicks the add button I receive the following error. If anyone knows where I am going wrong it would be much appreciated

03-29 11:45:08.781 6065-6065/com.test.test.app                 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 Process: com.test.test.app, PID: 6065
                                                                                 java.lang.IllegalStateException: Could not find method addButtonClicked(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'addButton'

MemoFragment.java

package com.teat.test.app;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


/**
  * A simple {@link Fragment} subclass.
  */
public class MemoFragment extends Fragment {

EditText Input;
TextView LyricText;
MyDBHandler dbHandler;
Button addButtonClicked;
Button deleteButtonClicked;

public MemoFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_memo, container, false);

    Input = (EditText) v.findViewById(R.id.Input);
    LyricText = (TextView) v.findViewById (R.id.LyricText);
    dbHandler = new MyDBHandler(getActivity() ,null, null, 1);
    printDatabase ();
    return v;
}
//add lyric to database
    public void addButtonClicked(View view){
    Lyrics lyrics = new Lyrics(Input.getText().toString());
        dbHandler.addLyric(lyrics);
    printDatabase();
}

//delete items
public void deleteButtonClicked(View view){
    String inputtext = Input.getText().toString();
    dbHandler.deleteLyrics(inputtext);
    printDatabase();


}

public void printDatabase(){
    String dbString = dbHandler.databaseToString();
    LyricText.setText(dbString);
    Input.setText("");
}

}

fragment_memo.xml

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/Input"
    android:layout_marginTop="71dp"
    android:width="300dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Add"
    android:id="@+id/addButton"
    android:layout_below="@+id/Input"
    android:layout_alignLeft="@+id/Input"
    android:layout_alignStart="@+id/Input"
    android:layout_marginTop="44dp"
    android:onClick="addButtonClicked" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Delete"
    android:id="@+id/deleteButton"
    android:layout_alignBottom="@+id/addButton"
    android:layout_alignRight="@+id/Input"
    android:layout_alignEnd="@+id/Input"
    android:longClickable="false"
    android:onClick="deleteButtonClicked" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/LyricText"
    android:layout_below="@+id/deleteButton"
    android:layout_toRightOf="@+id/addButton"
    android:layout_toEndOf="@+id/addButton"
    android:layout_marginTop="102dp" />

Aucun commentaire:

Enregistrer un commentaire