Android App - 按钮正在打开活动,但它正在崩溃

时间:2016-03-30 17:00:34

标签: java android android-button runtimeexception

我创建了一个滚动活动但是当我调试应用程序并单击按钮打开活动时,我收到一个错误。基本上,它崩溃了整个应用程序。但是,这是我的代码。

是因为它是滚动活动而不是空活动吗?

这是java文件:

news.java

package com.example.it5.foothillers;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

public class news extends AppCompatActivity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_news);
       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();
           }
       });
   }
}

main_acitivity.java

package com.example.it5.foothillers;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener
{

   Button button;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
           button = (Button)findViewById(R.id.button);
           button.setOnClickListener(this);

   }

   private void buttonClick() {
       startActivity(new Intent("it5.foothillers.news"));
   }

   @Override
   public void onClick(View v) {
       switch (v.getId()){
           case R.id.button:
               buttonClick();
               break;

       }

   }

}

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.it5.foothillers">

   <application
       android:allowBackup="true"
       android:icon="@mipmap/ic_launcher"
       android:label="@string/app_name"
       android:supportsRtl="true"
       android:theme="@style/AppTheme">

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

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

       <activity android:name=".news">
           <intent-filter>
               <action android:name="it5.foothillers.news" />
               <category android:name="android.intent.category.DEFAULT" />
           </intent-filter>
       </activity>

   </application>

</manifest>

日志:

03-30 12:01:49.871 2122-2122/com.example.it5.foothillers E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.it5.foothillers, PID: 2122
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.it5.foothillers/com.example.it5.foothillers.news}: 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:2325)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
   at android.app.ActivityThread.access$800(ActivityThread.java:151)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:135)
   at android.app.ActivityThread.main(ActivityThread.java:5254)
   at java.lang.reflect.Method.invoke(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:372)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
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:197)
   at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:129)
   at com.example.it5.foothillers.news.onCreate(news.java:17)
   at android.app.Activity.performCreate(Activity.java:5990)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
   at android.app.ActivityThread.access$800(ActivityThread.java:151) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:135) 
   at android.app.ActivityThread.main(ActivityThread.java:5254) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at java.lang.reflect.Method.invoke(Method.java:372) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

3 个答案:

答案 0 :(得分:0)

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

Mainifest.xml

<activity android:name="com.example.it5.foothillers.news" ></activity>

答案 1 :(得分:0)

  

此活动已经有一个由窗口装饰提供的操作栏。

您的异常消息清楚地告诉您,您已经有Toolbar作为活动主题的一部分。

如果要拨打styles.xml,您应该在setSupportActionBar文件中设置这样的主题。

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

你可能也可以删除这两行

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

并替换为

ActionBar toolbar = getSupportActionBar();

答案 2 :(得分:-1)

启动活动的最简单方法是将意图更改为:

startActivity(new Intent(MainActivity.this, news.class));

堆栈跟踪显示您正在尝试添加另一个ActionBar,并且您已经拥有了应用程序主题提供的ActionBar。

因此,您可以在清单上的活动声明中更改主题,以便不使用ActionBar,如下所示:

   <activity android:name=".news"
             android:theme="@style/AppTheme.NoActionBar">
       <intent-filter>
           <action android:name="it5.foothillers.news" />
           <category android:name="android.intent.category.DEFAULT" />
       </intent-filter>
   </activity>

或者您只需从活动代码和您的activity_news.xml中删除工具栏(因为您以后不再使用它):

protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_news);

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