• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Not Displaying Detail from Fragment

ac4android

Well-Known Member
Nov 3, 2015
198
29
Hi.
Problem: AVD not displaying the detail from the fragment!
Addendum: Android Monitor and Event log did NOT log any errors!

The display on AVD should be like this:
Core Agony
100 Pull-ups
100 Push-ups
100 Sit-ups
100 Squats

I am following some codes, the objective is to display an array of gym workouts and detail of each of the workouts. For testing, I hard-coded Java to return the detail for array index 1
Both the Java and the XML seem alright to me, however, the AVD displays only the workout e.g. "Core Agony", but does not display the detail.

I am using Nexus 4 API21 for this gradle.

I've pulled the XML from content_main.XML into activity_main.XML because it is too much trouble for testing purposes.

Here are the java constructors and getters from Workout.java:
Code:
public class Workout {
    private String name;
    private String description;

    public static final Workout[] workouts = {
            new Workout("The Limb Loosener", "5 Handstand push-ups\n10 1-legged squats\n15 Pull-ups"),
            new Workout("Core Agony", "100 Pull-ups\n100 Push-ups\n100 Sit-ups\n100 Squats"),
    };

    // CONSTRUCTOR: each workout has a name and description
    private Workout(String name, String description){
        this.name = name;
        this.description = description;
    }

    // GETTERS for the private variables
    public String getName(){
        return name;
    }
    public String getDescription() {
        return description;
    }

    public String toString(){
        return name;
    }
}
MainActivity.java and its XML file follow, this is where I use a setter to hardcode the array index position to 1:
Code:
import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {

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

        WorkoutDetailFragment frag = (WorkoutDetailFragment) getFragmentManager().findFragmentById(R.id.detail_frag);  // my reference to the fragment
        frag.setWorkout(1);  // hardcode the index position to 1

    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:eek:rientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
class="com.hfad.workout.WorkoutDetailFragment"
android:id="@+id/detail_frag"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_workout_detail" />
</LinearLayout>

The fragment WorkoutDetailFragment.java and its XML file follow:
Code:
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
* A simple {@link Fragment} subclass.
*/
public class WorkoutDetailFragment extends Fragment {
    private long workoutId;

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

    @Override
    public void onStart() {
        super.onStart();

        View view = getView();
        if (view != null) {
            Workout workout = Workout.workouts[(int) workoutId];

            // Get the textTitle from fragment_workout_detail.XML
            TextView title = (TextView) view.findViewById(R.id.textTitle);
            title.setText(workout.getName());
            // Get the textDescription from the XML
            TextView description = (TextView) view.findViewById(R.id.textDescription);
            description.setText(workout.getDescription());

        }
    }
    public void setWorkout(long id){
        this.workoutId = id;
    }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:eek:rientation="vertical"
tools:context="com.hfad.workout.WorkoutDetailFragment">

<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/textTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@String/textTitle" />

<TextView
android:id="@+id/textDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@String/textDescription"/>



</LinearLayout>
 
Last edited:
Try this:

Code:
<TextView
  android:id="@+id/textDescription"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
   android:maxLines="4"
  android:text="@String/textDescription"/>


android:maxLines="4"

Nope, that didn't work and I am still not getting any error messages from Android Monitor.
Besides which, I cannot limit the maxLines, I do not know how many there will be any whether they will gain or lose lines with time
 
Upvote 0
That's puzzling, your code seems ok. Couple of things to check:-

1. Set a breakpoint WorkoutDetailFragment.onStart() and verify the setText() parameter is not null or blank string
2. Try a shorter string value for the workout description, is the '\n' screwing it up?
 
Upvote 0
  • Dropped the \n
  • Reloaded the Google API, then recreated the AVD
  • Put in some test code for logcat and we can see the textDescription detail.

But Android Studio just won't display the textDescription detail.

Therefore cannot be the .java codes, must be the XML although I cannot see anything wrong in the XML either.

Code:
public void onStart() {
        super.onStart();
        /* The getView() method gets the fragment's root view, we can then use this to
         * ... to get references to the workout title and description from text views in the XML
         */
        View view = getView();
        if (view != null) {
            Workout workout = Workout.workouts[(int) workoutId];

            // Get the textTitle from fragment_workout_detail.XML
            TextView title = (TextView) view.findViewById(R.id.textTitle);
            title.setText(workout.getName());
            // Get the textDescription from the XML
            TextView description = (TextView) view.findViewById(R.id.textDescription);
            description.setText(workout.getDescription());
            // debug starts here
            String str_description = workout.getDescription().toString();
            if (description != null){Log.d(TAG, "textDescription is not NULL : "+ str_description );}
            else { Log.d(TAG, "textDescription is NULL");      }
        }
debug.JPG
 
Upvote 0
The breakpoints are in WorkoutDetailFragment.java, yet the Debugger keeps making references to ActivityThread.java which, I assume, is created by AS.

Stepping through the "frames", I notice there is a recurring "cause" : cause={ErrnoException@3564}"android.system.ErrnoException:stat failed:ENOENT(No such file or directory

I'll need some guidance to walk through the Debugger.

debugWindow.JPG
 
Last edited:
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones