我正在尝试构建导航抽屉活动,只要显示主要活动并崩溃应用程序,就会显示此错误。
xml代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="?attr/colorPrimaryDark"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/Button_sign_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_arrow_forward_black_24dp"
/>
</android.support.design.widget.CoordinatorLayout>
这是我的java代码:
package com.android.example.ithelpdesk;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
public DrawerLayout drawer;
public Toolbar supportActionBar;
private boolean mToolBarNavigationListenerIsRegistered = false;
ActionBarDrawerToggle toggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateTicketList();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this,
drawer, toolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close
);
drawer.addDrawerListener(toggle);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView)
findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ListView listView = (ListView) findViewById(R.id.lvSubcategory);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int
i, long l) {
Intent intent = new Intent(MainActivity.this, ComplaintDetail.class);
startActivity(intent);
}
});
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
/*private void signOut() {
mGoogleSignInClient.signOut()
.addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// ...
}
});
}*/
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void setSupportActionBar(Toolbar supportActionBar) {
this.supportActionBar = supportActionBar;
}
private void populateTicketList() {
ArrayList<Ticket> arrayOfTicket = Ticket.getTicket();
TicketAdapter adapter = new TicketAdapter(this, arrayOfTicket);
ListView listView = (ListView) findViewById(R.id.lvTicket);
listView.setAdapter(adapter);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_complaint) {
Intent intent = new Intent(MainActivity.this, TicketCategory.class);
startActivity(intent);
}
else if (id == R.id.nav_profile) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}// else if (id == R.id.nav_slideshow) {
// } else if (id == R.id.nav_manage) {
// }
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
来自日志猫的崩溃:
请帮助。我是初学者,所以我也很感激这个问题的解释是什么问题以及我将来如何避免它。
这是logcat:
java.lang.RuntimeException:无法启动活动ComponentInfo {com.android.example.ithelpdesk / com.android.example.ithelpdesk.MainActivity}:java.lang.ClassCastException:android.support.design.widget.CoordinatorLayout不能被强制转换为android.support.v7.widget.Toolbar 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2455) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2515) 在android.app.ActivityThread.access $ 1000(ActivityThread.java:154) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1379) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:157) 在android.app.ActivityThread.main(ActivityThread.java:5571) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:745) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635) 引起:java.lang.ClassCastException:android.support.design.widget.CoordinatorLayout无法强制转换为android.support.v7.widget.Toolbar 在com.android.example.ithelpdesk.MainActivity.onCreate(MainActivity.java:35) 在android.app.Activity.performCreate(Activity.java:6357) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2408) ... 9更多