导航栏现在显示在其他活动中

时间:2016-04-20 03:15:03

标签: android xml

我有这样的错误日志,实际上我试着在其他xml中显示DrawableToggle,但问题是这样的,并且错误日志也显示在帖子中

我已经在acitivity main及其显示切换栏中显示了导航栏的一些活动

但是当我尝试在另一个xml中实现时,切换栏不显示

manifest.xml

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.felix.tgp" >
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_icon"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        android:name="com.felix.tgp.AppController">

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Calculator"
            android:label="Body Mass Index Calculator"
            android:theme="@style/AppTheme.NoActionBar"
            android:screenOrientation="portrait">
        </activity>
        <activity
            android:name="com.felix.tgp.MainList"
            android:label="Chest Workout"
            android:screenOrientation="portrait">
        </activity>
    </application>

</manifest>

style.xml

    <resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">#FF39A3EB</item>
        <item name="colorPrimaryDark">#FF185896</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

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:fitsSystemWindows="true" tools:openDrawer="start">

    <include layout="@layout/app_bar_main" android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView android:id="@+id/nav_view"
        android:layout_width="wrap_content" android:layout_height="match_parent"
        android:layout_gravity="start" android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" />

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

calculator.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:fitsSystemWindows="true" tools:openDrawer="start">

    <android.support.design.widget.NavigationView android:id="@+id/nav_view"
        android:layout_width="wrap_content" android:layout_height="match_parent"
        android:layout_gravity="start" android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center_horizontal">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/Title"
                android:layout_weight="0.75"
                android:layout_x="0dp"
                android:layout_y="22dp"
                android:text="BMI Calculator"
                android:textColor="#000000"
                android:textSize="50dp"
                android:textStyle="bold" />
            </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Height"
                android:id="@+id/TextView"
                android:textColor="#000000"
                android:textSize="30dp" />

            <EditText
                android:layout_width="158dp"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:id="@+id/height"
                android:hint="cm"
                android:textColor="#000000"
                android:textStyle="bold"
                android:textSize="30dp"
                android:textAlignment="center"
                android:textColorHint="#898484" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Weight"
                android:id="@+id/textView1"
                android:layout_x="77dp"
                android:layout_y="168dp"
                android:textColor="#000000"
                android:textSize="30dp" />

            <EditText
                android:layout_width="158dp"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:ems="10"
                android:id="@+id/weight"
                android:layout_x="183dp"
                android:layout_y="154dp"
                android:hint="Kg"
                android:textColor="#000000"
                android:textStyle="bold"
                android:textSize="30dp"
                android:textAlignment="center"
                android:textColorHint="#898484" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:layout_width="147dp"
                android:layout_height="68dp"
                android:text="Calculate"
                android:id="@+id/hitung"
                android:textSize="20dp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="158dp"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:ems="10"
                android:id="@+id/result"
                android:hint="Result"
                android:textColor="#000000"
                android:textStyle="bold"
                android:textSize="30dp"
                android:textAlignment="center"
                android:textColorHint="#898484" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:ems="10"
                android:id="@+id/finalresult"
                android:textColor="#000000"
                android:textSize="18dp"
                android:textColorHint="#898484"
                android:scrollbars = "vertical"/>
        </LinearLayout>

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

MainActivity.java

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

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

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_calculator) {
            // Handle the camera action
            Intent calculator = new Intent(MainActivity.this, Calculator.class);
            startActivity(calculator);
        } else if (id == R.id.nav_chest) {
            Intent List = new Intent(MainActivity.this, MainList.class);
            startActivity(List);
        } else if (id == R.id.nav_back) {

        } else if (id == R.id.nav_shoulder) {

        } else if (id == R.id.nav_arm) {

        } else if (id == R.id.nav_abdominal) {

        } else if (id == R.id.nav_leg) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

calculator.java

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Created by Felix on 3/2/2016.
 */
public class Calculator extends AppCompatActivity implements View.OnClickListener, NavigationView.OnNavigationItemSelectedListener{

    private Button hitung;
    private TextView result, finalresult;
    private EditText height,weight;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calculator);

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        hitung = (Button)findViewById(R.id.hitung);
        height = (EditText)findViewById(R.id.height);
        weight = (EditText)findViewById(R.id.weight);
        result = (TextView)findViewById(R.id.result);
        finalresult = (TextView)findViewById(R.id.finalresult);

        finalresult.setMovementMethod(new ScrollingMovementMethod());
        hitung.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String tinggi = height.getText().toString();
        String berat = weight.getText().toString();
        double hasilakhir = Double.parseDouble(berat)/((Double.parseDouble(tinggi)*0.01)*(Double.parseDouble(tinggi)*0.01));
        result.setText(String.format("%.2f", hasilakhir));
        String bmiindex =   "Keterangan BMI\n" +
                "BMI < 18.5 = Berat Badan Anda dibawah rata-rata (underweight)\n" +
                "BMI 18.5 - 24 = Berat Badan Anda normal\n" +
                "BMI 25 - 29 = Kelebihan Berat Badan (overweight)\n" +
                "BMI >30 = Anda Tergolong Obesitas";
        if(hasilakhir < 18.5 ){
            finalresult.setText("Berat Badan Anda dibawah rata-rata (underweight)\n"+"\n" +bmiindex);
        }
        else if(hasilakhir >=18.5 && hasilakhir <=24.99) {
            finalresult.setText("Berat Badan Anda normal\n"+"\n" +bmiindex);
        }
        else if(hasilakhir >=25 && hasilakhir <=29.99) {
            finalresult.setText("Kelebihan Berat Badan (overweight)\n"+"\n" +bmiindex);
        }
        else if(hasilakhir >=30){
            finalresult.setText("Anda Tergolong Obesitas\n"+"\n" +bmiindex);
        }
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

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

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_calculator) {
            // Handle the camera action
            Intent calculator = new Intent(Calculator.this, Calculator.class);
            startActivity(calculator);
        } else if (id == R.id.nav_chest) {

        } else if (id == R.id.nav_back) {

        } else if (id == R.id.nav_shoulder) {

        } else if (id == R.id.nav_arm) {

        } else if (id == R.id.nav_abdominal) {

        } else if (id == R.id.nav_leg) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

记录错误

     FATAL EXCEPTION: main
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime: Process: com.felix.tgp, PID: 2255
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.felix.tgp/com.felix.tgp.Calculator}: java.lang.NullPointerException
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136)
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:  Caused by: java.lang.NullPointerException
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:100)
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at android.support.v7.internal.widget.ToolbarWidgetWrapper.<init>(ToolbarWidgetWrapper.java:93)
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at android.support.v7.internal.app.ToolbarActionBar.<init>(ToolbarActionBar.java:78)
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:206)
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:99)
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at com.felix.tgp.Calculator.onCreate(Calculator.java:39)
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5231)
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:135) 
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136) 
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5017) 
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method) 
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515) 
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
04-20 12:37:30.940 2255-2255/com.felix.tgp E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method) 
04-20 12:37:32.180 2255-2255/? I/Process: Sending signal. PID: 2255 SIG: 9

2 个答案:

答案 0 :(得分:0)

请选择没有操作栏功能的主题,您的主题如下,

如果你有像android:theme="@style/Theme.AppCompat.Light.DarkActionBar"这样的主题 替换为android:theme="@style/Theme.AppCompat.Light.NoActionBar"

可能是你用来改变它。

答案 1 :(得分:0)

好的,请尝试以下操作,让我知道会发生什么。

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>