onclick时app崩溃

时间:2017-11-04 08:19:31

标签: java android android-intent

当我点击numbersIntent侦听器打开我的NumbersActivity类文件时,我的Android应用程序崩溃了。

我正在使用在NumbersActivity类文件上设置的arraylist名称“word”来使用while循环分配字符串列表。

NumbersActivity类文件外,其他类看起来很好。我无法弄清楚为什么,有人可以帮忙吗?

以下是java文件

package com.example.android.miwok;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import android.view.View.OnClickListener;

import static android.R.attr.onClick;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set the content of the activity to use the activity_main.xml layout file
        setContentView(R.layout.activity_main);

//    public void openNumbersList(View view){
//        Intent i = new Intent(this,NumbersActivity.class);
//        startActivity(i);
//    }

        //   Find the View that shows the numbers category
        TextView numbers = (TextView) findViewById(R.id.numbers);

        // Set a click listener on that View
        numbers.setOnClickListener(new OnClickListener() {
            // The code in this method will be executed when the numbers category is clicked on.
            @Override
            public void onClick(View view) {
                // Create a new intent to open the {@link NumbersActivity}
                Intent numbersIntent = new Intent(MainActivity.this, NumbersActivity.class);

                // Start the new activity
                startActivity(numbersIntent);
            }
        });

        //   Find the View that shows the family category
        TextView family = (TextView) findViewById(R.id.family);

        // Set a click listener on that View
        family.setOnClickListener(new OnClickListener() {
            // The code in this method will be executed when the family category is clicked on.
            @Override
            public void onClick(View view) {
                // Create a new intent to open the {@link FamilyActivity}
                Intent familyIntent = new Intent(MainActivity.this, FamilyActivity.class);

                // Start the new activity
                startActivity(familyIntent);
            }
        });

        // Find the View that shows the colors category
        TextView colors = (TextView) findViewById(R.id.colors);

        // Set a click listener on that View
        colors.setOnClickListener(new OnClickListener() {
            // The code in this method will be executed when the colors category is clicked on.
            @Override
            public void onClick(View view) {
                // Create a new intent to open the {@link ColorsActivity}
                Intent colorsIntent = new Intent(MainActivity.this, ColorsActivity.class);

                // Start the new activity
                startActivity(colorsIntent);
            }
        });

        // Find the View that shows the phrases category
        TextView phrases = (TextView) findViewById(R.id.phrases);

        // Set a click listener on that View
        phrases.setOnClickListener(new OnClickListener() {
            // The code in this method will be executed when the phrases category is clicked on.
            @Override
            public void onClick(View view) {
                // Create a new intent to open the {@link PhrasesActivity}
                Intent phrasesIntent = new Intent(MainActivity.this, PhrasesActivity.class);

                // Start the new activity
                startActivity(phrasesIntent);
            }
        });
    }    
}

以下是我的logcat结果:

11-01 23:12:59.226 23384-23384/? E/Zygote: v2
11-01 23:12:59.226 23384-23384/? I/libpersona: KNOX_SDCARD checking this for 10238
11-01 23:12:59.226 23384-23384/? I/libpersona: KNOX_SDCARD not a persona
11-01 23:12:59.226 23384-23384/? I/SELinux: Function: selinux_compare_spd_ram, index[1], SPD-policy is existed. and_ver=SEPF_SM-A700FD_5.0.2-1 ver=51
11-01 23:12:59.226 23384-23384/? W/SELinux: Function: selinux_compare_spd_ram, index[1], priority [2], priority version is VE=SEPF_SECMOBILE_6.0.1_0029
11-01 23:12:59.226 23384-23384/? W/SELinux: SELinux: seapp_context_lookup: seinfo=default, level=s0:c512,c768, pkgname=com.example.android.miwok 
11-01 23:12:59.226 23384-23384/? I/art: Late-enabling -Xcheck:jni
11-01 23:12:59.286 23384-23384/? D/TimaKeyStoreProvider: TimaSignature is unavailable
11-01 23:12:59.286 23384-23384/? D/ActivityThread: Added TimaKeyStore provider
11-01 23:12:59.456 23384-23384/com.example.android.miwok W/System: ClassLoader referenced unknown path: /data/app/com.example.android.miwok-1/lib/arm
11-01 23:12:59.856 23384-23384/com.example.android.miwok D/SecWifiDisplayUtil: Metadata value : none
11-01 23:12:59.856 23384-23384/com.example.android.miwok D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{cca8e96 I.E...... R.....ID 0,0-0,0}
11-01 23:12:59.866 23384-23399/com.example.android.miwok D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
11-01 23:12:59.946 23384-23399/com.example.android.miwok I/Adreno: QUALCOMM build                   : d842ad3, Ia10634f51b
                                                                   Build Date                       : 01/04/16
                                                                   OpenGL ES Shader Compiler Version: XE031.06.00.05
                                                                   Local Branch                     : 
                                                                   Remote Branch                    : quic/LA.BR.1.2.6_rb1.7
                                                                   Remote Branch                    : NONE
                                                                   Reconstruct Branch               : NOTHING
