我正在尝试从我的主要版本中启动新的Activity
。我每次执行它都会得到,
Unfortunately, My application has Stopped.
我一直在经历类似的问题,但仍然无法解决我的问题。
以下是Manifest
:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.usuario.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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".secondActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
爪哇:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
TextView texto;
EditText editText;
Button boton;
Button boton2;
private GoogleApiClient client;
@Override
public void onClick(View v) {
switch (v.getId()) {
case (R.id.boton):
String dato = editText.getText().toString();
Toast.makeText(getApplicationContext(), dato, Toast.LENGTH_SHORT).show();
texto.setText(dato);
break;
case (R.id.boton2):
Intent intent = new Intent(MainActivity.this, secondActivity.class);
startActivity(intent);
break;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
texto = (TextView)findViewById(R.id.texto);
editText = (EditText)findViewById(R.id.editText);
boton = (Button)findViewById(R.id.boton);
boton2 = (Button)findViewById(R.id.boton2);
boton.setOnClickListener(this);
boton2.setOnClickListener(this);
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app deep link URI is correct.
Uri.parse("android-app://com.example.usuario.myapplication/http/host/path")
);
AppIndex.AppIndexApi.start(client, viewAction);
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
Action viewAction = Action.newAction(
Action.TYPE_VIEW, // TODO: choose an action type.
"Main Page", // TODO: Define a title for the content shown.
// TODO: If you have web page content that matches this app activity's content,
// make sure this auto-generated web page URL is correct.
// Otherwise, set the URL to null.
Uri.parse("http://host/path"),
// TODO: Make sure this auto-generated app deep link URI is correct.
Uri.parse("android-app://com.example.usuario.myapplication/http/host/path")
);
AppIndex.AppIndexApi.end(client, viewAction);
client.disconnect();
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.usuario.myapplication.MainActivity">
<TextView
android:id="@+id/texto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="ingresar dato" />
<Button
android:id="@+id/boton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/boton" />
<Button
android:id="@+id/boton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="new activity" />
</LinearLayout>
以下是Logcat
:
01-24 14:27:42.042 8033-8033/com.example.usuario.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.usuario.myapplication, PID: 8033
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.usuario.myapplication/com.example.usuario.myapplication.secondActivity}: 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:99)
at com.example.usuario.myapplication.secondActivity.onCreate(secondActivity.java:17)
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)
第二项活动:
package com.example.usuario.myapplication;
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 secondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
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();
}
});
}
}
答案 0 :(得分:3)
根据logcat日志;实施SecondActivity
文件和 android:theme="@style/AppTheme.NoActionBar"
类之间存在冲突。
你有两个解决方案,但实现其中任何一个:
- 您可以删除完整的两行“工具栏”实施代码。
- 醇>
您可以在清单文件中进行如下更改:
lists
希望它会帮助你。
干杯!
答案 1 :(得分:0)
从Manifest
文件中删除此内容:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
答案 2 :(得分:0)
变化:
<activity
android:name=".secondActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
要:
<activity
android:name=".secondActivity"
android:launchMode="singleTask" />
答案 3 :(得分:0)
在AppTheme风格中使用,
Theme.AppCompat.Light.NoActionBar
而不是
Theme.AppCompat.Light.DarkActionBar
您正在设置一个包含操作栏的主题,然后您将工具栏设置为操作栏。这就是您收到此错误的原因。
使用没有操作栏的主题,它将解决问题。
答案 4 :(得分:0)
由于您已经拥有Toolbar
使用此功能(在您的Styles
中):
Theme.AppCompat.Light.NoActionBar
示例:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
而且,加上这两个:
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
根据:
此活动已有窗口装饰提供的操作栏。 不要请求
Window.FEATURE_SUPPORT_ACTION_BAR
并设置 主题中windowActionBar
为false,而不是使用Toolbar
。
也许你想在你的OnCreate
中尝试这个:
@Override
public void onClick(View v) {
switch (v.getId()) {
case (R.id.boton):
String dato = editText.getText().toString();
Toast.makeText(getApplicationContext(), dato, Toast.LENGTH_SHORT).show();
texto.setText(dato);
break;
case (R.id.boton2):
Intent intent = new Intent(MainActivity.this, secondActivity.class);
startActivity(intent);
break;
}
}
答案 5 :(得分:0)
The problem is in your Manifest file.
从此更改Manifest xml文件的以下部分;
enter code here
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".secondActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>'
到此;
<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=".secondActivity"
<!--Add the following line to each of your activities in the manifest-->
android:theme="@style/AppTheme.NoActionBar">
</activity>'