Android-将菜单项添加到操作栏中时出错

时间:2018-12-02 23:53:03

标签: android android-studio android-actionbar android-menu android-inflate

我遇到以下问题,我只需要向ActionBar中显示的LoginActivity充值一项(注销)。但是它抛出一个"unexpected EOF!"。因此,我尝试了各种方法使项目膨胀以显示在操作栏中,但仍然没有良好的结果,最后我输入了错误消息。

这是示例图片(显示的内容) no item showed..

我的代码如下:

app / java / package / MainActivity。

    public class MainActivity extends AppCompatActivity {


    CircleImageView profile_image;
    TextView username;

    FirebaseUser firebaseUser;
    DatabaseReference reference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar();

        profile_image = findViewById(R.id.profile_image);
        username = (TextView) findViewById(R.id.userId);

        firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
        reference = FirebaseDatabase.getInstance().getReference("Users").child(firebaseUser.getUid());

        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                User2 user = dataSnapshot.getValue(User2.class);
                System.out.println(user);
                Log.d("Userz",user.getUsername());
                username.setText(user.getUsername().toString());
                if(user.getImageURL().equals("default")){
                    profile_image.setImageResource(R.mipmap.ic_launcher);
                } else {
                    Glide.with(MainActivity.this).load(user.getImageURL()).into(profile_image);
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        //getMenuInflater().inflate(R.menu.menu,menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        Log.d("Itemid",item.toString());
        switch(item.getItemId()){
            case R.id.logoutItem:
                FirebaseAuth.getInstance().signOut();
                startActivity(new Intent(MainActivity.this, StartActivity.class));
                finish();
                return true;
        }

        return false;
    }
}

app / res / menu / menu.xml:

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/logoutItem"
        android:title="Logout"
        app:showAsAction="always"

        />
</menu>

app / res / layout / activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimaryDark"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/MenuStyle">


            <de.hdodenhof.circleimageview.CircleImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:id="@+id/profile_image"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/userId"
                android:text="username"
                android:textColor="#fff"
                android:layout_marginLeft="25dp"
                android:layout_marginStart="25dp"
                android:textStyle="bold"/>
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

</LinearLayout>

错误消息:

at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at android.support.v7.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:631)
        at android.support.v7.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:518)
        at android.support.v7.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:403)
        at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:191)
        at com.affinity.affinityteam.firebase_chat_try2.MainActivity.onCreateView(MainActivity.java:40)
        at android.app.Activity.onCreateView(Activity.java:6184)
        at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:389)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at android.support.v7.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:631)
        at android.support.v7.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:518)
        at android.support.v7.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:403)
        at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:191)
        at com.affinity.affinityteam.firebase_chat_try2.MainActivity.onCreateView(MainActivity.java:40)
        at android.app.Activity.onCreateView(Activity.java:6184)
        at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:389)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at android.support.v7.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:631)
        at android.support.v7.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:518)
        at android.support.v7.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:403)
        at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:191)
        at com.affinity.affinityteam.firebase_chat_try2.MainActivity.onCreateView(MainActivity.java:40)
        at android.app.Activity.onCreateView(Activity.java:6184)
        at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:389)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at android.support.v7.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:631)
        at android.support.v7.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:518)
        at android.support.v7.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:403)
        at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:191)
        at com.affinity.affinityteam.firebase_chat_try2.MainActivity.onCreateView(MainActivity.java:40)
        at android.app.Activity.onCreateView(Activity.java:6184)
        at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:389)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)

    read: unexpected EOF!

2 个答案:

答案 0 :(得分:0)

我认为错误意外的EOF 与您的代码无关,但与缓冲区大小的某些配置可能是固定大小有关。要解决此问题,可以在“开发人员”选项设置下将缓冲区大小设置为关闭

如果尚未启用,则可以在关于下启用设置,然后单击内部版本号,直到看到祝酒词确认开发人员选项已启用。

编辑: 在工具栏XML节点内,添加toolbar回调

中调用的值为onCreate的id属性
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);

//not sure that toolbar is not null since in xml there is no id toolbar
        setSupportActionBar(toolbar);
        getSupportActionBar();

由于找不到ID为toolbar的视图,因此findViewById返回null,setSupportActionBar将获得一个null值,这意味着没有工具栏可以转换为ActionBar。

修改工具栏XML并添加id属性,如下所述:

<android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimaryDark"
            android:id="@+id/toolbar"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/MenuStyle">

祝你好运

答案 1 :(得分:0)

In your onCreate() method you are trying to initialize toolbar with below code:

Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar); setSupportActionBar(toolbar);

Where you have not specified the id attribute for the toolbar view in your XML file.

Please specify the id for the toolbar in XML and then try to run it.

I think you will resolve this issue by following the above solution.