-5

trying to call method within a class extends Fragment, from a textview within layout file. In an activity class I would use this:

setContentView(R.layout.activity_main);

What would I do to achieve similarity using a class extends Fragment?

Error from logcat:

java.lang.IllegalStateException: Could not find method mood(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatTextView with id 'moodtw'

Class :

public class Tab1 extends Fragment {
private View rootView;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.tab1, container, false);
   return (rootView);


}
public void mood() {
    String tempo;
    int a;
    TextView moodouttw = (TextView) rootView.findViewById(R.id.moodouttw);
    tempo = moodouttw.getText().toString();
    a = Integer.parseInt(tempo);
    if (a < 5) {
        a = a + 1;
    } else a = 0;
    tempo = Integer.toString(a);
    moodouttw.setText(tempo);
}
}

COULDN'T GET ABOVE TO WORK, so I tried using onclicklistner found in a link from an prev. answer. This also with same error posted above. Looks like app is looking for a method with parameter which, to my knowledge, cant be set in a xml layout file.(?). I might try another approach.Below is copy of last approach and snippet from layout xml file. Thanks to all for help given.

    package com.example.android.xxxxx;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class Tab1 extends Fragment implements View.OnClickListener {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.tab1, container, false);
        TextView t = (TextView) v.findViewById(R.id.anxietyouttw);
        t.setOnClickListener(this);
        return v;
    }

    @Override
    public void onClick(View v) {

       switch (v.getId()) {
            case R.id.anxietyouttw:
                // do something
                String tempo;
                int a;
                TextView anxietyouttw = (TextView) v.findViewById(R.id.anxietyouttw);
                tempo = anxietyouttw.getText().toString();
                a = Integer.parseInt(tempo);
                if (a < 5) {
                    a = a + 1;
                } else a = 0;
                tempo = Integer.toString(a);
                anxietyouttw.setText(tempo);

                break;
        }
    }
}

XML LAYOUT FILE

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">


    <TextView
        android:id="@+id/headertw1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="18dp"
        android:layout_toEndOf="@+id/anxietyleveltw"
        android:layout_toRightOf="@+id/anxietyleveltw"
        android:text="@string/input_of_general_data_1_of_2"
        android:textAppearance="@style/TextAppearance.AppCompat"
        android:textColor="@color/colorPrimaryDark"
        android:textStyle="bold" />

<TextView
android:id="@+id/anxietyleveltw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/moodtw"
android:layout_alignStart="@+id/moodtw"
android:layout_below="@+id/moodtw"
android:layout_marginTop="15dp"
android:onClick="anxiety"
android:text="@string/anixiety"
android:textAppearance="@style/TextAppearance.AppCompat" />

<TextView
android:id="@+id/anxietyouttw"
android:layout_width="24dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/anxietyleveltw"
android:layout_alignBottom="@+id/anxietyleveltw"
android:layout_alignLeft="@+id/sleepouttw"
android:layout_alignStart="@+id/sleepouttw"
android:text="@string/_0"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat"
android:textIsSelectable="false"
android:textSize="14sp" />

</RelativeLayout>
jane
  • 1
  • 4
  • Asked already multiple times... Also what do you not understand in *Could not find method mood(View) in a parent or ancestor Context for android:onClick attribute* Fragment is not Context and mood() is not mood(View) – Selvin Jan 27 '18 at 13:32
  • hi, please. My previous issue was findviewbyid which i manage to solve after hours of reading. Now I ask for help with a new issue. I know my previous posts are shitty and would delete them if I could. Sorry if my last post trigger your anger/frustation. – jane Jan 27 '18 at 13:42
  • One error I could spot is your click handling method(mood) must take a View as parameter(this view represents the button clicked). Try public void mood(View v){ //.... } this applies when you are using android:onClick from xml file. – S Praveen Kumar Jan 27 '18 at 13:45
  • Possible duplicate of [findViewById in Fragment](https://stackoverflow.com/questions/6495898/findviewbyid-in-fragment) – Prakhar Agarwal Jan 27 '18 at 16:34

2 Answers2

0

please try this

setOnClickListener for the textview for which you are setting OnClick in xml layout.

For eg

private View rootView;
private TextView textView;

In onCreateView

rootView = inflater.inflate(R.layout.tab1, container, false);
textView = rootView.findViewById(R.id.<your textview id>);
textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mood();
            }
        });
shizhen
  • 12,251
  • 9
  • 52
  • 88
tanni tanna
  • 544
  • 4
  • 12
  • Tried it. Still cant find method mood(). When mood() is moved from MainActivity class to a class with extends Fragment, mood() is reported not found. My thought is howto make mood() "viewable" ? I have no answer on that. Thanks for Your suggestion. – jane Jan 27 '18 at 16:51
  • Making mood() viewable directly in Fragment class, I doubt is possible. What I have experienced is Android looks for the method defined via XML onClick in the activity class rather than Fragment class Please go through this, I hope you may find it useful https://stackoverflow.com/questions/6091194/how-to-handle-button-clicks-using-the-xml-onclick-within-fragments another option is what I have suggested in the answer section, implement listerner in the fragment. If you have tried and mood is not reported , then copy the method mood() into the fragment from activity. – tanni tanna Jan 27 '18 at 17:15
  • if you could share xml files as well please? – tanni tanna Jan 27 '18 at 17:22
  • Added in original posting now. – jane Jan 28 '18 at 00:33
  • thanks Jane, I copied your xml file and I see few issues android:layout_alignLeft="@+id/moodtw" android:layout_alignLeft="@+id/sleepouttw" the ids moodtw and sleepouttw are not defined in the xml file. could you please verify that. Also if you could share main activity code and xml please. – tanni tanna Jan 28 '18 at 13:09
0

Set View as arg in method

 public class Tab1 extends Fragment {
    private View rootView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.tab1, container, false);
       return (rootView);


    }
    public void mood(View view) {
        String tempo;
        int a;
        TextView moodouttw = (TextView) rootView.findViewById(R.id.moodouttw);
        tempo = moodouttw.getText().toString();
        a = Integer.parseInt(tempo);
        if (a < 5) {
            a = a + 1;
        } else a = 0;
        tempo = Integer.toString(a);
        moodouttw.setText(tempo);
    }
    }
MJM
  • 5,119
  • 5
  • 27
  • 53