如何添加片段,以便我的应用程序在屏幕上不显示为空白

时间:2018-03-13 10:34:57

标签: java android android-layout android-studio android-fragments

我正在制作Udacity Sunshine应用程序。由于使用的android studio的版本是0.5.8,现在我正在使用android studio 3.0.1。因此,在这两种方法中使用的编码技术之间存在很多差距。 所以,这里我在空活动中使用了片段,我通过阅读关于片段的developer.android.com文章实现了它,我得到了关于它的抽象概念,但仍然很多不清楚。 所以我完成了添加片段并将假数据添加到列表视图以填充它。 但是当我在我的设备上运行该应用程序时,它仍然显示一个空白屏幕,并且没有我在代码中添加的文本视图:

布局/ 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=".MainActivity"
tools:ignore="MergeRootFrame" />

布局/ fragment_layout.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"
    tools:context=".ArticleFragment"
    tools:showIn="@layout/activity_main">

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

</FrameLayout>

布局/ list_of_item_forecast.xml

 <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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"
    tools:layout="@layout/fragment_layout"/>

包/ MainActivity.java

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {

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

包/ ArticleFragment.java

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;

    public class ArticleFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    // Create some dummy data for the ListView.  Here's a sample weekly forecast
    String[] data = {
            "Mon 6/23 - Sunny - 31/17",
            "Tue 6/24 - Foggy - 21/8",
            "Wed 6/25 - Cloudy - 22/17",
            "Thurs 6/26 - Rainy - 18/11",
            "Fri 6/27 - Foggy - 21/10",
            "Sat 6/28 - TRAPPED IN WEATHERSTATION - 23/18",
            "Sun 6/29 - Sunny - 20/7"
    };
    List<String> weekForecast = new ArrayList<String>(Arrays.asList(data));

    // Now that we have some dummy forecast data, create an ArrayAdapter.
    // The ArrayAdapter will take data from a source (like our dummy forecast) and
    // use it to populate the ListView it's attached to.
    ArrayAdapter<String> mForecastAdapter =
            new ArrayAdapter<String>(
                    getActivity(), // The current context (this activity)
                    R.layout.list_item_forecast, // The name of the layout ID.
                    R.id.list_item_forecast_textview, // The ID of the textview to populate.
                    weekForecast);
    View rootView = inflater.inflate(R.layout.fragment_layout, container, false);

    // Get a reference to the ListView, and attach this adapter to it.
    ListView listview = (ListView) rootView.findViewById(
            R.id.listview_forecast);
    listview.setAdapter(mForecastAdapter);
    // Inflate the layout for this fragment
    return rootView;
}
}

Screenshot of app on device

3 个答案:

答案 0 :(得分:2)

您的代码存在的问题是您没有实例化您创建的Fragment类。您需要对其进行实例化并将其传递给FragmentManager,并同时更改主活动中的setContentView(R.layout.fragment_layout)

只需将您的MainActivity.java课改为此课程即可解决问题。无需更改布局文件。

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.container, new ArticleFragment())
                .commit();
    }
}

答案 1 :(得分:1)

我在这里看到两个可能的问题。首先,您应该有另一个布局,可能是activity_main.xml作为<LinearLayout 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/my_linear_layout_root" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" tools:ignore="MergeRootFrame" /> </LinearLayout> 文件中的根视图,如下所示:

public class MainActivity extends FragmentActivity {

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

        // Don't leave all this in your onCreate, try refactor
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        ArticleFragment fragment = new ArticleFragment();
        fragmentTransaction.add(R.id.container, fragment);
        fragmentTransaction.commit();
     }
}

然后在你的活动中:

def add_more_images(new_images)
    @account = Account.find(params[:id])
    images = @account.images.present? ? @account.images : []
    images << new_images
    @account.update_attributes(images: images)
end

我没有看到一个framelayout用作活动的根视图和片段容器,这就是为什么我可能错了,但这就是我通常会将一个片段添加到容器中。

答案 2 :(得分:-1)

您应该浏览以下链接:

https://www.raywenderlich.com/169885/android-fragments-tutorial-introduction-2

您未设置新活动的内容视图。您可能正在推送StaticFragment Activity,但是您没有加载视图的xml。在你的yourActivity的super.onCreate()之后,添加setContentView(R.layout.your_fragment.xml);

应该解决问题。