英文:
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("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);
}
}
}
main activity 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"/>
the menu xml i am using: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>
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
<style name="AppTheme.NoActionBar" >
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
add this line in the manifest file android:theme="@style/AppTheme.NoActionBar"
as shown below
<?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>`enter code here`
</activity>
</application>
</manifest>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论