应用程序从一项活动转到另一项活动时崩溃

时间:2019-06-07 12:28:57

标签: android

我正在android studio中制作UI。当我的应用在活动之间切换时会出现问题。实际上,我将计时器从3秒的一项活动切换到另一项活动。当3秒的时间完成时,我的应用崩溃了,无法进入NavigationDrawer活动(这是项目的一部分)。 它显示了logcat中的错误 (navigationdrawer.java.18) 变成setSupportActionBar(toolbar); Java文件中的错误

setSupportActionBar cannot be applied to android.support.v7.widget.Toolbar 我试图在其他站点上找到解决方案,但找不到任何解决方案。

Navigationdrawer.java

package com.example.zeeshan.myapplication;
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.os.Bundle;
import android.support.v7.widget.Toolbar;
    public class Navigationdrawer extends AppCompatActivity {
    private DrawerLayout drawer;
    @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigationdrawer);
        Toolbar toolbar=findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        drawer=findViewById(R.id.drawer_layout);

        ActionBarDrawerToggle toggle=new ActionBarDrawerToggle(this,drawer,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

    }

    @Override
    public void onBackPressed() {
        if(drawer.isDrawerOpen(GravityCompat.START)){
            drawer.closeDrawer(GravityCompat.START);
        }
        else {
            super.onBackPressed();
        }
    }
}

Navigationdrawer.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:context=".Navigationdrawer"
    tools:openDrawer="start">

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

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

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

    <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/nav_header"
        app:menu="@menu/drawer_menu" />

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

Home.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@drawable/gradient"
tools:context=".MainActivity">
 <TextView
   android:layout_width="match_parent"
   android:layout_height="190dp"
   android:text="Welcome to Wheat yield Estimation App..."
   android:textColor="@color/white"
   android:textSize="35dp"
   android:layout_marginTop="240dp"
   android:layout_marginLeft="40dp"
   android:layout_marginRight="40dp"
   android:textStyle="italic"/>
</RelativeLayout>

Home.java

package com.example.zeeshan.myapplication;
 import android.content.Intent;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;
 import java.util.Timer;
 import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
   Timer timer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
      timer=new Timer();
      timer.schedule(new TimerTask() {
          @Override
          public void run() {
            Intent intent=new Intent(MainActivity.this,navigationdrawer.class);
            startActivity(intent);

          }
      },3000);
        }

}

style.xml

    <resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Base.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>
    </style>
<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>


</style>
</resources>

我期望该应用会在活动之间切换,但不会切换并崩溃..

2 个答案:

答案 0 :(得分:1)

您很有可能只是在AndroidManifest:中遗漏了以下内容

<activity
android:name=".Navigationdrawer"
android:label="@string/app_name" />

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
<activity
        android:name=".Navigationdrawer"
        android:label="@string/app_name"
       />

答案 1 :(得分:0)

在MainActivity计时器中,您具有以下代码:

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

您将转到此活动(登录),而不是实际要转到的NavigationDrawer活动。将上面的代码更改为:

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

当然,除非您具有“登录”活动,然后从该活动中最终进入NavigationDrawer活动,在这种情况下,请同时包含该活动的代码和xml。