出现的白色屏幕,当叫片段时

时间:2017-11-28 08:16:52

标签: android android-fragments

我是android新手。所以,我一直在玩碎片来了解它们是如何工作的。

首先,我创建了calciKeyboard类,扩展了Fragment

代码:

public class calciKeyboard extends Fragment {
    @Nullable
    Button b;

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        GridLayout gridLayout=(GridLayout) inflater.inflate(R.layout.calci_keyboard,container,false);
        for(int i=0;i<gridLayout.getChildCount();i++){
            b=(Button)gridLayout.getChildAt(i);
            if (b != null) {
                b.setBackground(getResources().getDrawable(R.drawable.button_dark_gradient));
            }
        }
        return gridLayout;
    }
}

calciKeyboard.xml

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:rowCount="4"
    android:columnCount="4"
    >
    Buttons inside....
</GridLayout>

advance_mix

public class advance_mix extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.advance_length);
    }
}

advance_length

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

    <fragment android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="com.example.tilak.myfirstapplication.calciKeyboard"
        android:id="@+id/calciKeyboard" />
</GridLayout>

go_advance.java

 findViewById(R.id.redirectLength).setOnClickListener(
              new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      Intent i=new Intent(go_advance.this,advance_mix.class);
                      startActivity(i);
                  }
              }
      );

go_advance类包含启动advance_mix活动的意图,该活动进一步列出了advance_length布局文件。

问题是,当点击redirect_length按钮时,它会显示一个白色屏幕。

为什么会这样?

2 个答案:

答案 0 :(得分:1)

您应该return 查看

  View getRootView = inflater.inflate(R.layout.calci_keyboard,container,false);

   GridLayout gridLayout=(GridLayout)getRootView.findViewById(R.id._gridLAYOUT); 

          for(int i=0;i<gridLayout.getChildCount();i++){
                    b=(Button)gridLayout.getChildAt(i);
                    if (b != null) {
                        b.setBackground(getResources().getDrawable(R.drawable.button_dark_gradient));
                    }

     return getRootView ;

<强> XML

 <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/_gridLAYOUT">

  <fragment android:layout_width="match_parent"  //Change this to match_parent
        android:layout_height="match_parent"   //Change this to match_parent
        android:name="com.example.tilak.myfirstapplication.calciKeyboard"
        android:id="@+id/calciKeyboard" />
</GridLayout>

答案 1 :(得分:0)

行。尝试使用“android.support.v4.app.Fragment”而不是“android.app.Fragment”,然后再考虑“Button b;”在@Nullable之前声明