在这个程序中,我试图将ArrayList
充气到其中有TextView
的布局。所以我的应用程序运行正常,但当我把它加载到我的手机时它显示空白屏幕。
这是我的 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>
在此我试图在屏幕上建立预定义天气报告列表。
答案 0 :(得分:0)
所以基本上你只发布了你的片段代码。您将拥有一个MainActivity类。在这个类中,您需要添加此片段。喜欢这个
Fragment newFragment = new MainActivityFragment();
getSupportFragmentManager().beginTransaction().add(R.id.container, newFragment).commit();
在此处发布您的MainActivity代码。
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);
//缺少此代码
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);
}
}