0

So I have 3 actionBar tabs running 3 different fragment classes. In Tab1 of the actionBar I have a simple form that a user fills in and the data is then saved in an SQLite database, this form and the tasks carried out on it work fine as a project by itself but when I tried to integrate it as a fragment (FragmentTab1) then the app crashes when I click the save button. Any ideas as to why it may be crashing? I think I may have to implement an onClickListener maybe, not sure though! I am aware that the way I am using tabs and fragments is a bit outdated but I'd still like to get it working this way.

Error Log

        02-14 16:42:14.560  10914-10914/com.androidbegin.absfragtabhost/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.androidbegin.absfragtabhost, PID: 10914
java.lang.IllegalStateException: Could not find a method addButtonClicked(View) in the activity class com.androidbegin.absfragtabhost.MainActivity for onClick handler on view class android.widget.Button with id 'addButtonClicked'
        at android.view.View$1.onClick(View.java:3828)
        at android.view.View.performClick(View.java:4456)
        at android.view.View$PerformClick.run(View.java:18465)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5086)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NoSuchMethodException: addButtonClicked [class android.view.View]
        at java.lang.Class.getConstructorOrMethod(Class.java:472)
        at java.lang.Class.getMethod(Class.java:857)
        at android.view.View$1.onClick(View.java:3821)
        at android.view.View.performClick(View.java:4456) 
        at android.view.View$PerformClick.run(View.java:18465)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5086) 
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
        at dalvik.system.NativeStart.main(Native Method)

FragmentTab1.java

package com.androidbegin.absfragtabhost;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;



public class FragmentTab1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment1, container, false);
    return rootView;
}

public class Activity extends ActionBarActivity {

    TextView firstName;
    EditText editTextName;

    TextView textView5;
    EditText editTextSurname;

    TextView textView4;
    EditText editTextMobile;

    TextView textView2;
    EditText editTextEmail;

    TextView textView3;
    EditText editTextAddress1;

    TextView textView6;
    EditText editTextAddress2;

    MyDBHandler dbHandler;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        firstName = (TextView) findViewById(R.id.firstName);
        editTextName = (EditText) findViewById(R.id.editTextName);
        textView5 = (TextView) findViewById(R.id.textView5);
        editTextSurname = (EditText) findViewById(R.id.editTextSurname);
        textView4 = (TextView) findViewById(R.id.textView4);
        editTextMobile = (EditText) findViewById(R.id.editTextMobile);
        textView2 = (TextView) findViewById(R.id.textView2);
        editTextEmail = (EditText) findViewById(R.id.editTextEmail);
        textView3 = (TextView) findViewById(R.id.textView3);
        editTextAddress1 = (EditText) findViewById(R.id.editTextAddress1);
        textView6 = (TextView) findViewById(R.id.textView6);
        editTextAddress2 = (EditText) findViewById(R.id.editTextAddress2);

        dbHandler = new MyDBHandler(this, null, null, 1);
        //printDatabase();
    }


    //Add details to the database
    public void addButtonClicked(View view) {
        Details details = new Details("");
        details.setFirstname(editTextName.getText().toString());
        details.setSurname(editTextSurname.getText().toString());
        details.setPhone(editTextMobile.getText().toString());
        details.setEmail(editTextEmail.getText().toString());
        details.setAddress1(editTextAddress1.getText().toString());
        details.setAddress2(editTextAddress2.getText().toString());
        dbHandler.addDetails(details);
        //printDatabase();
    }
}
}

