E/AndroidRuntime: FATAL EXCEPTION: main [重复]

Posted

技术标签:

【中文标题】E/AndroidRuntime: FATAL EXCEPTION: main [重复]【英文标题】:E/AndroidRuntime﹕ FATAL EXCEPTION: main [duplicate] 【发布时间】:2017-02-18 03:29:04 【问题描述】:

在我测试我的应用时,我在 android-Studio-Consol 中收到以下错误:

 10-09 20:44:56.685 21573-21573/com.example.android.buyhatke E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.example.android.buyhatke, PID: 21573
                                                                              java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.android.buyhatke/com.example.android.buyhatke.HomeActivity: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                  at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                  at android.os.Looper.loop(Looper.java:148)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                               Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
                                                                                  at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:198)
                                                                                  at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:130)
                                                                                  at com.example.android.buyhatke.HomeActivity.onCreate(HomeActivity.java:36)
                                                                                  at android.app.Activity.performCreate(Activity.java:6237)
                                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                  at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                  at android.os.Looper.loop(Looper.java:148) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
10-09 20:49:57.180 26089-26089/com.example.android.buyhatke W/System: ClassLoader referenced unknown path: /data/app/com.example.android.buyhatke-3/lib/x86
10-09 20:49:58.795 26089-26089/com.example.android.buyhatke W/System: ClassLoader referenced unknown path: /data/app/com.example.android.buyhatke-3/lib/x86
10-09 20:49:58.905 26089-26089/com.example.android.buyhatke W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
10-09 20:49:59.023 26089-26089/com.example.android.buyhatke D/AndroidRuntime: Shutting down VM
10-09 20:49:59.023 26089-26089/com.example.android.buyhatke E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.example.android.buyhatke, PID: 26089
                                                                              java.lang.RuntimeException: Unable to start activity ComponentInfocom.example.android.buyhatke/com.example.android.buyhatke.HomeActivity: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                  at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                  at android.os.Looper.loop(Looper.java:148)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                               Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
                                                                                  at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:198)
                                                                                  at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:130)
                                                                                  at com.example.android.buyhatke.HomeActivity.onCreate(HomeActivity.java:36)
                                                                                  at android.app.Activity.performCreate(Activity.java:6237)
                                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                  at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                  at android.os.Looper.loop(Looper.java:148) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

我的代码是

  MainActivity.java



package com.example.android.buyhatke;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.appevents.AppEventsLogger;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;

import org.json.JSONObject;

public class MainActivity extends AppCompatActivity 

    CallbackManager callbackManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        //
            facebookSDKInitialize();

            LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
            loginButton.setReadPermissions("email");
            getLoginDetails(loginButton);
    

    /*
     Initialize the facebook sdk.
     And then callback manager will handle the login responses.<br />
   */
    protected void facebookSDKInitialize() 
               FacebookSdk.sdkInitialize(getApplicationContext());
               callbackManager = CallbackManager.Factory.create();
    


    /*
  Register a callback function with LoginButton to respond to the login result.
  On successful login,login result has new access token and  recently granted permissions.
 */
    protected void getLoginDetails(LoginButton login_button)
                // Callback registration<br />
        login_button.registerCallback(callbackManager, new FacebookCallback <LoginResult> ()

            @Override
            public void onSuccess(LoginResult login_result) 
                getUserInfo(login_result);
                            

            @Override
            public void onCancel() 
              // code for cancellation
            

            @Override
            public void onError(FacebookException error) 
              // code for error handling.
            
        );
    

    /*To get the facebook user's own profile information via  creating a new request.
      When the request is completed, a callback is called to handle the success condition. */

    protected void getUserInfo(LoginResult login_result)
               GraphRequest data_request = GraphRequest.newMeRequest(
                login_result.getAccessToken(),
                new GraphRequest.GraphJSONObjectCallback() 
                    @Override
                    public void onCompleted( JSONObject json_object, GraphResponse response) 
                        Intent intent = new Intent(MainActivity.this,HomeActivity.class);
                        intent.putExtra("jsondata",json_object.toString());
                                startActivity(intent);
                    
                );
                Bundle permission_param = new Bundle();
                permission_param.putString("fields", "id,name,email,picture.width(120).height(120)");
                data_request.setParameters(permission_param);
                data_request.executeAsync();
            


        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) 
                super.onActivityResult(requestCode, resultCode, data);
                callbackManager.onActivityResult(requestCode, resultCode, data);
                Log.e("data",data.toString());
        

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        // Inflate the menu; this adds items to the action bar if it is present.
        // getMenuInflater().inflate(R.menu.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.
        return super.onOptionsItemSelected(item);
    

    @Override
    protected void onResume() 
        super.onResume();
        // Logs 'install' and 'app activate' App Events.
        AppEventsLogger.activateApp(this);
    

    @Override
    protected void onPause() 
        super.onPause();
        // Logs 'app deactivate' App Event.
        AppEventsLogger.deactivateApp(this);
    

