我正在学习谷歌的纳米学位,并完成了天气应用程序的第一课。该应用程序使用我在创建项目时创建的片段。该视频显示了一个"占位符碎片"带有" oncreateview"的课程main_Activity.java文件中写入代码的方法。但是在我的Android工作室中,main_Activity.java中没有这种形式的类。相反,还有另一个名为mainActivityFragment的文件。此外,在xml文件中还有一个名为content_scrolling.xml的额外文件,其中包含指向片段xml文件的片段标记。我使用了其中一些文件,并做了一切必要的事情。应用程序运行但没有列表显示。该应用程序有一个工具栏,其中包含应用程序名称和白色屏幕。
检查代码。
Blankapp中的Main_Activity.xml,带有ftagment
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_scrolling" />
<!--
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />--></android.support.design.widget.CoordinatorLayout>
Content_scrolling.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment"
android:name="com.udacity_test.vatsan.udacity_test.MainActivityFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:layout="@layout/fragment_main" />
List_item_forecast.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/list_item_forecast_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"
></TextView>
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="com.udacity_test.vatsan.udacity_test.MainActivityFragment"
tools:showIn="@layout/activity_main">
<ListView
android:id="@+id/fragment_main_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
Main_Activity.java
package com.udacity_test.vatsan.udacity_test;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
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);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
/*FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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);
}
}
MainActivityFragment.java
package com.udacity_test.vatsan.udacity_test;
import android.support.v4.app.Fragment;
import android.os.Bundle;
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;
/**
* 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 tmp[]= {
"Sunday-", "monday", "tueday", "wednesday", "thursday", "friday", "saturday"
};
ArrayList<String> fake_data= new ArrayList<String>(Arrays.asList(tmp));
ArrayAdapter<String> adapt= new ArrayAdapter<String>(getActivity(),R.layout.list_item_forcast, R.id.list_item_forecast_textview,fake_data);
ListView mylistview= (ListView) rootView.findViewById(R.id.fragment_main_listview);
mylistview.setAdapter(adapt);
return inflater.inflate(R.layout.fragment_main, container, false);
}
}
任何帮助?
答案 0 :(得分:0)
在mainActivityFragment java文件中,在onCreateView方法中,您已将布局膨胀为变量“rootview”。由于您已使用rootview识别了listview,因此必须返回此rootview,而不是返回布局文件的新膨胀视图。
替换此行
return inflater.inflate(R.layout.fragment_main, container, false);
使用
return rootview;
希望这有帮助。