我在我的某个应用程序的活动中设置了一个工具栏,但每次尝试进入所述活动时它都会崩溃。也许我错过了什么?有人可以帮我解决这个问题吗?之前没有使用过工具栏,所以还是有点困惑。
以下是活动代码:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
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.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class ProjectCreateScreen extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondary_layout1);
Toolbar toolbar = (Toolbar) findViewById(R.id.AwesomeBar);
setSupportActionBar(toolbar);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
// Handle the menu item
return true;
}
});
toolbar.inflateMenu(R.menu.menu_main);
final TextView noProject = (TextView) findViewById(R.id.NOPROJECT);
Button btn = (Button) findViewById(R.id.addBtn);
final ArrayList<String> listItems=new ArrayList<String>();
final ListAdapter addAdapter = new ArrayAdapter<String>(this,
R.layout.list_item, R.id.listFrame, listItems);
final ListView lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(addAdapter);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
noProject.setVisibility(View.GONE);
lv.setVisibility(View.VISIBLE);
listItems.add("New Project");
((ArrayAdapter) addAdapter).notifyDataSetChanged();
}
});
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent switchToEdit = new Intent(ProjectCreateScreen.this,
teamCreateScreen.class);
startActivity(switchToEdit);
}
});
}
活动使用的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rl">
<android.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="60dp"
android:minHeight="60dp"
android:id="@+id/AwesomeBar"
android:background="@android:color/white">
</android.widget.Toolbar>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:id="@+id/addBtn"
android:layout_below="@+id/AwesomeBar"
android:background="@drawable/add_greyslate"/>
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="@string/noProjectsNotice"
android:id="@+id/NOPROJECT"
android:gravity="center"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textSize="16sp"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/addBtn"
android:id="@+id/lv"
android:visibility="invisible">
</ListView>
</RelativeLayout>
答案 0 :(得分:3)
在您的XML文件中替换:
<android.widget.Toolbar
使用:
<android.support.v7.widget.Toolbar
因为在您的代码中您指的是支持版本。