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>