0

I am trying to display a listview of items at the launch of my application.But at the launch of the application no listview is displayed but only a blank screen.Below are the codes:

MainActivity.java

public class MainActivity extends ActionBarActivity {

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


public static class PlaceholderFragment extends Fragment
{
    ArrayAdapter<String> mForecastAdapter;
    public PlaceholderFragment()
    {}

    @Override
    public View onCreateView(LayoutInflater inflator, ViewGroup v, Bundle savedInstanceState)
    {
        View rootView = inflator.inflate(R.layout.fragment_main, v);

        String[] forecastArray= {
                "Today - Sunny - 88/63",
                "Monday - foggy - 67/20",
                "Tuesday - Sunny - 88/63"
                };

        List<String> weekforecast= new ArrayList<String>(Arrays.asList(forecastArray));

        mForecastAdapter= new ArrayAdapter<String>(
                getActivity(),
                R.layout.list_item_forecast,
                R.id.list_item_forecast_textview,
                weekforecast);

        ListView listview =(ListView)rootView.findViewById(R.id.listview_forecast);
        listview.setAdapter(mForecastAdapter);


        return rootView;
    }


}

}

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sunshineapp.MainActivity" 
tools:ignore="MergeRootFrame" />

fragment_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:paddingLeft="64dp" 
android:paddingRight="64dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
tools:context="com.example.sunshineapp.MainActivity$PlaceholderFragment" >

<ListView
    android:id="@+id/listview_forecast"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     />

list_item_forecast.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical" 
android:id="@+id/list_item_forecast_textview"/>

Can Someone please guide me with the issue? Thanks in advance!

Edit: As suggested by @Sanjeet I had forgotten to attach the fragment to the activity.So I attached it as follows in the onCreate method of activity:

if(savedInstanceState == null)
    {
    android.app.FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction  =fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.container, new PlaceholderFragment());
    fragmentTransaction.commit();   

    }  

But now I am getting the following in the logcat:

08-03 11:55:37.365: E/AndroidRuntime(20088): FATAL EXCEPTION: main
08-03 11:55:37.365: E/AndroidRuntime(20088): java.lang.RuntimeException:   Unable to start       activity ComponentInfo{com.example.sunshineapp/com.example.sunshineapp.MainActivity}:     java.lang.IllegalStateException: The specified child already has a parent. You must call     removeView() on the child's parent first.
08-03 11:55:37.365: E/AndroidRuntime(20088):    at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
08-03 11:55:37.365: E/AndroidRuntime(20088):    at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
08-03 11:55:37.365: E/AndroidRuntime(20088):    at android.app.ActivityThread.access$600    (ActivityThread.java:162)

And the application stops..Please help!

ghostrider
  • 2,046
  • 3
  • 23
  • 46

5 Answers5

1

To me somehow helped to update the adapter simply:

ListView listview =(ListView)rootView.findViewById(R.id.listview_forecast);
        listview.setAdapter(mForecastAdapter);
        mForecastAdapter.notifyDataSetChanged();

        return rootView;
1

it seems to me that your mForecastAdapter array is empty. try to add some values inside for testing first.

Giorgos Neokleous
  • 1,709
  • 1
  • 14
  • 25
  • It isn't empty, he initializes it here: `mForecastAdapter= new ArrayAdapter( getActivity(), R.layout.list_item_forecast, R.id.list_item_forecast_textview, weekforecast);` – frgnvola Aug 02 '15 at 18:02
1

Try changing

List<String> weekforecast= new ArrayList<String (Arrays.asList(forecastArray));

to

ArrayList<String> weekforecast= new ArrayList<String>(Arrays.asList(forecastArray));
frgnvola
  • 518
  • 3
  • 16
1

Try changing

View rootView = inflator.inflate(R.layout.fragment_main, v);

to

View rootView = inflator.inflate(R.layout.fragment_main, v,false);

For more info about false in last parameter. You should check this answer.

Hope this helps.

Community
  • 1
  • 1
Ye Min Htut
  • 2,904
  • 15
  • 28
1

You should use

View rootView = inflator.inflate(R.layout.fragment_main, v,false);

instead of

View rootView = inflator.inflate(R.layout.fragment_main, v);
Sanjeet A
  • 5,171
  • 3
  • 23
  • 40