I have the following problem, I need to inflate just one item (logout) to the ActionBar
displayed in the LoginActivity
. But it throws an "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.
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>
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. 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
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.
Modify your toolbar XML and add the id attribute as stated below:
<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