2017-06-23 1 views
0

Ich bin nicht in Lage, meine MainActivity mit beliebigem Fragment zu verknüpfen.
Hier ist meine MainActivity.java:Verknüpfen von LinearLayout-Aktivität mit einem Fragment

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 

Button btn1,btn2,btn3,btn4,btn5; 

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

    btn1=(Button)findViewById(R.id.btn1); 
    btn2=(Button)findViewById(R.id.btn2); 
    btn3=(Button)findViewById(R.id.btn3); 
    btn4=(Button)findViewById(R.id.btn4); 
    btn5=(Button)findViewById(R.id.btn5); 

    btn1.setOnClickListener(this); 
    btn2.setOnClickListener(this); 
    btn3.setOnClickListener(this); 
    btn4.setOnClickListener(this); 
    btn5.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 

    onItemSelected(v.getId()); 
    /*Fragment fragment=null; 
    int id=v.getId(); 
    switch(id){ 
     case R.id.btn1: 
      fragment=new Btn1Fragment(); 
      break; 
     case R.id.btn2: 
      fragment=new Btn2Fragment(); 
      break; 
     case R.id.btn3: 
      fragment=new Btn3Fragment(); 
      break; 
     case R.id.btn4: 
      fragment=new Btn4Fragment(); 
      break; 
     case R.id.btn5: 
      finish(); 
    } 

    FragmentManager fm=getSupportFragmentManager(); 
    fm.beginTransaction().replace(R.id.main,fragment).commit(); 
    LinearLayout linear=(LinearLayout)findViewById(R.id.main);*/ 


} 

private void onItemSelected(int id) { 
    Fragment fragment=null; 
    switch(id){ 
     case R.id.btn1: 
      fragment=new Btn1Fragment(); 
      break; 
     case R.id.btn2: 
      fragment=new Btn2Fragment(); 
      break; 
     case R.id.btn3: 
      fragment=new Btn3Fragment(); 
      break; 
     case R.id.btn4: 
      fragment=new Btn4Fragment(); 
      break; 
     case R.id.btn5: 
      finish(); 
    } 

    FragmentManager fm=getSupportFragmentManager(); 
    fm.beginTransaction().replace(R.id.main,fragment).addToBackStack(null).commit(); 
} 

Und ein Fragment:

public class Btn1Fragment extends Fragment { 
@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    View view= inflater.inflate(R.layout.fragment_btn1,container,false); 
    return view; 
} 

Hilfe mich jemand.

+0

lesen https://examples.javacodegeeks.com/android/core/app/fragment/android-fragments-example/ –

+0

versuchen dann ersetzen Fragment anstelle hinzuzufügen, und sicher sein, dass Sie Fragmente hinzufügen zum framelayout in der Hauptaktivität genommen –

+0

Ich habe versucht, auch zu addieren, aber Scheiße funktioniert nicht xD Yup .. ich füge es hinzu, obwohl ich LinearLayout am Main verwende. –

Antwort

0

Sie können den Code unten versuchen: -

 public class MainActivity extends AppCompatActivity implements View.OnClickListener { 

     Button btn1,btn2,btn3,btn4,btn5; 
     Fragment fragment; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      btn1=(Button)findViewById(R.id.btn1); 
      btn2=(Button)findViewById(R.id.btn2); 
      btn3=(Button)findViewById(R.id.btn3); 
      btn4=(Button)findViewById(R.id.btn4); 
      btn5=(Button)findViewById(R.id.btn5); 

      btn1.setOnClickListener(this); 
      btn2.setOnClickListener(this); 
      btn3.setOnClickListener(this); 
      btn4.setOnClickListener(this); 
      btn5.setOnClickListener(this); 
     } 

     @Override 
     public void onClick(View v) { 
      switch(v.getId()){ 
       case R.id.btn1: 
        fragment=new Btn1Fragment(); 
        break; 
       case R.id.btn2: 
        fragment=new Btn2Fragment(); 
        break; 
       case R.id.btn3: 
        fragment=new Btn3Fragment(); 
        break; 
       case R.id.btn4: 
        fragment=new Btn4Fragment(); 
        break; 
       case R.id.btn5: 
        finish(); 
      } 

FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
transaction.replace(R.id.main, fragment); 
transaction.addToBackStack(null); 

// Commit the transaction 
transaction.commit(); 
     } 

    } 

Btn1Fragment

public class Btn1Fragment extends Fragment { 
@Override 
public View onCreateView(LayoutInflater inflater, 
     ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.fragment_btn1, null); 
return v; 
} 

verwenden xml.

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


<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" 
> 

<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/btn1" 
    android:text="Black Screen" 
    style="@style/textstyle" 
    /> 

<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/btn2" 
    android:text="Flashing Colors" 
    style="@style/textstyle" 
    android:layout_below="@+id/btn1"/> 

<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/btn3" 
    android:text="Back Button wali Screen" 
    style="@style/textstyle" 
    android:layout_below="@+id/btn2"/> 
<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/btn4" 
    android:text="Jaado Dekhlo" 
    style="@style/textstyle" 
    android:layout_below="@+id/btn3"/> 

<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/btn5" 
    android:text="Exit" 
    style="@style/textstyle" 
    android:layout_below="@+id/btn4"/> 


</LinearLayout> 

    <LinearLayout 
     android:id="@+id/main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
    /> 
</LinearLayout> 
+0

Soll ich ein Fragment Widget in main.xml mit "+ id/main" erstellen? –

+0

Nein, Sie sollten eine Linearlayout als Container von Fragment nehmen. –

+0

Aber es ersetzt immer noch nicht das Layout ... –

Verwandte Themen