Android:使用可变数量的自定义布局项填充 ListView; java.lang.ClassCastException
我正在编写一个应用程序,随机生成 NPC 的基本细节和 RPG 的其他内容,此活动是一个设置页面,用于修改不同角色种族的数量、名称和概率,以便随机生成这些角色。
我的布局将有一个 ListView,每个项目都有一个显示种族名称的 TextView,以及一个带有浮点值的 EditText,用于输入在生成新角色时出现种族的概率。尽管它(感觉上?)相对简单,但我在实现这种布局时遇到了困难,因为我对 Android 平台以及 Java 都很陌生。
- 固定的 - 启动活动后,我收到 java.lang.ClassCastException: Integer。调试器将我指向预编译资源 jar 中的行,并且我不确定带有变量 e 的 classCastException 到底意味着什么,如果我自己没有编写异常处理程序,那么它会在哪里传递? -- 已修复 --
我有一个处理 JSONArray 的新错误,您可以在我的代码中看到我声明了 null String[] 和 float[] 数组,然后尝试使用将 JSONArray 拉取为的方法在 try/catch 中填充它们从 SharedPreferences 中获取字符串并将值放入给定的数组中。当我在下面的 for 循环中为其赋值时,将它们声明为 null 会引发 NullPointer 异常,而在 try/catch 内声明它们则仅提供 try/catch 块语句的作用域。
我感谢您对我的困境的考虑以及您可以提供的任何帮助。
这是我的代码:
import org.json.JSONException;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class GeneratorSettingsNPCRaceProbsActivity extends RandomGeneratorActivity
{
/** Called when the activity is first created.
* @throws JSONException */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_npc_race_probs);
ListView settingsList = (ListView)findViewById(R.id.listView_menu);
//Sets up a list of races with their corresponding probabilities in a text field. In the future,
//one will be able to edit a text field an commit changes to set the probabilities for each race.
//Perhaps I will implement a way to add custom races to the list.
//Finds current profile
SharedPreferences appSettings = getSharedPreferences(APP_PREFERENCES, MODE_PRIVATE);
String currentProfile = appSettings.getString("CurrentProfile", DEFAULT_PROFILE);
//Gets races and probabilities from current SharedPreferences profile
SharedPreferences profileSettings = getSharedPreferences(currentProfile, MODE_PRIVATE);
String[] races = null;
float[] raceProbs = null;
try {
races = getProfileRaces(currentProfile);
raceProbs = getProfileRaceProbabilities(currentProfile);
} catch (JSONException e) {e.printStackTrace();}
//Puts the races and raceProbs into a RaceItem array
RaceItem[] raceItems = new RaceItem[races.length];
for (int i = 0; i < races.length; i++)
{
raceItems[i].raceName = races[i];
raceItems[i].raceProb = raceProbs[i];
}
Context context = this;
final LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
ArrayAdapter<RaceItem> adaptMenu = new ArrayAdapter<RaceItem>(context, android.R.layout.expandable_list_content)
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row;
if (convertView == null) {
row = inflater.inflate(R.layout.settings_npc_race_probs_item, null);
}
else {
row = convertView;
}
TextView tv1 = (TextView) row.findViewById(android.R.id.text1);
tv1.setText(getItem(position).raceName);
EditText et1 = (EditText) row.findViewById(android.R.id.text2);
et1.setText(String.valueOf(getItem(position).raceProb));
return row;
}
};
settingsList.setAdapter(adaptMenu);
}
}
I am writing an application that randomly generates basic details of NPCs and other things for RPGs, this Activity is a settings page for modifying the number, names, and probabilities of the different character races for the purposes of randomly generating these characters.
My layout would have a ListView, each item having a TextView displaying the name of a race, and an EditText with a float value to input the probability of the race appearing upon generation of a new character. I am having trouble making this layout happen, despite its (perceived?) relative simplicity, as I am quite new to the Android platform as well as Java in general.
-- FIXED --
Upon starting the activity I get a java.lang.ClassCastException: Integer. The debugger points me to lines in precompiled resource jars, and I am unsure of what exactly a classCastException with the variable e means, where would that get passed in, if I didn't write an exception handler myself?
-- FIXED --
I have a new error that deals with JSONArrays, you can see in my code that I declare null String[] and float[] arrays, then attempt to fill them within a try/catch using methods that pull JSONArrays as strings from the SharedPreferences and put values into the given arrays. Declaring them null gives me a NullPointer exception when I assign values to it in my for loop below, and declaring them inside the try/catch gives them scope only for the try/catch block statement.
I appreciate your consideration of my predicament, and any help you can offer.
This is my code:
import org.json.JSONException;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class GeneratorSettingsNPCRaceProbsActivity extends RandomGeneratorActivity
{
/** Called when the activity is first created.
* @throws JSONException */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_npc_race_probs);
ListView settingsList = (ListView)findViewById(R.id.listView_menu);
//Sets up a list of races with their corresponding probabilities in a text field. In the future,
//one will be able to edit a text field an commit changes to set the probabilities for each race.
//Perhaps I will implement a way to add custom races to the list.
//Finds current profile
SharedPreferences appSettings = getSharedPreferences(APP_PREFERENCES, MODE_PRIVATE);
String currentProfile = appSettings.getString("CurrentProfile", DEFAULT_PROFILE);
//Gets races and probabilities from current SharedPreferences profile
SharedPreferences profileSettings = getSharedPreferences(currentProfile, MODE_PRIVATE);
String[] races = null;
float[] raceProbs = null;
try {
races = getProfileRaces(currentProfile);
raceProbs = getProfileRaceProbabilities(currentProfile);
} catch (JSONException e) {e.printStackTrace();}
//Puts the races and raceProbs into a RaceItem array
RaceItem[] raceItems = new RaceItem[races.length];
for (int i = 0; i < races.length; i++)
{
raceItems[i].raceName = races[i];
raceItems[i].raceProb = raceProbs[i];
}
Context context = this;
final LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
ArrayAdapter<RaceItem> adaptMenu = new ArrayAdapter<RaceItem>(context, android.R.layout.expandable_list_content)
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row;
if (convertView == null) {
row = inflater.inflate(R.layout.settings_npc_race_probs_item, null);
}
else {
row = convertView;
}
TextView tv1 = (TextView) row.findViewById(android.R.id.text1);
tv1.setText(getItem(position).raceName);
EditText et1 = (EditText) row.findViewById(android.R.id.text2);
et1.setText(String.valueOf(getItem(position).raceProb));
return row;
}
};
settingsList.setAdapter(adaptMenu);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该确保在代码中发生异常的地方放置了 try\catch (您可能会吞掉实际的异常)。
例外情况是指向代码中尝试用不一致的内容填充值的位置(可能尝试将 String 放入 int 值中)。
您没有在此问题中包含完整的堆栈(获得此信息的一个好方法是打开命令提示符,然后键入:adb logcat - 这应该清楚地指向 Android 代码中出错的特定位置)。
You should make sure you put a try\catch around the place in the code that the exception is occurring (you may be swallowing the actual exception).
The exception is pointing to a place in the code where you are trying to populate a value with something that is inconsistent (probably trying to put a String in an int value).
You didn't include the complete stack in this question (a good way to get this is to open a command prompt, and type: adb logcat - which should clearly point to the specific place in your Android code that is erroring out).