第二个活动

时间:2017-03-26 20:02:12

标签: java android android-actionbar

我有两个扩展AppCompatActivity的活动,我阅读了很多帖子并尝试了不同的东西,但似乎没有任何效果。我需要的是在第二个Activity上显示操作栏,当我没有包含方法ToolBarTitle(String textName)时我没有收到错误但是ActionA栏没有显示在SecondActivity上,已经尝试扩展活动和使用getActionBar();但同样的错误,任何帮助将不胜感激。

错误如下

java.lang.NullPointerException:尝试调用虚方法' void android.support.v7.app.ActionBar.setDisplayShowCustomEnabled(boolean)'在空对象引用上

引起:java.lang.NullPointerException:尝试调用虚方法' void android.support.v7.app.ActionBar.setDisplayShowCustomEnabled(boolean)'在空对象引用上

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
        public void onCreate(Bundle savedInstanceState){

            super.onCreate(savedInstanceState);  
            setContentView(R.layout.main_activity_layout); 

            ToolBarTitle("TITLE 1st");

}


     @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();



            if (id == R.id.food_tracker) //Food Tracker
            {


                Intent intent = new Intent(MainActivity.this, SecondActivity.class);

                finish();
                startActivity(intent);

                return true;
            }  

            return super.onOptionsItemSelected(item);

     }


 public void ToolBarTitle(String textName){


              ActionBar actionBar = getSupportActionBar();



              View viewActionBar = getLayoutInflater().inflate(R.layout.actionbar_titletext_layout, null);
              ActionBar.LayoutParams params = new ActionBar.LayoutParams(//Center the textview in the ActionBar !
                      ActionBar.LayoutParams.WRAP_CONTENT, 
                      ActionBar.LayoutParams.MATCH_PARENT, 
                      Gravity.CENTER);
              TextView textviewTitle = (TextView) viewActionBar.findViewById(R.id.actionbar_textview);
              textviewTitle.setText(textName);


              actionBar.setCustomView(viewActionBar, params);
              actionBar.setDisplayShowCustomEnabled(true);
              actionBar.setDisplayShowTitleEnabled(false);
              actionBar.setDisplayHomeAsUpEnabled(false); 
              actionBar.setIcon(R.color.colorRed);
              actionBar.setHomeButtonEnabled(false);

        }


}



public class SecondActivity extends AppCompatActivity {

    @Override
        public void onCreate(Bundle savedInstanceState){

            super.onCreate(savedInstanceState);  
            setContentView(R.layout.second_activity_layout); 

            ToolBarTitle("TITLE 2nd");

}




 public void ToolBarTitle(String textName){


              ActionBar actionBar = getSupportActionBar();



              View viewActionBar = getLayoutInflater().inflate(R.layout.actionbar_titletext_layout, null);
              ActionBar.LayoutParams params = new ActionBar.LayoutParams(//Center the textview in the ActionBar !
                      ActionBar.LayoutParams.WRAP_CONTENT, 
                      ActionBar.LayoutParams.MATCH_PARENT, 
                      Gravity.CENTER);
              TextView textviewTitle = (TextView) viewActionBar.findViewById(R.id.actionbar_textview);
              textviewTitle.setText(textName);


              actionBar.setCustomView(viewActionBar, params);
              actionBar.setDisplayShowCustomEnabled(true);
              actionBar.setDisplayShowTitleEnabled(false);
              actionBar.setDisplayHomeAsUpEnabled(false); 
              actionBar.setIcon(R.color.colorRed);
              actionBar.setHomeButtonEnabled(false);

    }

}

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.workingout"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="23" />




    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_app"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:theme="@style/MyMaterialTheme.Base"
         >


             <!-- SPLASH SCREEN -->
      <activity  
        android:noHistory="true"
        android:name="com.workingout.SplashScreen"

        >

      <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>   


        <!--  MAIN ACTIVITY                          -->      
        <activity
            android:name="com.workingout.MainActivity" 
            android:label="@string/app_name" 
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustPan"
            android:parentActivityName="com.workingout.MainActivity"
            >
                      <!-- To support the up functionality in an activity, you need to declare the activity's parent <- UP BUTTON -->
          <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.workingout.ParentActivity" />


            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
<!--                 <category android:name="android.intent.category.LAUNCHER" /> -->
                <category android:name="android.intent.category.default" /> 
            </intent-filter>

        </activity>


        <activity  android:name="com.workingout.SecondActivity" 
                   android:theme="@style/MyMaterialTheme.Base"
                   android:screenOrientation="landscape" >
        </activity> 




    </application>

</manifest>

styles.xml

<!-- Application base theme. -->
<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/colorActionBarBackground</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item> 




</style>  

//MAINACTIVITY LAYOUT
<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/drawerWidget"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >



        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <android.support.v7.widget.Toolbar
                xmlns:local="http://schemas.android.com/apk/res-auto"
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?attr/colorPrimary"
                android:minHeight="?attr/actionBarSize"
                local:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
    <!--              android:typeface="serif"-->

        </LinearLayout>

        <FrameLayout
            android:id="@+id/containerView"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>             
      </LinearLayout>


 <!--  Drawer background color and text items change here -->   
    <android.support.design.widget.NavigationView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/navigationView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:layout_marginTop="-24dp"
        android:background="@color/colorDrawerBackgroundBody"
        app:menu="@menu/drawer_menu"
        app:itemTextColor="@color/colorDrawerTextBody"      
        app:itemIconTint="@color/colorDrawerTextBody"> 
   <!-- app:itemBackground="@color/orange" -->

  <!-- Separator/Divider line drawer -->  
  <View
    android:layout_width="match_parent"
    android:layout_marginLeft="5dp"
    android:layout_marginStart="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginEnd="5dp"
    android:layout_height="0.3dp"
    android:layout_marginTop="436dp"
    android:background="@color/colorDrawerLineBody"/>   

  <!--  Drawer header --> 
  <include layout="@layout/drawer_layout_header" />


    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>


//SECONDACTIVITY LAYOUT
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_food_tracker"
    android:id="@+id/food_tracker_layout"
    android:orientation="vertical" >

   <TextView
       android:id="@+id/textViewDate"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentLeft="true"
       android:layout_alignParentStart="true"
       android:layout_below="@+id/adView"
       android:layout_marginLeft="60dp"
       android:layout_marginStart="60dp"
       android:layout_marginTop="1dp"
       android:text="date" />

   <TextView
       android:id="@+id/textViewDay"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignBaseline="@+id/textViewDate"
       android:layout_alignBottom="@+id/textViewDate"
       android:layout_alignParentRight="true"
       android:layout_alignParentEnd="true"
       android:layout_marginRight="82dp"
       android:layout_marginTop="1dp"
       android:layout_marginEnd="82dp"
       android:text="day" />



</RelativeLayout>

//ACTION BAR LAYOUT
 <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/actionbar_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:maxLines="1"
        android:clickable="false"
        android:focusable="false"
        android:longClickable="false"
        android:textStyle="bold"
        android:textSize="18sp"
        android:textColor="@color/colorActionBarTextTitle" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

第一个问题是,您在第一个活动中没有工具栏视图,而在第一个活动中没有。

尝试添加到第二个。