看板 AndroidDev 關於我們 聯絡資訊
小弟剛剛接觸android 問題可能有點簡單,還請見諒。 如題,我先新增了AS default可以選擇有negative drawer的activity作為MAIN之後 新增了另一個negative drawer在 新建的activity(一樣繼承AppCompatActivity) 接著也複製了跟Main相同的code(包含重建兩個main,bar兩個的layout) java裏頭的code也是直接複製過去,可是只要一跑就會crash,不知道是哪裡出了問題 希望能尋求版上大大協助orz,非常感謝。 附上我的CODE public class informationActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_info); getSupportActionBar().setTitle("Information"); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar2); setSupportActionBar(toolbar); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout2); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( informationActivity.this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.addDrawerListener(toggle); toggle.syncState(); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view2); navigationView.setNavigationItemSelectedListener(this); -- 每個人都走到了盡頭,也都成為盡頭,每個人都有很多機會鑿開盡頭後的海闊天空,只是不敢鑿,不想鑿,就這麼卡在盡頭裡 。而我 “我會鑿開他。” -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.248.66.225 ※ 文章網址: https://www.ptt.cc/bbs/AndroidDev/M.1532599473.A.CDF.html
Fordevoted: (如果沒有用negative drawer 的話是可以正常執行的07/26 18:08
SmoothieNoIc: 你要不要看一下logcat 07/26 18:47
zcbxvsdf: 通靈一下:你有在androidmanifest宣告你新建的activity07/26 18:53
zcbxvsdf: 嗎?07/26 18:53
zcbxvsdf: 你要先setSupportActionBar再get 07/26 18:56
zcbxvsdf: setContentView裡的layout忘記改新copy的?07/26 19:03
去看logcat之後發現是因為原本就已經有actionbar了,因此setSupportActionBar時發生錯誤(也因此那時候getSupportActionBar放前面才會可以work),解決方法為: 在style.xml中新增 windowsActionBar fslse及windowsNoTitle true後就可以正常work了 謝謝大大們的幫忙~ ※ 編輯: Fordevoted (223.139.171.16), 07/26/2018 21:27:19