工具栏错误导致我的应用程序崩溃Android工作室

时间:2017-04-27 13:50:45

标签: java android sqlite

关于我的代码中的工具栏,我一直收到错误。如果我把代码拿出来它工作正常但它在我添加到数据库的单独行之前工作正常,所以我真的很困惑为什么这不起作用。我在底部添加了我的XML。

private static final String TAG = MainActivity.class.getSimpleName();

private ArrayList<Item>list = new ArrayList<Item>();
private ItemAdapter adapter;
private RecyclerView recyclerView;
private AlertDialog dialog;

@Override
***protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);***

    FloatingActionButton fab = (FloatingActionButton) 
    findViewById(R.id.fab);


    fetchRecords();

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            LayoutInflater inflater = getLayoutInflater();
            View mView1 = inflater.inflate(R.layout.activity_add,null);

            final EditText input_name = (EditText) 
            mView1.findViewById(R.id.Name);
            final EditText input_age = (EditText) 
            mView1.findViewById(R.id.Age);
            final EditText input_weight = (EditText) 
            mView1.findViewById(R.id.Weight);
            final EditText input_height = (EditText) 
            mView1.findViewById(R.id.Height);
            final EditText input_reach = (EditText) 
            mView1.findViewById(R.id.Reach);
            final Button btnSave = (Button) 
            mView1.findViewById(R.id.btnSave);

            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setView(mView1).setTitle("Add new Record")
                    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                            dialog.dismiss();
                        }
                    });

            btnSave.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    String name = input_name.getText().toString();
                    String age = input_age.getText().toString();
                    String weight = input_weight.getText().toString();
                    String height = input_height.getText().toString();
                    String reach = input_reach.getText().toString();

                    if (name.equals("") && age.equals("") && weight.equals("") && height.equals("")&& reach.equals("")){
                        Snackbar.make(view,"Field incomplete",Snackbar.LENGTH_SHORT).show();

                    }else {
                        Save(name,age,name,height,reach);
                        dialog.dismiss();
                        Snackbar.make(view,"Saving",Snackbar.LENGTH_SHORT).show();
                    }
                }
            });

            dialog = builder.create();
            dialog.show();


        }
    });
}

public void fetchRecords() {

    recyclerView = (RecyclerView) findViewById(R.id.recycler);
    adapter = new ItemAdapter(this,list);

    LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(adapter);

    list.clear();

    Functions functions = new Functions(MainActivity.this);

    ArrayList<Item>data = functions.getAllRecords();

    if (data.size()>0){
        for (int i = 0; i<data.size(); i++){

            int id = data.get(i).getId();
            String namel = data.get(i).getName();
            String agel = data.get(i).getAge();
            String weightl = data.get(i).getWeight();
            String heightl = data.get(i).getHeight();
            String reachl = data.get(i).getHeight();

            Item item = new Item();

            item.setId(id);
            item.setName(namel);
            item.setAge(agel);
            item.setWeight(weightl);
            item.setHeight(heightl);
            item.setReach(reachl);
            list.add(item);

        }adapter.notifyDataSetChanged();

    }else {
        Toast.makeText(MainActivity.this, "No Records found.", Toast.LENGTH_SHORT).show();
    }
}

private void Save(String name, String age, String weight, String height, String reach) {


    Functions functions = new Functions(MainActivity.this);
    Item item = new Item();

    item.setName(name);
    item.setAge(age);
    item.setWeight(weight);
    item.setHeight(height);
    item.setHeight(reach);
    functions.Insert(item);
    Toast.makeText(MainActivity.this, "Saved...", Toast.LENGTH_SHORT).show();
    fetchRecords();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

布局文件

<?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"
android:fitsSystemWindows="true"
tools:context="android.wit.dale.fighterstudio.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="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="10dp"
    android:layout_gravity="bottom|end"
    android:layout_marginBottom="65dp"
    android:src="@android:drawable/ic_input_add" />

</android.support.design.widget.CoordinatorLayout>

RUN TIME ERROR

2 个答案:

答案 0 :(得分:0)

在styles.xml中使用Theme.AppCompat.Light.NoActionBar

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
........
</style>

答案 1 :(得分:0)

使用

androidx.appcompat.widget.Toolbar 

代替

Toolbar 

在您的布局中。另外,请确保在您的样式中使用Theme.AppCompat.Light.NoActionBar