11-01 23:12:59.956 23384-23399/com.example.android.miwok I/OpenGLRenderer: Initialized EGL, version 1.4
11-01 23:12:59.986 23384-23384/com.example.android.miwok V/ActivityThread: updateVisibility : ActivityRecord{ee55121 token=android.os.BinderProxy@7ab99b1 {com.example.android.miwok/com.example.android.miwok.MainActivity}} show : true
11-01 23:13:00.006 23384-23384/com.example.android.miwok D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 72 - 0, 0) vi=Rect(0, 72 - 0, 0) or=1
11-01 23:13:00.026 23384-23399/com.example.android.miwok D/libGLESv1: DTS_GLAPI : DTS is not allowed for Package : com.example.android.miwok
11-01 23:13:00.076 23384-23384/com.example.android.miwok I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@7ab99b1 time:230587661
11-01 23:13:02.196 23384-23384/com.example.android.miwok D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 72 - 0, 0) vi=Rect(0, 72 - 0, 0) or=1
11-01 23:13:02.236 23384-23384/com.example.android.miwok I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@7ab99b1 time:230589825
11-01 23:13:03.176 23384-23384/com.example.android.miwok I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@7ab99b1 time:230590768
11-01 23:13:09.576 23384-23384/com.example.android.miwok D/ViewRootImpl: ViewPostImeInputStage processPointer 0
11-01 23:13:09.616 23384-23384/com.example.android.miwok D/ViewRootImpl: ViewPostImeInputStage processPointer 1
11-01 23:13:09.626 23384-23384/com.example.android.miwok I/Timeline: Timeline: Activity_launch_request id:com.example.android.miwok time:230597210

以下是我的NumbersActivity java文件:

package com.example.android.miwok;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;

public class NumbersActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_numbers);

        ArrayList<String> words =  new ArrayList<String>();
        words.add("One");
        words.add("Two");
        words.add("three");
        words.add("Four");
        words.add("Five");
        words.add("Six");
        words.add("Seven");
        words.add("Eight");
        words.add("Nine");
        words.add("Ten");

        LinearLayout rootView = (LinearLayout) findViewById(R.id.rootView);
        int index = 0;
            while (index < words.size());
        {

            TextView wordView = new TextView(this);
            wordView.setText(words.get(index));
            rootView.addView(wordView);
index++;

        }

    }

}

这是我的活动主xml文件

<LinearLayout
    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:background="@color/tan_background"
    android:orientation="vertical"
    tools:context="com.example.android.miwok.MainActivity">

    <TextView
        android:id="@+id/numbers"
        style="@style/CategoryStyle"
        android:background="@color/category_numbers"
        android:text="@string/category_numbers" />

    <TextView
        android:id="@+id/family"
        style="@style/CategoryStyle"
        android:background="@color/category_family"
        android:text="@string/category_family" />

    <TextView
        android:id="@+id/colors"
        style="@style/CategoryStyle"
        android:background="@color/category_colors"
        android:text="@string/category_colors" />

    <TextView
        android:id="@+id/phrases"
        style="@style/CategoryStyle"
        android:background="@color/category_phrases"
        android:text="@string/category_phrases" />

</LinearLayout>



here is my activity_numbers xml file
<LinearLayout 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"
    android:orientation="vertical"
    android:id="@+id/rootView"
    tools:context="com.example.android.miwok.NumbersActivity">

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

在您的NumbersActivity中,您有以下代码行:

while (index < words.size());

尝试删除运营商末尾的&#34;;&#34; ,应该是这样的:

while (index < words.size())

答案 1 :(得分:-1)

编辑答案

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View view = getLayoutInflater().inflate(R.layout.activity_numbers, null);
        setContentView(view);

        ArrayList<String> words =  new ArrayList<String>();
        words.add("One");
        words.add("Two");
        words.add("three");
        words.add("Four");
        words.add("Five");
        words.add("Six");
        words.add("Seven");
        words.add("Eight");
        words.add("Nine");
        words.add("Ten");

        int index = 0;
        while (index < words.size());{
            TextView wordView = new TextView(this);
            wordView.setText(words.get(index));
            view.addView(wordView);
            index++;
        }

    }