片段交易既不工作也不给错误

时间:2016-08-06 10:12:06

标签: android android-fragments fragmenttransaction

我是Android新手,我正在尝试了解Fragment Transaction。但我没有得到所需的输出或任何有关失败的错误。请帮帮我。

MainActivity:

package com.example.runwaylabz.canvinew;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;

public class MainActivity extends AppCompatActivity {

public static FragmentManager fragmentmanager;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    fragmentmanager = getSupportFragmentManager();
    fragmentmanager.beginTransaction().add(R.id.main_layout,new Canvi_Select(),"add_canvi_select").commit();


}

@Override
protected void onDestroy() {
    super.onDestroy();

}
}

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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"
    android:background="@color/cardview_light_background"
    android:id="@+id/main_layout"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.runwaylabz.canvinew.MainActivity"
    android:orientation="horizontal">

</LinearLayout>

CanviSelect:

package com.example.runwaylabz.canvinew;


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.Button;



public class Canvi_Select extends Fragment implements View.OnClickListener {


private Button button1,button2,button3;

public Canvi_Select() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v=inflater.inflate(R.layout.fragment_canvi__select, container, false);
    button1= (Button) v.findViewById(R.id.button8nodes);
    button2= (Button) v.findViewById(R.id.button4nodes);
    button3= (Button) v.findViewById(R.id.buttonplug);
    button1.setOnClickListener(this);
    button2.setOnClickListener(this);
    button3.setOnClickListener(this);
    return v;
}

@Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.button8nodes:
            MainActivity.dataconnection.setType(8);
            break;
        case R.id.button4nodes:
            MainActivity.dataconnection.setType(4);
            break;
        case R.id.buttonplug:
            MainActivity.dataconnection.setType(1);
            break;
    }
    MainActivity.fragmentmanager.beginTransaction().replace(R.id.main_layout,new CanviSearch(),"add_canvi_search").addToBackStack("canvi_search_back").commit();
}
}

1 个答案:

答案 0 :(得分:0)

尝试更改您的activity_main.xml,如下所示:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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"
    android:background="@color/cardview_light_background"
    android:id="@+id/main_layout"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.runwaylabz.canvinew.MainActivity"
    android:orientation="horizontal">
    <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/frag_container"/>

</LinearLayout>

现在,

 getSupportFragmentManager().beginTransaction().add(R.id.frag_container,new Canvi_Select(),"add_canvi_select").commit();