- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在单击上一个 Activity 的 View 列表中的一个项目。此 Activity 应该获取该对象并显示该对象的特征。当我单击 viewList 时,应用程序崩溃。这是日志猫:
04-16 19:37:09.183 22696-22696/cs4326.cook4me E/AndroidRuntime: FATAL EXCEPTION: main
Process: cs4326.cook4me, PID: 22696
java.lang.RuntimeException: Unable to start activity ComponentInfo{cs4326.cook4me/cs4326.cook4me.RecipeInstructActivity}: java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
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.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.add(ArrayList.java:457)
at cs4326.cook4me.RecipeInstructActivity.onCreate(RecipeInstructActivity.java:45)
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)
这是相应的 Activity 代码(错误显示在第 45 行):
package cs4326.cook4me;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
public class RecipeInstructActivity extends FragmentActivity {
private static final String TAG = "RecipeInstructActivity";
/**
* Identifier for the example fragment.
*/
public static final int FRAGMENT_COMPLETE = 1;
public static final int FRAGMENT_STEPPED = 2;
/**
* The adapter definition of the fragments.
*/
private FragmentPagerAdapter _fragmentPagerAdapter;
/**
* The ViewPager that hosts the section contents.
*/
private ViewPager _viewPager;
/**
* List of fragments.
*/
private List<Fragment> _fragments = new ArrayList<>();
@Override
protected void onCreate(final Bundle savedInstanceState) {
Log.d(TAG, "onCreate:");
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
// Each fragment to our list.
this._fragments.add(FRAGMENT_COMPLETE, new CompleteInstructionFragment());
this._fragments.add(FRAGMENT_STEPPED, new SteppedInstructionFragment());
// Setup the fragments, defining the number of fragments, the screens and titles.
this._fragmentPagerAdapter = new FragmentPagerAdapter(this.getSupportFragmentManager()){
@Override
public int getCount() {
return RecipeInstructActivity.this._fragments.size();
}
@Override
public Fragment getItem(final int position) {
return RecipeInstructActivity.this._fragments.get(position);
}
@Override
public CharSequence getPageTitle(final int position) {
// Define titles for each fragment.
switch (position) {
case FRAGMENT_COMPLETE:
return "Complete Recipe";
case FRAGMENT_STEPPED:
return "Step-by-Step";
default:
return null;
}
}
};
this._viewPager = (ViewPager) this.findViewById(R.id.pager);
this._viewPager.setAdapter(this._fragmentPagerAdapter);
// Set the default fragment.
this.openFragment(FRAGMENT_COMPLETE);
}
/**
* Open the specified fragment.
* @param fragment
*/
public void openFragment(final int fragment) {
this._viewPager.setCurrentItem(fragment);
}
/**
* Get the fragment object for the specified fragment.
* @param fragment
* @return
*/
public Fragment getFragment(final int fragment) {
return this._fragments.get(fragment);
}
}
这是父 viewList Activity 的代码:
package cs4326.cook4me;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import java.util.Comparator;
import java.util.TreeSet;
public class RecipesActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{
private static final String TAG = "RecipesActivity";
private DatabaseReference databaseReference;
private ListView mainListView ;
private ArrayAdapter<String> listAdapter ;
private TreeSet<Recipe> allRecipeData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipes);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Initialize reference to database
databaseReference = FirebaseDatabase.getInstance().getReference();
//Initialize recipe data set
allRecipeData = new TreeSet<Recipe>(new Comparator<Recipe>() {
@Override
public int compare(Recipe o1, Recipe o2) {
return o1.getTitle().compareToIgnoreCase(o2.getTitle());
}
});
// Create ArrayAdapter
listAdapter = new ArrayAdapter<String>(this, R.layout.row_layout);
//Create query for recipes
Query recipesReference = databaseReference.child("recipes").orderByChild("title");
recipesReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
// Get Recipe object and use the values to update the UI
Recipe recipe = dataSnapshot.getValue(Recipe.class);
listAdapter.add(recipe.getTitle());
allRecipeData.add(recipe);
Log.d(TAG, "loadRecipe:onChildAdded");
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String previousName) {
// Get Recipe object and use the values to update the UI
Recipe recipe = dataSnapshot.getValue(Recipe.class);
listAdapter.remove(previousName);
listAdapter.add(recipe.getTitle());
Log.d(TAG, "loadRecipe:onChildChanged");
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
// Get Recipe object and use the values to update the UI
Recipe recipe = dataSnapshot.getValue(Recipe.class);
listAdapter.remove(recipe.getTitle());
allRecipeData.remove(recipe);
Log.d(TAG, "loadRecipe:onRemoved");
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String olden) {
// Get Recipe object and use the values to update the UI
Recipe recipe = dataSnapshot.getValue(Recipe.class);
listAdapter.remove(olden);
listAdapter.add(recipe.getTitle());
Log.d(TAG, "loadRecipe:onChildMoved");
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Recipe failed, log a message
Log.w(TAG, "loadRecipe:onCancelled", databaseError.toException());
}
});
// Find the ListView resource.
mainListView = (ListView) findViewById( R.id.listViewRecipes );
//Add list items!
mainListView.setAdapter( listAdapter );
//TODO: Make it so clicking / tapping on each item redirects to Recipe
mainListView.setOnItemClickListener(this);
//Floating login button
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent transfer = new Intent(RecipesActivity.this, LoginActivity.class);
startActivity(transfer);
}
});
//If you type, it will automatically start searching
setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
//Floating search button
FloatingActionButton floatingSearch = (FloatingActionButton) findViewById(R.id.search_recipes);
floatingSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onSearchRequested();
}
});
//Redirects back to Main Menu if press up button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public void onItemClick(AdapterView<?> l, View v, int position, long id) {
Log.d(TAG, "You clicked " + listAdapter.getItem(position));
String nameOf = listAdapter.getItem(position);
Recipe selectedRecipe = new Recipe();
// Then you start a new Activity via Intent
Intent specialTransfer = new Intent(RecipesActivity.this, RecipeInstructActivity.class);
for (Recipe r : allRecipeData) {
if (r.getTitle().equals(nameOf)) {
selectedRecipe = r;
}
}
//Pass selected recipe
specialTransfer.putExtra("recipe_object", selectedRecipe);
startActivity(specialTransfer);
}
}
这是 AndroidManifest.XML:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cs4326.cook4me">
<uses-permission android:name="android.permission.INTERNET" />
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light">
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="cs4326.cook4me.MainActivity" />
</activity>
<activity
android:name=".RegisterActivity"
android:label="Register User"
android:parentActivityName=".LoginActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="cs4326.cook4me.LoginActivity" />
</activity>
<activity
android:name=".ProfileActivity"
android:label="User profile"
android:parentActivityName=".LoginActivity" />
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="cs4326.cook4me.MainActivity" />
</activity>
<!--
<activity
android:name=".RecipeFragment"
android:label="@string/title_activity_recipe_fragment"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".ProfileFragment"
android:label="@string/title_activity_profile_fragment"
android:theme="@style/AppTheme.NoActionBar"></activity>
-->
<activity
android:name=".RecipesActivity"
android:label="@string/title_activity_recipes"
android:parentActivityName=".MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="cs4326.cook4me.MainActivity" />
<meta-data android:name="android.app.default_searchable"
android:value=".SearchRecipeActivity" />
</activity>
<activity
android:name=".CookingTerminologyActivity"
android:label="@string/title_activity_cooking_terminology"
android:parentActivityName=".MainActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="cs4326.cook4me.MainActivity" />
</activity>
<activity
android:name=".TermActivity"
android:label="@string/title_activity_term"
android:parentActivityName=".CookingTerminologyActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="cs4326.cook4me.CookingTerminologyActivity" />
</activity>
<activity
android:name=".SearchRecipeActivity"
android:label="@string/title_activity_search_recipe"
android:parentActivityName=".RecipesActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="cs4326.cook4me.RecipesActivity" />
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
<activity
android:name=".RecipeInstructActivity"
android:parentActivityName=".RecipesActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="cs4326.cook4me.RecipeInstructActivity" />
</activity>
</application>
</manifest>
最佳答案
问题 1:静态初始化
private List<Fragment> _fragments = new ArrayList<>(); //be careful doing this!
字段初始化在 Android Activity 和 fragment 中效果不佳,因为它们的生命周期是受控制的。虽然在这种情况下它不会导致问题,但如果您在字段初始化中使用 findViewById(int id) 或其他此类方法,则可能会遇到麻烦。在 Activity 和 Fragment 的生命周期真正清晰之前,最好避免将代码放入 onCreate()
或 onResume()
回调中(根据需要)。
问题 2:滚动我们自己的 FragmentManager
在此代码中, fragment 被缓存在列表中。没有必要这样做 - 这是 FragmentManager
缓存 fragment 的工作。
此外,方法 FragmentPagerAdapter#getItem()
应该用于实例化,而不是从列表中检索。这并不是你的错——我认为这个方法的命名很糟糕。它的真正含义更像是“createItem()”,它应该看起来更像这样:
this._fragmentPagerAdapter = new FragmentPagerAdapter(this.getSupportFragmentManager()){
@Override
public int getCount() {
return RecipeInstructActivity.this._fragments.size();
}
@Override
public Fragment getItem(final int position) {
switch (position) {
case FRAGMENT_COMPLETE:
return new CompleteInstructionFragment();
case FRAGMENT_STEPPED:
return new StepByStepFramgment();
}
如果可能的话,最好将所有内容留给 ViewPager。如果您不能并且需要自己操作 fragment ,那么当您添加 fragment 时,请使用重载,该重载允许您在完成事务时向 fragment 添加标签,然后当您需要 fragment 的句柄时,您可以使用 findFragmentByTag
来检索它。
关于java - IndexOutOfBoundsException 导致 Android 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43443726/
这个问题已经有答案了: What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? (25 个回答)
((fn [coll] (letfn [(add-item [acc coll idx] (conj acc (nth coll idx))
我不是 Java 初学者,但也不是专家,这就是我发布此文章以寻求帮助/解释的原因。我在互联网上查找了很多地方,但没有找到我正在寻找的答案。 public class Driver { public s
我不断得到 线程“main”中出现异常 java.lang.ArrayIndexOutOfBoundsException:MergeSorter.merge(MergeSorter.java:44)、
我做了一个小射击游戏..它工作正常但我也想实现如果火相交它们会消失。我有两个播放器子弹列表和计算机子弹列表......但是如果我有更多来自计算机或反向的子弹。这是我的循环 for (int
我试图从 JOptionPane 读取像 3+9-2*10/5 这样的数学表达式并得到它的结果——当然要考虑到操作顺序。我使用 String.split() 将字符串拆分为数字和操作数,并创建了一个寻
我在Grails中有以下代码: public LibraryItem createLibraryItemWithValues(ProjectItem projectItem) { def li
这个问题已经有答案了: What is a debugger and how can it help me diagnose problems? (3 个回答) 已关闭 7 年前。 我有一组以下类型的
请看下面的代码 DatabaseHandler.java public List getDetails(String name) { List details = new Ar
我应该让蛇形矩形沿着屏幕的一侧移动。然而它保持在原位,当我运行它时,我还收到 IndexOutOfBoundsException 错误。我相信这与我的 Render.java 文件中的 for 循环有
ArrayList beds = new ArrayList(49); public Patient getPatient(int bedNumber) { if (beds.get(bedN
我有 10 张卡片的数组。在“回合”的过程中,我向数组添加一张卡片,然后从数组中删除一张卡片(并不总是相同的)。该代码在前 6-7 回合中效果很好,然后抛出 IndexOutofBoundsExcep
我正在尝试创建一个评分系统并获得最佳分数,但我的方法生成了 IndexOutOfBoundsException,但我找不到超出数组列表范围的内容,有人可以帮助我吗? 代码: public stati
为什么 int row = 1; // multiArray[col].length = 6 while(multiArray[col][row] > 1 && row 1) { 关于java -
我制作了一款游戏,我们控制一艘宇宙飞船,可以发射激光摧毁来自顶部的小行星。但在游戏过程中,每当它抛出 IndexOutOfBoundsException 时,我的游戏就会卡住。我不知道为什么会这样。请
我正在尝试实现合并排序,但我不断收到 IndexOutOfBoundsException 并且我无法找出原因。 我已经调试了我的程序,并且在此函数中我从未使用无效索引访问数组。引发异常的行也与抛出异常
我无法弄清楚为什么我的代码出现 IndexOutOfBoundsException。我想知道是否有人可以成为我的额外眼睛来帮助我发现错误。我正在手动尝试这个,但我认为在浏览代码时我遗漏了一些东西。 代
为什么我会收到此错误? Caused by: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0 at java.util.
我不知道这是否是一个简单的问题,但我只是看不出问题是什么。我现在从 Google Play 中的应用收到了三份关于 points.get(++i) 处的 IndexOutOfBoundsExcepti
如何执行以下内容 String p = "abcd"; System.out.print(p.substring(4)); 不会导致java.lang.IndexOutOfBoundsExceptio
我是一名优秀的程序员,十分优秀!