简体   繁体   English

工具栏错误让我的应用程序崩溃 | 安卓工作室

[英]Toolbar error keeps crashing my app | Android studio

I keep getting an error in regards to the toolbar I have in my code, If i take the code out it works fine but it was working fine before i added in a separate line for a database so i'm really confused to why this isn't working.关于我在代码中的工具栏,我不断收到错误消息,如果我取出代码,它工作正常,但在我为数据库添加单独的行之前它工作正常,所以我真的很困惑为什么会这样'工作。 I've added in my XML at the bottom also.我还在底部添加了我的 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;
}

Layout file布局文件

<?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运行时错误

In your styles.xml use Theme.AppCompat.Light.NoActionBar在你的 styles.xml 使用 Theme.AppCompat.Light.NoActionBar

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

use采用

androidx.appcompat.widget.Toolbar 

instead of代替

Toolbar 

in your layout.在你的布局中。 Also, make sure that in your styles you use Theme.AppCompat.Light.NoActionBar另外,请确保在您的样式中使用Theme.AppCompat.Light.NoActionBar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM