【发布时间】:2015-07-19 12:10:21
【问题描述】:
我有一堂课:
import android.content.Context;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class workingOneWayAdapter extends BaseAdapter {
private Context mContext;
public workingOneWayAdapter(Context c) {
mContext = c;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
TextView workingLabel;
if (convertView == null) {
workingLabel = new TextView(mContext);
workingLabel.setLayoutParams(new MyGridView.LayoutParams(85, 85));
workingLabel.setPadding(10, 5, 5, 5);
workingLabel.setTextColor(Color.parseColor("#000000"));
workingLabel.setTextSize(TypedValue.COMPLEX_UNIT_SP, 19);
workingLabel.setSingleLine();
setTimes();
} else {
workingLabel = (TextView) convertView;
}
workingLabel.setText(workingOneWayArray[position]);
return workingLabel;
}
String[] workingOneWayArray;
void setTimes() {
workingOneWayArray = new String[] { "00:00" };
}
public int getCount() {
return workingOneWayArray.length;
}
}
但这会使我的应用程序崩溃。我需要编辑方法内的数组,因为然后可以从类的其他部分访问该数组。你能告诉我有什么问题吗?谢谢!
【问题讨论】:
-
你能提供更多这个类的代码吗,因为看起来没什么问题,也尝试调试你的android应用程序
-
应用在getCount()方法上崩溃,但我不知道为什么
-
也许您在发出 getCount 之前没有调用 setTimes 方法。向我们展示您的完整代码
-
我会建议创建这个类的构造函数并在那里初始化你的 workingOneWayArray =new String[0];并确保在调用 setTimes() 之后调用 getCount
-
通过使用 Log.d("APP","EXECUTED"); 发现 setTimes() 实际执行与否。在这个函数内部并在 Logcat 中检查它