HomeActivity.java

  package com.example.android.buyhatke;

    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.NavigationView;
    import android.support.design.widget.Snackbar;
    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.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.ImageView;
    import android.widget.TextView;
    import android.widget.Toast;

    import com.squareup.picasso.Picasso;

    import org.json.JSONObject;

    public class HomeActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener 
           JSONObject response, profile_pic_data, profile_pic_url;
            TextView user_name, user_email;
            ImageView user_picture;
            NavigationView navigation_view;
        @Override
            protected void onCreate(Bundle savedInstanceState) 
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_home);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            getSupportActionBar().setTitle("Home Page");            // may produce null pointer exception
            Intent intent = getIntent();
            String jsondata = intent.getStringExtra("jsondata");
            setNavigationHeader();    // call setNavigationHeader Method.
            setUserProfile(jsondata);  // call setUserProfile Method

            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.openDrawer, R.string.closeDrawer);
                 try 
                     drawer.setDrawerListener(toggle);
                 catch(NullPointerException e)
                     
                         Context context = getApplicationContext();
                         CharSequence text = "Error";
                         int duration = Toast.LENGTH_SHORT;
                         Toast toast = Toast.makeText(context, text, duration);
                         toast.show();
                     

                     toggle.syncState();
                    navigation_view.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();
            
            
          /*
            Set Navigation header by using Layout Inflater.<br />
           */
           public void setNavigationHeader()
             navigation_view = (NavigationView) findViewById(R.id.nav_view);
             View header = LayoutInflater.from(this).inflate(R.layout.nav_header_home, null);
             navigation_view.addHeaderView(header);
             user_name = (TextView) header.findViewById(R.id.username);
             user_picture = (ImageView) header.findViewById(R.id.profile_pic);
             user_email = (TextView) header.findViewById(R.id.email);
            
             /*
              Set User Profile Information in Navigation Bar.<br />
             */
             public  void  setUserProfile(String jsondata)
            try 
            response = new JSONObject(jsondata);
            user_email.setText(response.get("email").toString());
            user_name.setText(response.get("name").toString());
            profile_pic_data = new JSONObject(response.get("picture").toString());
            profile_pic_url = new JSONObject(profile_pic_data.getString("data"));
                    Picasso.with(this).load(profile_pic_url.getString("url"))
            .into(user_picture);
             catch (Exception e)
            e.printStackTrace();
            
            
        @Override
            public boolean onCreateOptionsMenu(Menu menu) 
                    // Inflate the menu; this adds items to the action bar if it is present.<br />
                    //   getMenuInflater().inflate(R.menu.home, menu);<br />
            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) /** check if error*/
            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.inbox) 
            // Handle the camera action
             else if (id ==R.id.starred) 
            
            else if (id ==R.id.sent_mail) 

            
            else if (id ==R.id.drafts) 

            
            else if (id ==R.id.allmail ) 

            
           else if (id ==R.id.trash ) 

            
            else if (id ==R.id.spam ) 

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

    

谁能帮忙?

【问题讨论】:

引起:java.lang.IllegalStateException:此活动已经有一个由窗口装饰提供的操作栏。不要在主题中请求 Window.FEATURE_SUPPORT_ACTION_BAR 并将 windowActionBar 设置为 false 以使用工具栏。 检查异常 Do not request Window.FEATURE_SUPPORT_ACTION_BAR 需要清单以获得确切答案 【参考方案1】:

在您的 styles.xml 文件中,如果您使用 Toolbar,请将您的主题父级更改为 NoActionBar。

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

【讨论】:

以上是关于E/AndroidRuntime: FATAL EXCEPTION: main [重复]的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio 标准列表视图:E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1

java.lang.NoSuchMethodError E / AndroidRuntime:FATAL EXCEPTION:Thread-4进程:com.hikvision.open.app,PID

Binary XML file line #2: Error inflating

E/AndroidRuntime(3306): java.lang.RuntimeException: 无法启动活动 ComponentInfoc [重复]

Proguard Retrace 不适用于 E/AndroidRuntime(10237) 等堆栈跟踪运行时信息:

E/AndroidRuntime:致命异常:Android 的 AsyncTask #1 错误