有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java自定义操作栏未显示

我有一个自定义操作栏,它应该执行以下操作:

  • 显示操作栏
  • 显示用户名(来自firebase数据库)
  • 显示用户配置文件(来自firebase数据库)

我可以在其他活动中访问用户名和个人资料图片,但无法在操作栏上检索它。 我收到的错误表明可能是findview。r、 id与文本视图不匹配,但我进行了双重检查,确实如此

The error points to the following line: 
*"custom_action_bar_text_view_full_name.setText(userName);"*
When I commented this line out, the app no longer crashed, but the action bar still did not display.

如果有人对我如何解决有任何想法,请让我知道

Error Message

尝试调用虚拟方法“void 安卓”。小装置。文本视图。空对象引用上的setText(java.lang.CharSequence)”

Chat Activity
public class ChatActivity extends AppCompatActivity implements View.OnClickListener {

    Window window;
    private ImageView image_view_send, image_view_attachment;
    private ImageView image_view_profile_picture;
    private TextView custom_action_bar_text_view_full_name;
    private EditText edit_text_message;
    private DatabaseReference mRootRef;
    private FirebaseAuth firebaseAuth;
    private String currentUserId, chatUserId;

    private RecyclerView recyclerViewMessages;
    private SwipeRefreshLayout srlMessages;
    private MessagesAdapter messagesAdapter;
    private List<MessageModel> messagesList;

    private LinearLayout linearLayout_progress;




    private int currentPage=1;
    private static final int RECORD_PER_PAGE = 30; //this will show 30 records per page.

    private static final int REQUEST_CODE_PICK_IMAGE=101;
    private static final int REQUEST_CODE_CAPTURE_IMAGE=102;
    private static final int REQUEST_CODE_PICK_VIDEO=103;


    private DatabaseReference databaseReferenceMessages;
    private ChildEventListener childEventListener;

    private BottomSheetDialog bottomSheetDialog;

    private String userName, photoName;

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

       //Declaring our custom action bar.//
        ActionBar actionBar = getSupportActionBar();
        if(actionBar !=null) {
            actionBar.setTitle("");
            ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(R.layout.custom_action_bar_chat, null);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeButtonEnabled(true);

            actionBar.setElevation(0);
            actionBar.setCustomView(actionBarLayout);
            actionBar.setDisplayOptions(actionBar.getDisplayOptions()|ActionBar.DISPLAY_SHOW_CUSTOM);
            //Declaring our custom action bar.//

        }
        custom_action_bar_text_view_full_name = findViewById(R.id.custom_action_bar_chat_user_full_name); // from the custom action bar.
        image_view_profile_picture = findViewById(R.id.custom_action_bar_chat_profile_image); //from thee custom action bar//



        image_view_send = findViewById(R.id.image_view_send);
        image_view_attachment = findViewById(R.id.imageView_attachment);
        edit_text_message = findViewById(R.id.etMessage);


        linearLayout_progress = findViewById(R.id.llProgress);




        recyclerViewMessages = findViewById(R.id.recycler_view_Messages);
        srlMessages = findViewById(R.id.swipe_refresh_layout);
        messagesList = new ArrayList<>();
        messagesAdapter = new MessagesAdapter(this, messagesList);
        recyclerViewMessages.setLayoutManager(new LinearLayoutManager(this));
        recyclerViewMessages.setAdapter(messagesAdapter);




        image_view_send.setOnClickListener(this); //setting the onclick listner for the icon that lets the user send a message.
        image_view_attachment.setOnClickListener(this); //setting the onclick listener for the icon that lets the user attach something to their chat.
        firebaseAuth = FirebaseAuth.getInstance();
        mRootRef = FirebaseDatabase.getInstance().getReference();
        currentUserId = firebaseAuth.getCurrentUser().getUid();





        
        if (Build.VERSION.SDK_INT > 21) {
            window = this.getWindow();
            window.setStatusBarColor(this.getResources().getColor(R.color.white));
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//  set status text dark


        }
        






        //pulling in user information for chat//
        if (getIntent().hasExtra(Constants.USER_KEY))
        {
            chatUserId = getIntent().getStringExtra(Constants.USER_KEY);
            photoName = chatUserId + "jpg";

        }
        //pulling in user information for chat//


        
        if(getIntent().hasExtra(Constants.USER_FULL_NAME))
            userName = getIntent().getStringExtra(Constants.USER_FULL_NAME);

        if(getIntent().hasExtra(Constants.PROFILE_PHOTO_FILE_NAME))
            photoName = getIntent().getStringExtra(Constants.PROFILE_PHOTO_FILE_NAME);

        custom_action_bar_text_view_full_name.setText(userName);


        if(!TextUtils.isEmpty(photoName)) { // <-- this is just incase someone does not have a profile image added.
            StorageReference photoRef = FirebaseStorage.getInstance().getReference().child(Constants.IMAGES_FOLDER).child(photoName);

            photoRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri uri) {
                    Glide.with(ChatActivity.this)
                            .load(uri)
                            .placeholder(R.drawable.small_grey_circle)
                            .error(R.drawable.small_grey_circle)
                            .into(image_view_profile_picture);

                }
            });
        }
Chat Adapter
 holder.linearLayout_chat_list.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ChatActivity.class);
                intent.putExtra(Constants.USER_KEY, chatListModel.getUserId());
                intent.putExtra(Constants.USER_FULL_NAME, chatListModel.getUserName());
                intent.putExtra(Constants.PROFILE_PHOTO_FILE_NAME, chatListModel.getPhotoName());
                context.startActivity(intent);
            }
        });
Custom Action Bar xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:id="@+id/linear_layout_custom_action_bar"
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:background="#EFEFEF"
    安卓:gravity="center_vertical"
    安卓:orientation="horizontal">

    <de.hdodenhof.circleimageview.CircleImageView
        安卓:id="@+id/custom_action_bar_chat_profile_image"
        安卓:layout_width="50dp"
        安卓:layout_height="50dp"
        安卓:src="@drawable/small_grey_circle">

    </de.hdodenhof.circleimageview.CircleImageView>

    <LinearLayout
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓:orientation="vertical"
        安卓:layout_marginStart="8dp"
        安卓:layout_marginLeft="8dp">


    <TextView
        安卓:id="@+id/custom_action_bar_chat_user_full_name"
        安卓:layout_width="match_parent"
        安卓:layout_height="wrap_content"
        安卓:layout_marginStart="8dp"
        安卓:layout_marginLeft="8dp"
        安卓:singleLine="true"
        安卓:ellipsize="marquee"
        安卓:fadingEdge="horizontal"
        安卓:marqueeRepeatLimit="marquee_forever"
        安卓:scrollHorizontally="true"
        安卓:textColor="#4B16E7"
        安卓:textStyle="bold"
        安卓:textSize="20dp"
        安卓:fontFamily="@font/roboto_medium"
        tools:text="User Name">

    </TextView>
    </LinearLayout>
</LinearLayout>

        

共 (0) 个答案