安卓应用在添加工具栏后崩溃。

huangapple go评论77阅读模式
英文:

android app crashed after addding toolbar

问题

我有一个安卓应用,如果我尝试打开它,它会崩溃。这是在我添加了工具栏之后发生的。

这是我的MainActivity.java文件:

import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.appcompat.widget.Toolbar;

public class MainActivity extends AppCompatActivity {
    Toolbar toolbar;

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.navigation_menu, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
            case R.id.action_add_contact:
                Toast.makeText(MainActivity.this, "Add Contacts", Toast.LENGTH_LONG).show();
                return true;
            case R.id.action_about_us:
                Toast.makeText(MainActivity.this, "About Us", Toast.LENGTH_LONG).show();
                return true;
            case R.id.action_settings:
                Toast.makeText(MainActivity.this, "Settings", Toast.LENGTH_LONG).show();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

MainActivity的XML文件:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <androidx.appcompat.widget.Toolbar
            android:layout_width="409dp"
            android:layout_height="53dp"
            android:background="#104885"
            android:theme="?attr/actionBarTheme"
            android:minHeight="?attr/actionBarSize"   
            android:id="@+id/toolbar2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="2dp"
            app:layout_constraintTop_toTopOf="parent"
            android:visibility="visible"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            android:fadeScrollbars="true"
            app:menu="@menu/navigation_menu"
            app:subtitleTextColor="@color/design_default_color_background"
            app:title="test2"/>
</androidx.constraintlayout.widget.ConstraintLayout>

我使用的菜单XML文件:navigation_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu 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">
    <item
            android:id="@+id/action_more"
            android:icon="@drawable/ic_baseline_more_vert_24"
            android:title="Settings"
            app:showAsAction="always"
            tools:showIn="@layout/activity_main" android:enabled="true" android:checkable="false"
            android:menuCategory="container" android:onClick="onCreateOptionsMenu">
        <menu>
            <item
                    android:id="@+id/action_add_contact"
                    android:icon="@drawable/ic_person_add_black_24dp"
                    android:title="Add contact"
                    app:showAsAction="ifRoom" tools:showIn="@layout/activity_main"
                    android:onClick="onOptionsItemSelected"/>
            <item
                    android:id="@+id/action_settings"
                    android:icon="@drawable/ic_settings_black_24dp"
                    android:title="Settings"
                    app:showAsAction="ifRoom"
                    tools:showIn="@layout/activity_main" android:onClick="onOptionsItemSelected"/>
            <item
                    android:id="@+id/action_about_us"
                    android:icon="@drawable/ic_info_outline_black_24dp"
                    android:title="About us"
                    app:showAsAction="ifRoom"
                    tools:showIn="@layout/activity_main" android:onClick="onOptionsItemSelected"/>
        </menu>
    </item>
</menu>

AndroidManifest.xml

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

    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            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>
    </application>

</manifest>

你做错了什么?我尝试过删除和添加工具栏,但没有成功。此外,我尝试了https://stackoverflow.com/questions/43804715/app-with-toolbar-crashing中的解决方案。

英文:

So i have an android app which crashes if i try to open it.
this started happening after i added toolbar.

Here is my MainActivity.java

import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.appcompat.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar2);
setSupportActionBar(toolbar);
toolbar.setTitle(&quot;Title&quot;);
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.navigation_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.action_add_contact:
Toast.makeText(MainActivity.this,&quot;Add Contacts&quot;,Toast.LENGTH_LONG).show();
return true;
case R.id.action_about_us:
Toast.makeText(MainActivity.this,&quot;About Us&quot;,Toast.LENGTH_LONG).show();
return true;
case R.id.action_settings:
Toast.makeText(MainActivity.this,&quot;Settings&quot;,Toast.LENGTH_LONG).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}

main activity xml:activity_main.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.MainActivity&quot;&gt;
&lt;androidx.appcompat.widget.Toolbar
android:layout_width=&quot;409dp&quot;
android:layout_height=&quot;53dp&quot;
android:background=&quot;#104885&quot;
android:theme=&quot;?attr/actionBarTheme&quot;
android:minHeight=&quot;?attr/actionBarSize&quot;   
android:id=&quot;@+id/toolbar2&quot;
app:layout_constraintEnd_toEndOf=&quot;parent&quot;
app:layout_constraintStart_toStartOf=&quot;parent&quot;
android:layout_marginStart=&quot;2dp&quot;
app:layout_constraintTop_toTopOf=&quot;parent&quot;
android:visibility=&quot;visible&quot;
app:popupTheme=&quot;@style/ThemeOverlay.AppCompat.Light&quot;
android:fadeScrollbars=&quot;true&quot;
app:menu=&quot;@menu/navigation_menu&quot;
app:subtitleTextColor=&quot;@color/design_default_color_background&quot;
app:title=&quot;test2&quot;/&gt;

