Intent不会启动活动(java.lang.RuntimeException:无法启动活动)

时间:2016-06-09 14:35:31

标签: java android xml android-intent android-activity

我正在尝试以意图开始活动,但活动无法启动。

这是我得到的错误:

06-09 10:32:16.031 28535-28535/com.fluffyclouds.satroots E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.fluffyclouds.satroots, PID: 28535
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fluffyclouds.satroots/com.fluffyclouds.satroots.AfterSessionActivity}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
     at android.app.ActivityThread.-wrap11(ActivityThread.java)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:148)
     at android.app.ActivityThread.main(ActivityThread.java:5422)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
     at java.util.ArrayList.set(ArrayList.java:481)
     at com.fluffyclouds.satroots.AfterSessionActivity.onCreate(AfterSessionActivity.java:81)
     at android.app.Activity.performCreate(Activity.java:6251)
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:148) 
     at android.app.ActivityThread.main(ActivityThread.java:5422) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

我发现了这样的多个帖子,但答案要么不适用于我,要么似乎不起作用。

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fluffyclouds.satroots">
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".StartActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".SessionActivity"
        android:label="@string/title_activity_session"
        android:theme="@style/AppTheme" />
    <activity
        android:name=".AfterSessionActivity"
        android:label="End Report"
        android:theme="@style/AppTheme">
    </activity>
</application>

以下是启动意图的初始活动的一部分:

    Intent intent=new Intent(SessionActivity.this, AfterSessionActivity.class);
    /*intent.putExtra("FILE_NAME", filename);*/
    startActivity(intent);

这是第二个应该由意图启动但从未启动的活动:

package com.fluffyclouds.satroots;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;

public class AfterSessionActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    int numberOfQuestions; //number of questions answered is the same as the number of questions that get asked.
                           //it gets set in the onCreate and an int is pulled from the integers xml file

    ArrayList < String > numberDateSet = new ArrayList < String > ();
    ArrayList < String > numberCorrectSet = new ArrayList < String > ();
    ArrayList < String > numberIncorrectSet = new ArrayList < String > ();
    ArrayList < String > numberAccuracySet = new ArrayList < String > ();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //sliding in animation
        overridePendingTransition(R.anim.slide_in, R.anim.slide_out);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_after_session);

        numberOfQuestions = getResources().getInteger(R.integer.numberOfQuestions);

        /*Bundle extras= getIntent().getExtras();
            if(extras!=null){
                filename=extras.getString("FILE_NAME");
            }*/

        numberDateSet.add("5/12/2015");
        numberCorrectSet.add("3");

        /*File progressReport=new File(getFilesDir()+"/"+"progress_file.txt");
            try {
                BufferedReader br=new BufferedReader(new FileReader(progressReport));

                String line;
                int i=0;
                while ((line=br.readLine())!=null) {//this will populate the date and numberCorrect ArrayLists instantiated earlier
                    if (i % 2 == 0) {
                        numberDateSet.add(line);
                        System.out.println("added line to numberDateSet");
                    } else {
                        numberCorrectSet.add(line);
                        System.out.println("added line to numberCorrectSet");
                    }
                    i++;
                }
                br.close();
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "Unable to retrieve previous data", Toast.LENGTH_SHORT);
            }*/

        //necessary to reverse the arraylists so they can be listed on cardviews in the appropriate order (most recent at the top)
        Collections.reverse(numberDateSet);
        Collections.reverse(numberCorrectSet);

        for (int i = 0; i < numberCorrectSet.size(); i++) {
            numberIncorrectSet.set(i, (numberOfQuestions - Integer.parseInt(numberCorrectSet.get(i))) + "");
            int accuracy = numberOfQuestions / (Integer.parseInt(numberCorrectSet.get(i)));
            numberAccuracySet.set(i, (accuracy * 100) + "");
        }

        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

        mRecyclerView.setHasFixedSize(true);

        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);

        mAdapter = new MyAdapter(numberDateSet, numberCorrectSet, numberIncorrectSet, numberAccuracySet);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent(AfterSessionActivity.this, StartActivity.class);
        startActivity(intent);
    }

}

这是第二个活动的xml:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="wrap_content"
  android:layout_height="match_parent"
  android:layout_gravity="center|top"
  android:columnCount="1"
  >

  <android.support.v7.widget.RecyclerView
      android:id="@+id/my_recycler_view"
      android:scrollbars="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>

</GridLayout>

提前致谢

1 个答案:

答案 0 :(得分:0)

Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
 .
 .
 .
 at com.fluffyclouds.satroots.AfterSessionActivity.onCreate(AfterSessionActivity.java:81)

我认为这一行是:

numberIncorrectSet.set(i, (numberOfQuestions - Integer.parseInt(numberCorrectSet.get(i))) + "");

该代码将失败,因为代码中此时numberIncorrectSet是一个空列表。