Announcement

Collapse
No announcement yet.

Probleme beim Layout/Seitenwechsel

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Probleme beim Layout/Seitenwechsel

    Hallo zusammen,

    ich hoffe Ihr könnt mir weiterhelfen.

    Ich habe mir drei Seiten (Fragemente) als Hauptnavigation angelet.
    Vom ersten Segment möchte ich dann noch über ein Button auf eine weitere Seite wechseln.
    Das Funktioniert auch so weit. Nur habe ich das Problem, wenn ich auf der weiteren Seite bin
    und wieder mit der Zurücktaste zu meiner Hauptansicht mit den drei Fragmenten zurückgehren möchte,
    stürzt die App ab (im Simulator, wie auch auf einem Smartphone).

    Anbei den Quellcode dazu, einmal aus der Main Activity:
    Code:
    package main.activity;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.Fragment;
    //import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends Activity 
    {
    	
    	Fragment fragment;
    	Button btn1, btn2, btn3;
    	
    	@Override
    	public void onCreate(Bundle savedInstanceState) 
    	{
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
    		
    		btn1=(Button)findViewById(R.id.btn1);
    		btn2=(Button)findViewById(R.id.btn2);
    		btn3=(Button)findViewById(R.id.btn3);
    		
    		android.app.FragmentManager fm = getFragmentManager();
    		android.app.FragmentTransaction ft = fm.beginTransaction();
    		
    		StartFragment myFragment = new StartFragment();
    		ft.add(R.id.myFragment, myFragment);
    		ft.commit();
    		
    		btn1.setOnClickListener(btnOnClickListener);
    		btn2.setOnClickListener(btnOnClickListener);
    		btn3.setOnClickListener(btnOnClickListener);
    		
    	}
    
    		Button.OnClickListener btnOnClickListener = new Button.OnClickListener()
    		{
    			@Override
    			public void onClick(View v)
    			{
    				
    				Fragment newFragment;
    				
    				if(v == btn1)
    				{
    					newFragment = new Fragment1();
    				}
    				else if (v == btn2)
    				{
    					newFragment = new Fragment2();
    				}
    				else if (v == btn3)
    				{
    					newFragment = new Fragment3();
    				}
    				else
    				{
    					newFragment = new StartFragment();
    				}
    				android.app.FragmentTransaction transaction = getFragmentManager().beginTransaction();
    				transaction.replace(R.id.myFragment, newFragment);
    				transaction.addToBackStack(null);
    				transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    				transaction.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.main, menu);
    		return true;
    	}
    
    	public void bt_wohnzimmer (View view)
    	{
    		setContentView(R.layout.seite_wohnzimmer);
    	}
    	
    }

    und der Quellcode aus der Layout Main.xml:
    Code:
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" 
        android:background="#1f1f1f">
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <Button
                android:id="@+id/btn1"
                style="?android:attr/borderlessButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#0000cc"
                android:text="@string/frag1"
                android:textColor="#ffffff"
                android:textSize="@dimen/activity_horizontal_margin" />
                
            <Button
                android:id="@+id/btn2"
                style="?android:attr/borderlessButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#0000cc"
                android:text="@string/frag2"
                android:textColor="#ffffff"
                android:textSize="@dimen/activity_horizontal_margin" />
    
            <Button
                android:id="@+id/btn3"
                style="?android:attr/borderlessButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#0000cc"
                android:text="@string/frag3"
                android:textColor="#ffffff"
                android:textSize="@dimen/activity_horizontal_margin" />
    
        </LinearLayout>
    
    	<LinearLayout 
    	    android:layout_width="fill_parent" 
    	    android:layout_height="fill_parent" 
    	    android:orientation="horizontal"
    	    android:baselineAligned="false">
    
    		<LinearLayout
    		    android:id="@+id/myFragment"
    		    android:layout_width="0dip"
      			android:layout_height="fill_parent"		  
    		    android:layout_weight="3"
    		    android:orientation="horizontal" >
    		</LinearLayout>
    
    	    </LinearLayout>
    
    </LinearLayout>
    Die Navigation in den Drei Fragment Seiten funktioniert.
    Das Problem tritt auf, wenn ich vom ersten Fregment auf meine Seite "seite_wohnzimmer" wechselele und dann wieder mit der Zurücktaste
    zu den drei Haupt Segmenten zurückkehren möchte.

    Ich hoffe Ihr könnt mir helfe.


    Gruß Daniel

  • #2
    Da die Frage nichts mit Eclipse zu tun hat -> verschoben
    Christian

    Comment

    Working...
    X