Android片段不膨胀

时间:2019-02-07 02:18:02

标签: java android fragment layout-inflater

我只是想为使用RSS提要显示新闻报道的新闻阅读器项目添加一个简单的片段。任务只是将活动转换为片段并保留功能。我已经尝试了好几天才能使它正常工作。

目前唯一的目标是使片段膨胀并显示测试按钮,文本视图和空列表视图。我已经多次检查了片段XML,没有发现错误。据我了解,在ActivityItems中显示fragment_items应该可以。我正按照我的教科书描述如何将片段充气无济于事地执行这种充气。我尝试过搜索logcat的每一行,并读取尽可能多的线程,但还没有找到解决方案。

ItemsActivity.java

p = subprocess.Popen(**kwargs)

activity_items.xml

package com.murach.newsreader;

import java.util.ArrayList;
import java.util.HashMap;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ItemsActivity extends Activity {

private RSSFeed feed;
private FileIO io;

private TextView titleTextView;
private ListView itemsListView;

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

    io = new FileIO(getApplicationContext());

    titleTextView = (TextView) findViewById(R.id.titleTextView);
    itemsListView = (ListView) findViewById(R.id.itemsListView);
}

class DownloadFeed extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        io.downloadFile();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        Log.d("News reader", "Feed downloaded");
        new ReadFeed().execute();
    }
}

class ReadFeed extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        feed = io.readFile();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        Log.d("News reader", "Feed read");

        // update the display for the activity
        ItemsActivity.this.updateDisplay();
    }
}

public void updateDisplay()
{
    if (feed == null) {
        titleTextView.setText("Unable to get RSS feed");
        return;
    }

    // set the title for the feed
    titleTextView.setText(feed.getTitle());

    // get the items for the feed
    ArrayList<RSSItem> items = feed.getAllItems();

    // create a List of Map<String, ?> objects
    ArrayList<HashMap<String, String>> data =
            new ArrayList<HashMap<String, String>>();
    for (RSSItem item : items) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("date", item.getPubDateFormatted());
        map.put("title", item.getTitle());
        data.add(map);
    }

    // create the resource, from, and to variables
    int resource = R.layout.listview_item;
    String[] from = {"date", "title"};
    int[] to = {R.id.pubDateTextView, R.id.titleTextView};

    // create and set the adapter
    SimpleAdapter adapter =
            new SimpleAdapter(this, data, resource, from, to);
    itemsListView.setAdapter(adapter);

    Log.d("News reader", "Feed displayed");
}

public void onItemClick(AdapterView<?> parent, View v,
                        int position, long id) {

    // get the item at the specified position
    RSSItem item = feed.getItem(position);

    // create an intent
    Intent intent = new Intent(this, ItemActivity.class);

    intent.putExtra("pubdate", item.getPubDate());
    intent.putExtra("title", item.getTitle());
    intent.putExtra("description", item.getDescription());
    intent.putExtra("link", item.getLink());

    this.startActivity(intent);
}
}

ItemsFragment.java

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/titleTextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFAC83"
    android:padding="7dp"
    android:text="@string/items_title"
    android:textSize="22sp" />

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

</LinearLayout>

fragment_items.xml

package com.murach.newsreader;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class ItemsFragment extends Fragment {

private Button testButton;
private TextView titleTextView;
private ListView itemsListView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,     Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_items, container,     false);

    testButton = (Button) view.findViewById(R.id.testButton);
    titleTextView = (TextView) view.findViewById(R.id.titleTextView);
    itemsListView = (ListView) view.findViewById(R.id.itemsListView);

    return view;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
}
}

日志

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<fragment android:name="com.murach.newsreader.ItemsFragment"
    android:id="@+id/fragment_items"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<TextView
    android:id="@+id/titleTextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#FFAC83"
    android:padding="7dp"
    android:text="@string/items_title"
    android:textSize="22sp" />

<Button
    android:id="@+id/testButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="TEST BUTTON" />

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

</LinearLayout>

<pre> at com.murach.newsreader.ItemsFragment.onCreateView(ItemsFragment.java:25) at android.app.Fragment.performCreateView(Fragment.java:2522) at android.app.FragmentManagerImpl.ensureInflatedFragmentView(FragmentManager.jav a:1486) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1269) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1481) at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1723) at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3556) at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:186 ) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730) 02-06 20:45:13.014 4765-4765/? E/AndroidRuntime: at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1481) at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1723) at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3556) at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:186 ) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730) at android.view.LayoutInflater.rInflate(LayoutInflater.java:863) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824) at android.view.LayoutInflater.inflate(LayoutInflater.java:515) at android.view.LayoutInflater.inflate(LayoutInflater.java:423) at com.murach.newsreader.ItemsFragment.onCreateView(ItemsFragment.java:25) at android.app.Fragment.performCreateView(Fragment.java:2522) at android.app.FragmentManagerImpl.ensureInflatedFragmentView(FragmentManager.jav a:1486) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1269) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1481) at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1723) 应该只增加片段xml布局并显示测试按钮和其他小部件。我尝试删除所有与膨胀片段无关的代码以及与小部件有关的所有代码/ xml,但仍然崩溃。我什至创建了一个测试项目,但完全没有,只不过是带有测试按钮的初始活动和带有测试按钮的片段,而layoutinflater仍然无法正常工作。

0 个答案:

没有答案
相关问题