the menu xml i am using:navigation_menu.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;menu xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;&gt;
&lt;item
android:id=&quot;@+id/action_more&quot;
android:icon=&quot;@drawable/ic_baseline_more_vert_24&quot;
android:title=&quot;Settings&quot;
app:showAsAction=&quot;always&quot;
tools:showIn=&quot;@layout/activity_main&quot; android:enabled=&quot;true&quot; android:checkable=&quot;false&quot;
android:menuCategory=&quot;container&quot; android:onClick=&quot;onCreateOptionsMenu&quot;&gt;
&lt;menu&gt;
&lt;item
android:id=&quot;@+id/action_add_contact&quot;
android:icon=&quot;@drawable/ic_person_add_black_24dp&quot;
android:title=&quot;Add contact&quot;
app:showAsAction=&quot;ifRoom&quot; tools:showIn=&quot;@layout/activity_main&quot;
android:onClick=&quot;onOptionsItemSelected&quot;/&gt;
&lt;item
android:id=&quot;@+id/action_settings&quot;
android:icon=&quot;@drawable/ic_settings_black_24dp&quot;
android:title=&quot;Settings&quot;
app:showAsAction=&quot;ifRoom&quot;
tools:showIn=&quot;@layout/activity_main&quot; android:onClick=&quot;onOptionsItemSelected&quot;/&gt;
&lt;item
android:id=&quot;@+id/action_about_us&quot;
android:icon=&quot;@drawable/ic_info_outline_black_24dp&quot;
android:title=&quot;About us&quot;
app:showAsAction=&quot;ifRoom&quot;
tools:showIn=&quot;@layout/activity_main&quot; android:onClick=&quot;onOptionsItemSelected&quot;/&gt;
&lt;/menu&gt;
&lt;/item&gt;
&lt;/menu&gt;

AndroidManifest.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
package=&quot;com.testapp.test2&quot;&gt;
&lt;application
android:allowBackup=&quot;true&quot;
android:icon=&quot;@mipmap/ic_launcher&quot;
android:label=&quot;@string/app_name&quot;
android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
android:supportsRtl=&quot;true&quot;
android:theme=&quot;@style/AppTheme&quot;&gt;
&lt;activity android:name=&quot;.MainActivity&quot;&gt;
&lt;intent-filter&gt;
&lt;action android:name=&quot;android.intent.action.MAIN&quot;/&gt;
&lt;category android:name=&quot;android.intent.category.LAUNCHER&quot;/&gt;
&lt;/intent-filter&gt;
&lt;/activity&gt;
&lt;/application&gt;
&lt;/manifest&gt;

What am i doing wrong?
I tried the removing and adding the toolbar but that did not work out.
Also,i tried the solutions from https://stackoverflow.com/questions/43804715/app-with-toolbar-crashing.

答案1

得分: 0

你可以考虑使用android.support.v7.widget.Toolbar替代androidx.appcompat.widget.Toolbar,但需要查看日志和AndroidManifest.xml文件。

顺祝商祺!

英文:

You could use maybe android.support.v7.widget.Toolbar instead of androidx.appcompat.widget.Toolbar, but need to see the logs and the AndroidManifest.xml.

Kind regards!

答案2

得分: 0

将以下内容添加到您的styles.xml文件中:

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

将以下行添加到清单文件中,作为android:theme=" @style/AppTheme.NoActionBar",如下所示:

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

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

</manifest>
英文:

add this in your styles.xml

&lt;style name=&quot;AppTheme.NoActionBar&quot; &gt;
&lt;item name=&quot;windowActionBar&quot;&gt;false&lt;/item&gt;
&lt;item name=&quot;windowNoTitle&quot;&gt;true&lt;/item&gt;
&lt;/style&gt;

add this line in the manifest file android:theme=&quot;@style/AppTheme.NoActionBar&quot; as shown below

 &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
package=&quot;com.testapp.test2&quot;&gt;
&lt;application
android:allowBackup=&quot;true&quot;
android:icon=&quot;@mipmap/ic_launcher&quot;
android:label=&quot;@string/app_name&quot;
android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
android:supportsRtl=&quot;true&quot;
android:theme=&quot;@style/AppTheme&quot;&gt;
&lt;activity android:name=&quot;.MainActivity&quot;
android:theme=&quot;@style/AppTheme.NoActionBar&quot;&gt;
&lt;intent-filter&gt;
&lt;action android:name=&quot;android.intent.action.MAIN&quot;/&gt;
&lt;category android:name=&quot;android.intent.category.LAUNCHER&quot;/&gt;
&lt;/intent-filter&gt;`enter code here`
&lt;/activity&gt;
&lt;/application&gt;
&lt;/manifest&gt;

huangapple
  • 本文由 发表于 2020年9月16日 22:02:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63921758.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定