0

私はStack Overflowで同様の質問をしてきましたが、運がありません。私は最初の活動であるMapsActivityを持っています。 MapActivityレイアウトのFrameLayoutに2番目のアクティビティAboutActivityが表示されるように、LayoutInflaterを使用しました。私は、アプリケーションを実行すると、約0.5秒間私のAboutActivityが表示され、その後すぐに戻ってMapActivityに変更されます。私はlogcatに何のエラーもなく、両方の活動がマニフェストファイルにリストされています。AndroidStudio:なぜLayoutInflaterは古いアクティビティに戻りますか?

ここに私のMapsActivity(関連する部分のみ)があります。私のGoogleマップはフラグメントの中にあります(2番目のアクティビティが表示されることを願っています)。私は、メニューボタンがあり、ボタンの一つは、第二の活動につながる:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 
private String[] menuOptions; 
private DrawerLayout myDrawerLayout; 
private ListView myList; 
// private Fragment menuFragment; 
private MapFragment mMapFragment; 

private GoogleMap mMap; 

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

    if (findViewById(R.id.content_frame)!=null){ 
     if (savedInstanceState!=null){ 
      return; 
     } 
     // to add fragment dynamically, create new instance- add to frame layout 
     mMapFragment= MapFragment.newInstance(); 
     FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); 
     fragmentTransaction.add(R.id.content_frame, mMapFragment); 
     fragmentTransaction.commit(); 

     //make menu from array indices 
     menuOptions = getResources().getStringArray(R.array.menu_array); 
     myDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout); 
     myList= (ListView)findViewById(R.id.left_drawer); 
     //adapter for list view 
     myList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, menuOptions)); 
     //list's click listener 
     myList.setOnItemClickListener(new DrawerItemClickListener()); 

     Button menuButton = (Button) findViewById(R.id.menuButton); 
     menuButton.setOnClickListener(new View.OnClickListener(){ 
      public void onClick(View v){ 
       myDrawerLayout.openDrawer(Gravity.LEFT); 
      } 
     }); 

... 
private void selectItem(int position){ 
    switch(position) { 
     case 0: 
      //Highlight selected item 
      myList.setItemChecked(position, true); 
      myDrawerLayout.closeDrawer(myList); 
      break; 
     case 1: 
      //about 
      Intent switchIntent = new Intent(getApplicationContext(), AboutActivity.class); 
      startActivity(switchIntent); 
      break; 

ここに私のAboutActivity、第二の活動です。 activity_aboutは1秒間だけ表示されます。私は地図がどこにあったかを表示したい。ここで

public class AboutActivity extends MapsActivity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
//  setContentView(R.layout.activity_about); 
    FrameLayout frame = (FrameLayout) findViewById(R.id.content_frame); 
    getLayoutInflater().inflate(R.layout.activity_about, frame); 
} 

は、それは私が(私も各アクティビティにListViewコントロールメニューを持つことができますこの方法)表示されるように私の他の活動をしたいマップとでframeLayoutを備え、私のactivity_maps.xmlです:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <!-- The main content view --> 
    <FrameLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 
     <Button 
      android:id="@+id/menuButton" 
      android:layout_width="100dp" 
      android:layout_height="50dp" 
      android:text="Menu" /> 
    </FrameLayout> 

     <!-- The navigation drawer --> 
    <ListView android:id="@+id/left_drawer" 
      android:layout_width="240dp" 
      android:layout_height="match_parent" 
      android:layout_gravity="start" 
      android:choiceMode="singleChoice" 
      android:divider="@android:color/transparent" 
      android:dividerHeight="0dp" 
      android:background="#111"> 
    </ListView> 

</android.support.v4.widget.DrawerLayout> 
+0

"私は私の第二の活動、AboutActivityは、MapsActivityレイアウトのでframeLayoutに表示したいので、私はLayoutInflaterを使用していました。"どうして? –

+0

@EliasFazel FrameLayoutにマップが含まれていて、後で2回目のアクティビティが必要になりました。私はLayoutInflaterが私のマップから私の第二のアクティビティにプログラム的に変更する最良の方法であると思った。別のやり方をしたでしょうか? – jay

答えて

0

ButtonListViewを両方のアクティビティで使用する場合は、AboutActivityの代わりにAboutFragmentを使用してください。 ListViewのaboutButtonをクリックすると、FrameLayoutのフラグメントショーが置き換えられます。menuButtonをFrameLayoutの外側に置かなければなりません。

activity_xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <!-- The main content view --> 
    <LinearLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 
    <FrameLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_weight="1" 
     android:layout_height="0dp" /> 

    <Button 
      android:id="@+id/menuButton" 
      android:layout_width="100dp" 
      android:layout_height="50dp" 
      android:text="Menu" /> 
    </LinearLayout> 


     <!-- The navigation drawer --> 
    <ListView android:id="@+id/left_drawer" 
      android:layout_width="240dp" 
      android:layout_height="match_parent" 
      android:layout_gravity="start" 
      android:choiceMode="singleChoice" 
      android:divider="@android:color/transparent" 
      android:dividerHeight="0dp" 
      android:background="#111"> 
    </ListView> 

</android.support.v4.widget.DrawerLayout> 
0

public class AboutActivity FragmentActivityを展開し、フラグメントを使用しようとします。

"メイン" レイアウトで1を作成します。

<FrameLayout 
    android:id="@+id/content_frame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
    <Button 
     android:id="@+id/menuButton" 
     android:layout_width="100dp" 
     android:layout_height="50dp" 
     android:text="Menu" /> 
</FrameLayout> 

mainActivityにでframeLayoutに作成したフラグメントを膨らませます。

+0

FragmentActivityを拡張することは機能していないようです.MapsActivityのボタンとリストビューが私のAboutActivityにも必要であるからです。私はあなたのコードを理解しているかどうかはわかりませんが、XMLファイルにListViewを追加する必要もあります。 – jay

関連する問題