应用程序在模拟器中启动时崩溃

时间:2017-04-12 16:32:39

标签: java android crash

我是Android开发的新手,我正在尝试为我班上的项目构建一种列出应用程序类型的书。教授最喜欢的座右铭是“#34;无论你不知道谷歌它是什么"。当你半知道该做什么时,这并不坏,因为我们正在学习java以及android开发,因为java看起来有些陌生,所以没有用。

无论如何将我的应用程序基于联系人应用程序,这就是我的MainActivity的外观

//MainActivity.java
package com.iekproject.siegfried.libraryapp;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

public class MainActivity extends AppCompatActivity
   implements LibraryFragment.LibraryFragmentListener, DetailFragment.DetailFragmentListener,
    AddEditFragment.AddEditFragmentListener {

//key for storing a book's Uri in a Bundle passed to a fragment
public static final String BOOK_URI = "book_uri";

private LibraryFragment libraryFragment; //displays library aka book list

//displays LibraryFragment when MainActivity first loads
@Override
protected void onCreate(Bundle savedInstanceState) {

   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
   setSupportActionBar(toolbar);

    //if layout contains fragmentContainer, the phone layout is in use. Create and display
    //a LibraryFragment
    if (savedInstanceState == null && findViewById(R.id.fragmentContainer) != null) {
        //create LibraryFragment
        libraryFragment = new LibraryFragment();

        //add the fragment to the FrameLayout
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.fragmentContainer, libraryFragment);
        transaction.commit(); //displays LibraryFragment
    }
    else {
        libraryFragment =
                (LibraryFragment) getSupportFragmentManager().
                        findFragmentById(R.id.DetailFragment);
    }
}

public boolean isExternalStorageReadable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state) ||
            Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        return true;
    }
    return false;
}

//displays DetailFragment for selected book
@Override
public void onBookSelected(Uri bookUri) {
        getSupportFragmentManager().popBackStack();
        displayBook(bookUri, R.id.rightPaneContainer);
}

//displays AddEditFragment to add a new book. Possibly what I'll also have to change to make it
//scan/update the book list
@Override
public void onAddBook() {
        displayAddEditFragment(R.id.rightPaneContainer, null);
}

//displays a book
private void displayBook(Uri bookUri, int viewID) {
    DetailFragment detailFragment = new DetailFragment();

    //specify book's Uri as an argument to the DetailFragment
    Bundle arguments = new Bundle();
    arguments.putParcelable(BOOK_URI, bookUri);
    detailFragment.setArguments(arguments);

    //use a FragmentTransaction to display the DetailFragment
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(viewID, detailFragment);
    transaction.addToBackStack(null);
    transaction.commit(); //causes DetailFragment to display
}

//displays fragment for adding new or editing existing book
private void displayAddEditFragment(int viewID, Uri bookUri) {
    AddEditFragment addEditFragment = new AddEditFragment();

    //if editing existing book, provide bookUri as an argument
    if (bookUri != null) {
        Bundle arguments = new Bundle();
        arguments.putParcelable(BOOK_URI, bookUri);
        addEditFragment.setArguments(arguments);
    }

    //use a FragmentTransaction to display the AddEditFragment
    FragmentTransaction transaction =
            getSupportFragmentManager().beginTransaction();
    transaction.replace(viewID, addEditFragment);
    transaction.addToBackStack(null);
    transaction.commit(); //causes AddEditFragment to display
}

//return to book list when displayed book deleted
@Override
public void onBookDeleted() {
    //removes top of back stack
    getSupportFragmentManager().popBackStack();
    libraryFragment.updateLibrary(); //refresh book list
}

//displays the AddEditFragment to edit an existing book. Maybe it can be used as Move or sth
/*@Override
public void onEditBook(Uri bookUri) {
    displayAddEditFragment(R.id.rightPaneContainer, bookUri);
}*/

//update GUI after the new book or updated book saved
@Override
public void onAddEditCompleted(Uri bookUri) {
    //removes top of back stack
    getSupportFragmentManager().popBackStack();
    libraryFragment.updateLibrary(); //refresh book list

    if (findViewById(R.id.fragmentContainer) == null){ //tablet
        //removes top of back stack
        getSupportFragmentManager().popBackStack();

        //on tablet, displays the book that was just added or edited
        displayBook(bookUri, R.id.rightPaneContainer);
    }
  }
}

这是logcat

04-12 16:14:38.807 5796-5796/? I/art: Not late-enabling -Xcheck:jni (already on)
04-12 16:14:38.808 5796-5796/? W/art: Unexpected CPU variant for X86 using defaults: x86
04-12 16:14:38.926 5796-5796/com.iekproject.siegfried.libraryapp W/System: ClassLoader referenced unknown path: /data/app/com.iekproject.siegfried.libraryapp-1/lib/x86
04-12 16:14:38.945 5796-5796/com.iekproject.siegfried.libraryapp I/InstantRun: starting instant run server: is main process
04-12 16:14:38.991 5796-5796/com.iekproject.siegfried.libraryapp W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
04-12 16:14:39.119 5796-5796/com.iekproject.siegfried.libraryapp D/AndroidRuntime: Shutting down VM
04-12 16:14:39.119 5796-5796/com.iekproject.siegfried.libraryapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                   Process: com.iekproject.siegfried.libraryapp, PID: 5796
                                                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.iekproject.siegfried.libraryapp/com.iekproject.siegfried.libraryapp.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:2665)
                                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                                       at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                       at android.os.Looper.loop(Looper.java:154)
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                                    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.AppCompatDelegateImplV9.setSupportActionBar(AppCompatDelegateImplV9.java:207)
                                                                                       at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:130)
                                                                                       at com.iekproject.siegfried.libraryapp.MainActivity.onCreate(MainActivity.java:30)
                                                                                       at android.app.Activity.performCreate(Activity.java:6679)
                                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                                                       at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                       at android.os.Looper.loop(Looper.java:154) 
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

感谢任何帮助

1 个答案:

答案 0 :(得分:0)

您只需在设置工具栏后设置内容视图。你可以这样写:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setContentView(R.layout.activity_main);

应用程序崩溃是因为您首先设置视图然后设置工具栏。