Fragment1.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="600dp"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/AppTheme"
    android:touchscreenBlocksFocus="false">


    <!-- First name -->

    <TextView
        android:id="@+id/firstName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/editTextName"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:text="@string/firstname"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editTextName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="5dp"
        android:layout_marginStart="82dp"
        android:layout_marginLeft="90dp"
        android:ems="10"
        android:paddingTop="25dp"
        android:inputType="text" >
    </EditText>



    <!-- Surname -->

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/address2"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_marginTop="33dp"
        android:layout_below="@+id/editTextAddress1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:id="@+id/editTextAddress2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="text"
        android:layout_alignTop="@+id/textView6"
        android:layout_alignLeft="@+id/editTextAddress1"
        android:layout_alignStart="@+id/editTextAddress1" />




    <!-- Mobile Number -->

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/editTextSurname"
        android:layout_alignLeft="@+id/firstName"
        android:layout_alignStart="@+id/firstName"
        android:text="@string/surname"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editTextSurname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editTextMobile"
        android:layout_alignStart="@+id/editTextMobile"
        android:layout_below="@+id/editTextName"
        android:layout_marginTop="22dp"
        android:ems="10"
        android:inputType="text" />





    <!-- Email Address -->


    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/editTextEmail"
        android:layout_alignLeft="@+id/firstName"
        android:layout_alignStart="@+id/firstName"
        android:text="@string/email"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:inputType="textEmailAddress" />

    <EditText
        android:id="@+id/editTextEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/editTextMobile"
        android:layout_alignLeft="@+id/editTextMobile"
        android:layout_below="@+id/editTextMobile"
        android:layout_marginTop="22dp"
        android:ems="10"
        android:inputType="textEmailAddress" />



    <!-- Address 1 -->

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editTextAddress1"
        android:layout_alignBottom="@+id/editTextAddress1"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/editTextEmail"
        android:text="@string/address1"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editTextAddress1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/editTextName"
        android:layout_alignEnd="@+id/editTextName"
        android:layout_below="@+id/editTextEmail"
        android:layout_marginTop="30dp"
        android:ems="10"
        android:inputType="text" />




    <!-- Address 2 -->

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editTextEmail"
        android:layout_alignLeft="@+id/textView5"
        android:layout_alignStart="@+id/textView5"
        android:text="@string/phone"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <EditText
        android:id="@+id/editTextMobile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editTextName"
        android:layout_alignStart="@+id/editTextName"
        android:layout_below="@+id/editTextSurname"
        android:layout_marginTop="22dp"
        android:ems="10"
        android:inputType="phone">
    </EditText>




    <Button
        android:id="@+id/addButtonClicked"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="addButtonClicked"
        android:text="@string/save"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />


    <!-- <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/change"
        android:layout_below="@+id/editTextAddress2"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="30dp"
        android:onClick="onBtnClicked"/> -->


</RelativeLayout>
</ScrollView>

MainActivity

package com.androidbegin.absfragtabhost;

import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {
// Declare Tab Variable
ActionBar.Tab Tab1, Tab2, Tab3;
Fragment fragmentTab1 = new FragmentTab1();
Fragment fragmentTab2 = new FragmentTab2();
Fragment fragmentTab3 = new FragmentTab3();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ActionBar actionBar = getActionBar();

    // Hide Actionbar Icon
    actionBar.setDisplayShowHomeEnabled(false);

    // Hide Actionbar Title
    actionBar.setDisplayShowTitleEnabled(false);

    // Create Actionbar Tabs
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Set Tab Icon and Titles
    Tab1 = actionBar.newTab().setIcon(R.drawable.tab1);
    Tab2 = actionBar.newTab().setText("Tab2");
    Tab3 = actionBar.newTab().setText("Tab3");

    // Set Tab Listeners
    Tab1.setTabListener(new TabListener(fragmentTab1));
    Tab2.setTabListener(new TabListener(fragmentTab2));
    Tab3.setTabListener(new TabListener(fragmentTab3));

    // Add tabs to actionbar
    actionBar.addTab(Tab1);
    actionBar.addTab(Tab2);
    actionBar.addTab(Tab3);
}
}
user3375184
  • 147
  • 2
  • 3
  • 8
  • The problem is that Android is searching for addButtonClicked implementation in your Activity, not in your Fragment. See similar issue here http://stackoverflow.com/questions/21192386/android-fragment-onclick-button-method – Lucas Feb 14 '15 at 16:56
  • Where would I create the onClickListener? Slightly confused over this – user3375184 Feb 14 '15 at 20:40

1 Answers1

0

