【问题标题】:error `void android.support.v7.widget.Toolbar.setTitleTextColor(int)` android studio错误`void android.support.v7.widget.Toolbar.setTitleTextColor(int)` android studio
【发布时间】:2018-07-28 07:39:29
【问题描述】:

我有两个片段被替换在一页上。 但是当运行应用程序时看到这个错误: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.Toolbar.setTitleTextColor(int)' on a null object reference

这是我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/background"
    android:layoutDirection="rtl">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </FrameLayout>
    </ScrollView>
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/layout_navigation_header"
        app:menu="@menu/drawer_view" />
</android.support.v4.widget.DrawerLayout>

这是我的完整 MainActivity,它具有 RFragment 替换方法:

import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.example.exampleOne.myapplication.Adapter.AdapterSpecial;
import com.example.exampleOne.myapplication.DataFake.DataFakeGenerator;
import com.example.exampleOne.myapplication.Fragments.HomeFragment;


public class MainActivity extends AppCompatActivity {

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

        //change fragments with Navigation View
        NavigationView navigationView = (NavigationView)findViewById(R.id.nav_view);
        RFragment(new HomeFragment());
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.nav_home:
                        break;
                    case R.id.nav_login:
                        break;

                }
                return true;
            }
        });




        RecyclerView recyclerView = findViewById(R.id.special_offers_re);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        recyclerView.setAdapter(new AdapterSpecial(this, DataFakeGenerator.SpecailProduct(this)));

        RecyclerView recyclerView1 = findViewById(R.id.special_offers_re1);
        recyclerView1.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        recyclerView1.setAdapter(new AdapterSpecial(this, DataFakeGenerator.SpecailProduct(this)));
    }

    public void setupToolbar() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        DrawerLayout drawerLayout = findViewById(R.id.drawer_layout);
        setSupportActionBar(toolbar);

        ActionBar actionBar = getSupportActionBar();
        toolbar.setTitleTextColor(ContextCompat.getColor(this, R.color.white));
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);

        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, 0, 0);
        actionBarDrawerToggle.syncState();


    }

    public void RFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fragment_container,fragment);
        fragmentTransaction.commit();

    }
}

【问题讨论】:

  • 显示完整的 mainActivity 部分代码

标签: java android xml android-fragments fragment


【解决方案1】:

发生这种情况是因为您在 activity_main.xml 文件中没有任何工具栏。

这行显示你想查找activity_main.xml中不存在的视图工具栏

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

所以你在下面的行中出现错误,因为工具栏不存在并且它为空

toolbar.setTitleTextColor(ContextCompat.getColor(this, R.color.white));

【讨论】:

  • الان توactivity_main.xml کد با ایدی 工具栏 رو به من نشون بده
  • بین تو ایکس ام الهی تولباری نساختی هیچ آیدی ای نیست به اسم
  • "@+id/toolbar" 在 activity_main.xml 中不存在
  • بله درسته .واقعا ممنون دارم .دارم کد ها رو به فرگمنت تولبار توش هست انتقال می دم.
  • حله امیدوارم موفق باشی
最近更新 更多