我的问题是:当我尝试在仿真器中运行该应用程序时,我的应用程序崩溃了,我真的不知道问题出在哪里...下面是我的MainActivity
类{{1} },androidManifest.xml
和logcat崩溃日志。
MainActivity类:
activity_main.xml
activity_main.xml:
package com.example.notepadapp;
import android.content.Intent;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ArrayList<Aantekening> aantekeningen;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Stelt de nieuwe toolbar in
Toolbar toolbar = findViewById(R.id.custom_toolbar);
setSupportActionBar(toolbar);
// Verkrijg de aantekeningen van de database en stel de ListView adapter in
final DatabaseHandler db = new DatabaseHandler(getApplicationContext());
aantekeningen = db.getAllNotes();
ImageButton newEntry = findViewById(R.id.add);
ListView mylist = findViewById(R.id.notepad_listview);
final NotepadAdapter notepadAdapter = new NotepadAdapter(this, aantekeningen);
mylist.setAdapter(notepadAdapter);
// Stelt de onClick listener methode in welke de NoteActivity activiteit start
newEntry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent addNoteActivity = new Intent(MainActivity.this, NoteActivity.class);
addNoteActivity.putExtra("Optie", "toevoegen");
startActivity(addNoteActivity);
}
});
//onItemClick (KORT) in ListView start een NoteActivity
mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
sendEditIntent(position);
}
});
// Stelt de lange onClick listener om verwijdering toe te staan
//TODO Maak onClickLongListener methode af...
mylist.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
// Inflate view
PopupMenu popup = new PopupMenu(MainActivity.this, view);
popup.getMenuInflater().inflate(R.menu.note_select_menu, popup.getMenu());
popup.setGravity(Gravity.END);
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
// Verkrijg geselcteerde titel
switch (menuItem.getItemId()) {
case R.id.menuEdit:
sendEditIntent(position);
break;
case R.id.menuDelete:
db.deleteNote(aantekeningen.get(position).getId());
aantekeningen.remove(position);
notepadAdapter.notifyDataSetChanged();
break;
default:
break;
}
return false;
}
});
popup.show();
return true;
}
});
}
/**
* @param position positie van item dat is aangeklikt.
* updateNoteActivity wordt gestart.
*/
private void sendEditIntent(int position) {
Intent updateNoteActivity = new Intent(MainActivity.this, NoteActivity.class);
updateNoteActivity.putExtra("optie", "bijwerken");
updateNoteActivity.putExtra("id", aantekeningen.get(position).getId());
updateNoteActivity.putExtra("naam", aantekeningen.get(position).getNaam());
updateNoteActivity.putExtra("aantekening", aantekeningen.get(position).getAantekening());
startActivity(updateNoteActivity);
}
}
androidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!--Aangepaste toolbar-->
<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/custom_toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:theme="@style/customToolbar">
<!-- Tekst zit in Strings.xml bestand in res map -->
<!-- Knop om een nieuw taak (ListView Item) toe te voegen -->
<ImageButton
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginEnd="15dp"
android:src="@drawable/ic_add"
android:background="@drawable/button_background"
android:contentDescription="@string/add_note" />
</android.support.v7.widget.Toolbar>
<!-- ListView welke is toegevoegd m.b.v. onClick methode -->
<ListView
android:id="@+id/notepad_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Logcat错误日志:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.notepadapp">
<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=".NoteActivity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NoteActivity"/>
</application>
</manifest>
什么是导致这些错误的问题?
我的2019-02-12 12:58:00.572 4104-4104/? I/Zygote: seccomp disabled by setenforce 0
2019-02-12 12:58:00.573 4104-4104/? I/mple.notepadap: Late-enabling -Xcheck:jni
2019-02-12 12:58:00.588 4104-4104/? W/mple.notepadap: Unexpected CPU variant for X86 using defaults: x86
2019-02-12 12:58:01.035 4104-4104/com.example.notepadapp W/mple.notepadap: JIT profile information will not be recorded: profile file does not exits.
2019-02-12 12:58:01.068 4104-4104/com.example.notepadapp I/chatty: uid=10070(com.example.notepadapp) identical 10 lines
2019-02-12 12:58:01.068 4104-4104/com.example.notepadapp W/mple.notepadap: JIT profile information will not be recorded: profile file does not exits.
2019-02-12 12:58:01.118 4104-4104/com.example.notepadapp I/InstantRun: starting instant run server: is main process
2019-02-12 12:58:01.160 4104-4121/com.example.notepadapp D/libEGL: Emulator has host GPU support, qemu.gles is set to 1.
2019-02-12 12:58:01.180 4104-4104/com.example.notepadapp I/com.example.notepadapp: type=1400 audit(0.0:928): avc: denied { write } for comm=45474C20496E6974 name="property_service" dev="tmpfs" ino=8284 scontext=u:r:untrusted_app:s0:c70,c256,c512,c768 tcontext=u:object_r:property_socket:s0 tclass=sock_file permissive=1
2019-02-12 12:58:01.192 4104-4121/com.example.notepadapp D/vndksupport: Loading /vendor/lib/egl/libGLES_emulation.so from current namespace instead of sphal namespace.
2019-02-12 12:58:01.193 4104-4121/com.example.notepadapp E/libEGL: load_driver(/vendor/lib/egl/libGLES_emulation.so): dlopen failed: library "/vendor/lib/egl/libGLES_emulation.so" not found
2019-02-12 12:58:01.193 4104-4121/com.example.notepadapp D/vndksupport: Loading /vendor/lib/egl/libEGL_emulation.so from current namespace instead of sphal namespace.
2019-02-12 12:58:01.180 4104-4104/com.example.notepadapp I/com.example.notepadapp: type=1400 audit(0.0:929): avc: denied { connectto } for comm=45474C20496E6974 path="/dev/socket/property_service" scontext=u:r:untrusted_app:s0:c70,c256,c512,c768 tcontext=u:r:init:s0 tclass=unix_stream_socket permissive=1
2019-02-12 12:58:01.418 4104-4121/com.example.notepadapp D/libEGL: loaded /vendor/lib/egl/libEGL_emulation.so
2019-02-12 12:58:01.422 4104-4121/com.example.notepadapp D/vndksupport: Loading /vendor/lib/egl/libGLESv1_CM_emulation.so from current namespace instead of sphal namespace.
2019-02-12 12:58:01.423 4104-4121/com.example.notepadapp D/libEGL: loaded /vendor/lib/egl/libGLESv1_CM_emulation.so
2019-02-12 12:58:01.432 4104-4121/com.example.notepadapp D/vndksupport: Loading /vendor/lib/egl/libGLESv2_emulation.so from current namespace instead of sphal namespace.
2019-02-12 12:58:01.436 4104-4121/com.example.notepadapp D/libEGL: loaded /vendor/lib/egl/libGLESv2_emulation.so
2019-02-12 12:58:01.495 4104-4115/com.example.notepadapp I/mple.notepadap: Background concurrent copying GC freed 6767(3MB) AllocSpace objects, 0(0B) LOS objects, 53% free, 1313KB/2MB, paused 282us total 101.684ms
2019-02-12 12:58:01.496 4104-4104/com.example.notepadapp W/mple.notepadap: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
2019-02-12 12:58:01.497 4104-4104/com.example.notepadapp W/mple.notepadap: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
2019-02-12 12:58:01.567 4104-4104/com.example.notepadapp D/AndroidRuntime: Shutting down VM
--------- beginning of crash
2019-02-12 12:58:01.571 4104-4104/com.example.notepadapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.notepadapp, PID: 4104
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.notepadapp/com.example.notepadapp.MainActivity}: 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:2914)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6680)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
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.AppCompatDelegateImpl.setSupportActionBar(AppCompatDelegateImpl.java:345)
at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:130)
at com.example.notepadapp.MainActivity.onCreate(MainActivity.java:34)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2894)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6680)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-02-12 12:58:01.579 4104-4104/com.example.notepadapp I/Process: Sending signal. PID: 4104 SIG: 9
用荷兰语写成。抱歉,如果有问题
答案 0 :(得分:0)
您的styles.xml文件具有名为AppTheme的标签。 您选择的父主题提供一个操作栏。
如果要改为使用工具栏,则需要使用NoActionBar主题。这是个好主意。
答案 1 :(得分:0)
请仔细阅读该异常。该错误是由您的样式主题引起的。
java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.notepadapp / com.example.notepadapp.MainActivity}:java.lang.IllegalStateException:此活动已经具有窗口装饰提供的操作栏。请勿请求Window.FEATURE_SUPPORT_ACTION_BAR并将主题中的windowActionBar设置为false来代替使用工具栏。
这意味着您必须使用NoActionBar主题。只需转到您的styles.xml文件并更改Apptheme。