The trace says: "Could not find a method addButtonClicked(View) in the activity class com.androidbegin.absfragtabhost.MainActivity for onClick handler on view class android.widget.Button with id 'addButtonClicked'"

The method needs to be in the activity class not in the fragment, either put it in there or use a onClickListener instead (which is what I would do)

Like this:

Button addButtonClicked = (Button)rootView.findViewById(R.id.addButtonClicked);
addButtonClicked.setOnClickListener(new OnClickListener(){
            public void onClick(View v) 
            {
                addButtonClicked(v); //(you dont need this parameter though)
            }
});
CyborgFish
  • 356
  • 6
  • 13
  • Where would I put the onClickListener then? and any idea how to implement it? Thanks – user3375184 Feb 14 '15 at 20:31
  • In the oncreateview method in the fragment class, editing response to show how – CyborgFish Feb 14 '15 at 20:55
  • Okay, so I'm getting the following error: "cannot resolve symbol rootview". What do I do with the rest of the code, do I leave it in FragmentTab1 or? How do they connect? Sorry for all the questions, I'm new to fragments. thanks – user3375184 Feb 14 '15 at 21:48
  • np mate I was like that not long ago. the code I said has to be implemented inside the onCreateView method in your FragmentTab1 class, _after creating the rootView variable_ so you wont get that error. You can copy your `addButtonClicked(View view)` (which by the way doesnt need the View parameter anymore) method to the fragment class and make a call to it on the onClick method of the listener. Response edited – CyborgFish Feb 14 '15 at 21:55
  • So I've put the code you gave me in the onCreateView of the FragmentTab1 class and it's giving me an error saying "Method call expected"? – user3375184 Feb 14 '15 at 22:09
  • Delete your _Activity_ class, move the code to the _FragmentTab1_ class (_onCreate_ code goes to _onCreateView_, and `rootView.` before every instance of `findViewById(...`) then do the changes I said – CyborgFish Feb 14 '15 at 22:19
  • I'm sorry man, I'm really not getting you, really appreciate you trying though, thanks! – user3375184 Feb 14 '15 at 22:30
  • well i can help you further if that means you will make it, im a beginner too but i think your FragmentTab1.java file is wrong, you need to delete the _Activity_ (`public class Activity extends ActionBarActivity`) class and move all the code in it to the _FragmentTab1_ class so its the fragment that does those things, which is what you want to do. – CyborgFish Feb 14 '15 at 22:50
  • I appreciate this man, so I deleted that activity and put the code in FragmentTab1 but now in my 'Protected void onCreate' the setContentView and findViewbyId are saying "cannot resolve method" for all of them. – user3375184 Feb 14 '15 at 23:16
  • Maybe it's because I can't have two onCreates? – user3375184 Feb 14 '15 at 23:38
  • There is no _onCreate_ in Fragments, the code that was in the onCreate method of the Activity goes in _onCreateView_ in the Fragment. As for the "cannot resolve method" thats normal too, you got to call that method in the view, ie use it like this: `rootView.findViewById(...)` – CyborgFish Feb 15 '15 at 01:38
  • Ok so I've moved the code to the _onCreateView_ but now it's saying that 'dbHandler = new MyDBHandler(this, null, null, 1);' cannot be applied? – user3375184 Feb 15 '15 at 12:05
  • Nice. For the dbhandler thing change it for: dbHandler = new MyDBHandler(_getActivity()_, null, null, 1); – CyborgFish Feb 15 '15 at 13:10
  • Yup that worked! Is the next step to add the onClickListener? and if so where do I add it? – user3375184 Feb 15 '15 at 14:00
  • yes, with the code i said, you can add it on the onCreateView method of the fragment, after you create the rootView variable (in the code you gave, you created it by doing `View rootView = inflater.inflate(R.layout.fragment1, container, false);` so after that). In the listener make a call to your _addButtonClicked_ method I guess – CyborgFish Feb 15 '15 at 14:04
  • np mate, im glad to hear that. thanks for accepting my answer by the way – CyborgFish Feb 15 '15 at 15:25