应用程序崩溃没有任何错误或警告

时间:2017-05-04 15:04:47

标签: java android xml

我在一个名为ListView的活动中创建了一个简单的Android应用,其中包含2个按钮和一个MainActivityListView使用arrayAdapter填充arrayList。

启动应用程序时,我没有任何编译错误或其他错误,您可以在此留言板输出中看到:

Information:Gradle tasks [:app:assembleDebug]
Information:BUILD SUCCESSFUL
Information:Total time: 26.874 secs
Information:0 errors
Information:0 warnings
Information:See complete output in console

以下是控制台的完整输出:

Executing tasks: [:app:assembleDebug]

Configuration on demand is an incubating feature.
NDK is missing a "platforms" directory.
If you are using NDK, verify the ndk.dir is set to a valid NDK directory.  It is currently set to C:\Users\Bram\AppData\Local\Android\sdk\ndk-bundle.
If you are not using NDK, unset the NDK variable from ANDROID_NDK_HOME or local.properties to remove this warning.

Incremental java compilation is an incubating feature.

A larger heap for the Gradle daemon is recommended for running jack.

It currently has 1024 MB.
For faster builds, increase the maximum heap size for the Gradle daemon to at least 1536 MB.
To do this set org.gradle.jvmargs=-Xmx1536M in the project gradle.properties.
For more information see https://docs.gradle.org/current/userguide/build_environment.html


A larger heap for the Gradle daemon is recommended for running jack.

It currently has 1024 MB.
For faster builds, increase the maximum heap size for the Gradle daemon to at least 1536 MB.
To do this set org.gradle.jvmargs=-Xmx1536M in the project gradle.properties.
For more information see https://docs.gradle.org/current/userguide/build_environment.html


A larger heap for the Gradle daemon is recommended for running jack.

It currently has 1024 MB.
For faster builds, increase the maximum heap size for the Gradle daemon to at least 1536 MB.
To do this set org.gradle.jvmargs=-Xmx1536M in the project gradle.properties.
For more information see https://docs.gradle.org/current/userguide/build_environment.html

:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:compileDebugNdk UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAnimatedVectorDrawable2321Library
:app:prepareComAndroidSupportAppcompatV72321Library
:app:prepareComAndroidSupportDesign2321Library
:app:prepareComAndroidSupportRecyclerviewV72321Library
:app:prepareComAndroidSupportSupportV42321Library
:app:prepareComAndroidSupportSupportVectorDrawable2321Library
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:transformClassesWithPreJackPackagedLibrariesForDebug UP-TO-DATE
:app:transformClassesWithPreJackRuntimeLibrariesForDebug UP-TO-DATE
:app:processDebugJavaRes UP-TO-DATE
:app:transformResourcesWithMergeJavaResForDebug UP-TO-DATE
:app:transformJackWithJackForDebug
:app:compileDebugSources
:app:mergeDebugShaders UP-TO-DATE
:app:compileDebugShaders UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:mergeDebugJniLibFolders UP-TO-DATE
:app:transformNativeLibsWithMergeJniLibsForDebug UP-TO-DATE
:app:validateSigningDebug
:app:packageDebug
:app:assembleDebug

BUILD SUCCESSFUL

Total time: 26.874 secs

但是当我打开应用程序时,它会立即崩溃。我该怎么办?

以下是MainActivity.java的代码:

package com.mysystem.androidmysystem.mysystem;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ArrayList<String> listItems;

    ListView list;
    ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle iCicle) {
        super.onCreate(iCicle);
        //listItems=BookingList.getListAsString();      returns ArrayList of strings. this is what I normally use...
        listItems.add("foo");
        listItems.add("boo");
        listItems.add("goo");
        list = (ListView) findViewById(R.id.Bookings);
        Log.d("ITEMLIST","GOT LIST");
        for(String item: listItems)
            Log.d("ITEMLIST",item);
        setContentView(R.layout.activity_main);
        if(listItems!=null) {
            adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
            list.setAdapter(adapter);
        }

    }
}

这是我的activity_main.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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: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.mysystem.androidmysystem.mysystem.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/textView2"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add"
        android:id="@+id/Add"
        android:layout_below="@+id/textView2"
        android:layout_toRightOf="@+id/textView2"
        android:layout_toEndOf="@+id/textView2"
        android:layout_marginLeft="26dp"
        android:layout_marginStart="26dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Edit"
        android:id="@+id/Edit"
        android:layout_alignTop="@+id/Add"
        android:layout_alignRight="@+id/Bookings"
        android:layout_alignEnd="@+id/Bookings"
        android:layout_marginRight="24dp"
        android:layout_marginEnd="24dp" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Bookings"
        android:layout_below="@+id/Edit"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

我正在使用名为API 23 Armeabi-v7a portrait的AVD,它似乎正常运行。我该怎么做才能使应用程序有效?

2 个答案:

答案 0 :(得分:0)

您尚未在listItems方法中初始化arrayList onCreate。因此,在将元素添加到数组列表之前,请在onCreate方法中添加以下代码。

 listItems = new ArrayList<>();

并在初始化之后,您可以执行添加操作。

答案 1 :(得分:0)

@Override
    protected void onCreate(Bundle iCicle) {
        super.onCreate(iCicle);
        //listItems=BookingList.getListAsString();      returns ArrayList of strings. this is what I normally use...
        setContentView(R.layout.activity_main3);
        list = (ListView) findViewById(R.id.Bookings);
        listItems = new ArrayList<>();
        listItems.add("foo");
        listItems.add("boo");
        listItems.add("goo");

        Log.d("ITEMLIST", "GOT LIST");
        for (String item : listItems)
            Log.d("ITEMLIST", item);

        if (listItems != null) {
            adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
            list.setAdapter(adapter);
        }

    }

请使用此

更新您的onCreate