简体   繁体   English

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

[英]Android - Error when inflate Menu Item into Actionbar

I have the following problem, I need to inflate just one item (logout) to the ActionBar displayed in the LoginActivity . LoginActivity以下问题,我只需要向LoginActivity显示的ActionBar LoginActivity一项(注销)。 But it throws an "unexpected EOF!" 但这会引发"unexpected EOF!" . So I have tried different ways of inflating the item to showing it up in the action bar but still without good results, at the end I've put the error message. 因此,我尝试了多种放大项目的方式,以将其显示在操作栏中,但仍然没有良好的结果,最后,我输入了错误消息。

This is an image of example (what is showing) no item showed.. 这是示例图片(显示内容), 没有显示项目。

My code is the following: 我的代码如下:

app/java/package/MainActivity. 应用程序/爪哇/包/ 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: 应用程序/ RES /菜单/ 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 应用程序/ RES /布局/ 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>

Error Message: 错误信息:

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!

I think the error unexpected EOF has nothing to do with your code but with some configurations about the buffer sizes that may be of a fixed size. 我认为错误意外EOF与您的代码无关,但与缓冲区大小的某些配置可能是固定大小有关。 To solve that you can set the buffer sizes to off under Developer options settings. 要解决此问题,可以在“开发人员”选项设置下将缓冲区大小设置为“ ”。

If not enabled yet, you can enable the settings under About and click Build number until the see a toast confirming that developer options are enabled. 如果尚未启用,则可以启用“ 关于”下的设置,然后单击“ 内部版本号”,直到看到祝酒词确认开发人员选项已启用。

EDIT : And inside your toolbar XML node, add the id attribute with value toolbar as called in onCreate callback 编辑 :在您的工具栏XML节点内,添加具有值toolbar的id属性,如onCreate回调中所调用

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();

Since the view with toolbar as id could not be found, findViewById returns null and setSupportActionBar will get a null value that means no Toolbar to cast as ActionBar. 由于找不到带有toolbar ID的视图,因此findViewById返回null,setSupportActionBar将获得一个null值,这意味着没有工具栏可以转换为ActionBar。

Modify your toolbar XML and add the id attribute as stated below: 修改工具栏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">

Good luck 祝好运

In your onCreate() method you are trying to initialize toolbar with below code: 在您的onCreate()方法中,您尝试使用以下代码初始化工具栏:

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. 您尚未在XML文件中为工具栏视图指定id属性的地方。

Please specify the id for the toolbar in XML and then try to run it. 请使用XML指定工具栏的ID,然后尝试运行它。

I think you will resolve this issue by following the above solution. 我认为您可以按照上述解决方案解决此问题。

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

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