碎片不会出现在扩展ActionBarActivity的活动中

时间:2014-10-29 15:00:57

标签: java android android-fragments

我有一个扩展ActionBarActivity的主要活动。在我的活动中,我创建了一个不会出现的片段。我修改了主要活动以扩展FragmentActivity,在这种情况下片段显示出来。当我的活动扩展ActionBarActivity时,为什么片段不显示?我知道ActionBarActivity扩展了FragmentActivity,所以理论上ActionBarActivity应该支持片段。

编辑:我解决了。我不会在我的活动中设置任何内容视图。现在,我设置它,它扩展ActionBarActivity时工作。仍然很奇怪,即使我没有设置它,当我扩展FragmentActivity它也有效。

这是我的主要活动代码:

package com.example.yamba;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
//import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;

public class StatusActivity extends ActionBarActivity {

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

    if(savedInstanceState == null)
    {
        StatusFragment fragment = new StatusFragment();

        FragmentTransaction fr = getSupportFragmentManager().beginTransaction();
        fr.add(android.R.id.content, fragment);
        fr.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.status, 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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

这是我的片段代码:

package com.example.yamba;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Toast;
import android.os.AsyncTask;
import android.text.TextWatcher;
import android.text.Editable;

import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.support.v4.app.Fragment;

import com.marakana.android.yamba.clientlib.YambaClient;
import com.marakana.android.yamba.clientlib.YambaClientException;

public class StatusFragment extends Fragment implements OnClickListener
{
private static final String TAG = "StatusFragment";
private EditText editStatus;
private Button buttonTweet;
private TextView textCount;
private int defaultColor;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    View view = inflater.inflate(R.layout.activity_fragment, container, false);

    editStatus = (EditText) view.findViewById (R.id.editStatus);
    buttonTweet = (Button) view.findViewById (R.id.buttonTweet);
    textCount = (TextView) view.findViewById (R.id.textCount);

    buttonTweet.setOnClickListener(this);
    defaultColor = textCount.getTextColors().getDefaultColor();
    editStatus.addTextChangedListener(new TextWatcher()
    {
        public void afterTextChanged (Editable s)
        {
            int count = 140 - editStatus.length();
            textCount.setText(Integer.toString(count));
            textCount.setTextColor(Color.GREEN);

            if(count<20  && count >= 10)
                textCount.setTextColor(Color.YELLOW);
            else if (count<10)
                textCount.setTextColor(Color.RED);
            else
                textCount.setTextColor(defaultColor);
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after){}

        public void onTextChanged (CharSequence s, int start, int b, int c){}
    });

    return view;
}

public void onClick(View view)
{
    String status = editStatus.getText().toString();
    Log.d(TAG, "onClicked with status: " + status);

    new PostTask().execute(status);
    editStatus.getText().clear();
}

private final class PostTask extends AsyncTask<String, Void, String>
{
    protected String doInBackground(String... params)
    {
        YambaClient yambaCloud = new YambaClient("student", "password");

        try
        {
            yambaCloud.postStatus(params[0]);
            return "Succesfuly posted!";
        }
        catch (YambaClientException e)
        {
            e.printStackTrace();
            return "Failed to post to yamba sevice!";
        }
    }

    @Override
    protected void onPostExecute (String result)
    {
        super.onPostExecute(result);

        Toast.makeText(StatusFragment.this.getActivity(), result, Toast.LENGTH_LONG).show();
    }
}
}

2 个答案:

答案 0 :(得分:0)

您正在将FragmentManager方法add调用到错误的容器中。您提供的ID必须引用作为活动布局一部分的视图(通常是布局)。所以android.R.id.content肯定是错的,查看你的活动xml布局并找到正确的id(也许是R.id.content(没有android)?)

答案 1 :(得分:0)

它似乎是bug。作为一种解决方法,您可以尝试在post命令中添加片段。它为我做了诀窍。

new Handler().post(new Runnable() {
    @Override
    public void run() {
        StatusFragment fragment = new StatusFragment();

        FragmentTransaction fr = getSupportFragmentManager().beginTransaction();
        fr.add(android.R.id.content, fragment);
        fr.commit();
    }
});