前提・実現したいこと
AndroidStudioでナビゲーションドロワーを使ってアプリを作っている初心者です。
ナビゲーションドロワーのメニューをタップするとそれぞれ違う画面に切り替わってほしいのですがどうやって実装すればいいのかわかりません…
いろんなサイトやほかの方の質問等も見ていろいろやってみましたが、エラーが出たり、途中で分からなくなってしまいます
初歩的な質問で申し訳ないのですが、躓いてしまって先に進めないので教えていただきたいと思います。
しょっぱなからわからないので最初に生成されるコードから変えたのは一部のレイアウトだけです…
発生している問題・エラーメッセージ
エラーメッセージ
該当のソースコード
Java
1import android.os.Bundle; 2import android.support.design.widget.FloatingActionButton; 3import android.support.design.widget.Snackbar; 4import android.view.View; 5import android.support.design.widget.NavigationView; 6import android.support.v4.view.GravityCompat; 7import android.support.v4.widget.DrawerLayout; 8import android.support.v7.app.ActionBarDrawerToggle; 9import android.support.v7.app.AppCompatActivity; 10import android.support.v7.widget.Toolbar; 11import android.view.Menu; 12import android.view.MenuItem; 13import android.widget.SeekBar; 14import android.widget.TextView; 15import java.util.Locale; 16 17public class MainActivity extends AppCompatActivity 18 implements NavigationView.OnNavigationItemSelectedListener { 19 20 21 private TextView numberView1; 22 private TextView numberView2; 23 private TextView numberView3; 24 private TextView numberView4; 25 private TextView numberView5; 26 private TextView numberView6; 27 28 @Override 29 protected void onCreate(Bundle savedInstanceState) { 30 super.onCreate(savedInstanceState); 31 setContentView(R.layout.activity_main); 32 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 33 setSupportActionBar(toolbar); 34 35 numberView1 = findViewById(R.id.numberView1); 36 37 SeekBar seekBar1 = findViewById(R.id.seekBar1); 38 39 seekBar1.setProgress(0); 40 seekBar1.setMax(100); 41 42 seekBar1.setOnSeekBarChangeListener( 43 new SeekBar.OnSeekBarChangeListener() { 44 @Override 45 public void onProgressChanged( 46 SeekBar seekBar1, int progress, boolean fromUser) { 47 String str = String.format(Locale.US, "%d %%",progress); 48 numberView1.setText(str); 49 } 50 @Override 51 public void onStartTrackingTouch(SeekBar seekBar1) { 52 } 53 public void onStopTrackingTouch(SeekBar seekBar1) { 54 55 } 56 } 57 ); 58 59 /*長すぎたので省略しますが、6個同じようなことが書いてあります 60 これは後からボタンを押したときにSeekBarとかNumberViewとかをセットで増やすようにする予定だからです 61*/ 62 63 FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 64 fab.setOnClickListener(new View.OnClickListener() { 65 @Override 66 public void onClick(View view) { 67 Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 68 .setAction("Action", null).show(); 69 } 70 }); 71 72 DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 73 ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( 74 this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 75 drawer.addDrawerListener(toggle); 76 toggle.syncState(); 77 78 NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 79 navigationView.setNavigationItemSelectedListener(this); 80 } 81 82 @Override 83 public void onBackPressed() { 84 DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 85 if (drawer.isDrawerOpen(GravityCompat.START)) { 86 drawer.closeDrawer(GravityCompat.START); 87 } else { 88 super.onBackPressed(); 89 } 90 } 91 92 @Override 93 public boolean onCreateOptionsMenu(Menu menu) { 94 // Inflate the menu; this adds items to the action bar if it is present. 95 getMenuInflater().inflate(R.menu.main, menu); 96 return true; 97 } 98 99 @Override 100 public boolean onOptionsItemSelected(MenuItem item) { 101 102 int id = item.getItemId(); 103 104 //noinspection SimplifiableIfStatement 105 if (id == R.id.action_settings) { 106 return true; 107 } 108 109 return super.onOptionsItemSelected(item); 110 } 111 112 @SuppressWarnings("StatementWithEmptyBody") 113 @Override 114 public boolean onNavigationItemSelected(MenuItem item) { 115 // Handle navigation view item clicks here. 116 int id = item.getItemId(); 117 118 DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 119 drawer.closeDrawer(GravityCompat.START); 120 return true; 121 122 } 123}
試したこと
いろんな方のブログやサイト、質問と回答など見てやってみましたがよくわからないままです。
補足情報(FW/ツールのバージョンなど)
ソースコードに載せたのはJavaのMainActivityです
変えたのはほとんどレイアウトとかだけです
Content_main.xmlはこちらになります
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/app_bar_main">
<ScrollView android:id="@+id/ScrollView" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:id="@+id/LinearLayout" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginLeft="16dp" android:layout_marginTop="5dp" android:layout_marginEnd="0dp" android:layout_marginRight="0dp" android:text="元気!" android:textSize="24sp" app:layout_constraintBottom_toTopOf="@+id/seekBar2" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/numberView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="150dp" android:layout_marginLeft="120dp" android:layout_marginTop="0dp" android:layout_marginEnd="0dp" android:layout_marginRight="0dp" android:layout_marginBottom="0dp" android:text="" android:textSize="18sp" app:layout_constraintBottom_toTopOf="@+id/seekBar1" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.491" app:layout_constraintStart_toEndOf="@+id/textView1" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.544" /> <SeekBar android:id="@+id/seekBar1" android:layout_width="347dp" android:layout_height="44dp" android:layout_marginStart="18dp" android:layout_marginLeft="18dp" android:layout_marginTop="0dp" android:layout_marginEnd="0dp" android:layout_marginRight="0dp" app:layout_constraintBottom_toTopOf="@+id/textView1" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView1" /> </LinearLayout>
<!--これも同じように6個ぐらいあります。-->
</LinearLayout> </ScrollView>
</android.support.constraint.ConstraintLayout>
あなたの回答
tips
プレビュー