碎片将不会延长活动ActionBarActivity现身

碎片将不会延长活动ActionBarActivity现身

本文介绍了碎片将不会延长活动ActionBarActivity现身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有延伸ActionBarActivity主要活动。在我的活动我创建一个没有显示出来的片段。我修改的主要活动延长FragmentActivity和在这种情况下,片段被显示出来。为什么不片段显示出来时,我的活动来延长ActionBarActivity?我知道,ActionBarActivity延伸FragmentActivity所以理论上ActionBarActivity应该支持片段。

编辑:我解决它。我不会设置在我的活动中的任何内容视图。现在,我将它设置它的工作原理,当我向ActionBarActivity。不过奇怪的是,即使我不设置它,当我向FragmentActivity它的工作原理。

这是我的主要活动code:

 包com.example.yamba;

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

公共类StatusActivity扩展ActionBarActivity {

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    如果(savedInstanceState == NULL)
    {
        StatusFragment片段=新StatusFragment();

        FragmentTransaction FR = getSupportFragmentManager()的BeginTransaction()。
        fr.add(android.R.id.content,片段);
        fr.commit();
    }
}

@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    //充气菜单;这增加了项目操作栏,如果它是present。
    。getMenuInflater()膨胀(R.menu.status,菜单);
    返回true;
}

@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    //处理动作栏项目点击这里。将操作栏
    //自动在主/向上按钮操作的点击,只要
    //你在AndroidManifest.xml中指定一个父活动。
    INT的id = item.getItemId();
    如果(ID == R.id.action_settings){
        返回true;
    }
    返回super.onOptionsItemSelected(项目);
}
}
 

这是我的片段code:

 包com.example.yamba;

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

进口android.view.LayoutInflater;
进口android.view.ViewGroup;
进口android.support.v4.app.Fragment;

进口com.marakana.android.yamba.clientlib.YambaClient;
进口com.marakana.android.yamba.clientlib.YambaClientException;

公共类StatusFragment扩展片段工具OnClickListener
{
私有静态最后字符串变量=StatusFragment;
私人的EditText editStatus;
私人按钮buttonTweet;
私人TextView的textCount;
私人诠释defaultColor;

@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState)
{
    super.onCreate(savedInstanceState);

    查看查看= inflater.inflate(R.layout.activity_fragment,集装箱,假);

    editStatus =(EditText上)view.findViewById(R.id.editStatus);
    buttonTweet =(按钮)view.findViewById(R.id.buttonTweet);
    textCount =(TextView中)view.findViewById(R.id.textCount);

    buttonTweet.setOnClickListener(本);
    defaultColor = textCount.getTextColors()getDefaultColor()。
    editStatus.addTextChangedListener(新TextWatcher()
    {
        公共无效afterTextChanged(编辑S)
        {
            诠释计数= 140  -  editStatus.length();
            textCount.setText(Integer.toString(计数));
            textCount.setTextColor(Color.GREEN);

            如果(计数小于20&安培;&安培; COUNT> = 10)
                textCount.setTextColor(Color.YELLOW);
            否则,如果(计数小于10)
                textCount.setTextColor(Color.RED);
            其他
                textCount.setTextColor(defaultColor);
        }

        公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,之后INT){}

        公共无效onTextChanged(CharSequence中,诠释开始,诠释B,INT C){}
    });

    返回查看;
}

公共无效的onClick(视图查看)
{
    字符串状态= editStatus.getText()的toString()。
    Log.d(TAG,onClicked,状态:+状态);

    新PostTask()执行(状态);
    editStatus.getText()清()。
}

私人final类PostTask扩展的AsyncTask<字符串,太虚,字符串>
{
    保护字符串doInBackground(字符串... PARAMS)
    {
        YambaClient yambaCloud =新YambaClient(学生,密码);

        尝试
        {
            yambaCloud.postStatus(PARAMS [0]);
            返回Succesfuly贴!;
        }
        赶上(YambaClientException E)
        {
            e.printStackTrace();
            返回无法张贴到扬巴服务队!
        }
    }

    @覆盖
    保护无效onPostExecute(字符串结果)
    {
        super.onPostExecute(结果);

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

解决方案

这似乎是一个的。作为一种解决方法,你可以尝试添加片段后命令里面。它奏效了我。

 新的处理程序()。后(新的Runnable(){
    @覆盖
    公共无效的run(){
        StatusFragment片段=新StatusFragment();

        FragmentTransaction FR = getSupportFragmentManager()的BeginTransaction()。
        fr.add(android.R.id.content,片段);
        fr.commit();
    }
});
 

I have a main activity extending ActionBarActivity. In my activity i create a fragment which isn't showing up. I modified the main activity to extend FragmentActivity and in this case the fragment is showing up. Why isn't the fragment showing up when my activity extends ActionBarActivity? I know that ActionBarActivity extends FragmentActivity so in theory ActionBarActivity should support fragments.

Edit: I resolved it. I wouldn't set any content view on my activity. Now that i set it it works when i extend ActionBarActivity. Still is weird that even if i don't set it, when i extend FragmentActivity it works.

This is my main activity code:

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);
}
}

This is my fragment code:

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();
    }
}
}
解决方案

It appears to be a bug. As a workaround you could try to add fragment inside a post command. It did the trick for me.

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();
    }
});

这篇关于碎片将不会延长活动ActionBarActivity现身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 17:36