简体   繁体   中英

why it is showing blank white screen when i am load the app in which i am trying to inflate arraylist onto a layout?

In this program I am trying to inflate the ArrayList onto the layout which is having a TextView in it. So my app is running fine but when I load it on to my phone it shows blank screen.

Here is my MainActivityFragment.class :

package com.example.sagar.sunshine.app;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * A placeholder fragment containing a simple view.
 */
public class MainActivityFragment extends Fragment {

    public MainActivityFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        String forecastArray[] = {
                "Today-Sunny-88/63",
                "Tommorrow-Foggy-70/40",
                "Wens-Cloudy-72/63",
                "Thrus-Asteroids-75/65",
                "fri-Heavy Rain-60/51",
                "Sat-Help Trapped In WeatherStation-60/51",
                "Sun-Sunny-80/68"
        };
        List<String> weekForecast = new ArrayList<String>(Arrays.asList(forecastArray));

        ArrayAdapter mForecastAdapter = new ArrayAdapter<String>(
                // 1st parameter-Context gets the fragment activity
                getActivity(),
                //2nd parameter-Name of   the layout
                R.layout.list_item_forecast,
                //3rd parameter-Id of the list textview
                R.id.list_item_forecast_textview,
                //4th parameter-arraylist
                weekForecast
        );

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

        return rootView;
    }
}

fragment_main.xml :

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivityFragment">

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

</FrameLayout>

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:name="com.example.sagar.sunshine.app.MainActivityFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.sagar.sunshine.app.MainActivity"
    tools:ignore="MergeRootFrame"/>

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="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:id="@+id/list_item_forecast_textview">

</TextView>

In this I am trying to build a list of predefined weather reports on to the screen.

So basically you have posted only your fragment code. You will be having a MainActivity class. In this class you need to add this fragment. Like this

Fragment newFragment = new MainActivityFragment();
getSupportFragmentManager().beginTransaction().add(R.id.container, newFragment).commit();

Post your MainActivity code here.

MainActivity.class:

package com.example.sagar.sunshine.app

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

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

//This code was missing

  Fragment newFragment = new MainActivityFragment();
        getSupportFragmentManager().beginTransaction().add(R.id.container, newFragment).commit();
        }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM