我开发了一款小型Android应用。我使用了final关键字并在create方法中初始化了资源imageView。我已经定义了一个私有方法,我在其中使用了imageview但它在左侧给了我红色标记。拜托,有谁能帮我怎么写。这个计划。我在这里先向您的帮助表示感谢。
SecondActivity.java
public class SecondActivity extends Activity {
Button btnPrevious= null;
Button btnNext = null;
TextView txtTest = null;
int conditions[];
String days[];
// condition imageView initialization together with declaration
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
//ImageView Initialization
btnPrevious = (Button) findViewById(R.id.btnPrevious2);
btnNext = (Button) findViewById(R.id.btnNext2);
txtTest = (TextView) findViewById(R.id.txtTest2);
final ImageView T1C4R4 = (ImageView) findViewById(R.id.T1C4R4);
final ImageView T2C4R5 = (ImageView) findViewById(R.id.T2C4R5);
final ImageView T4C4R1 = (ImageView) findViewById(R.id.T4C4R1);
final ImageView T3C4R1 = (ImageView) findViewById(R.id.T3C4R1);
// imageView initialization together with declaration
final ImageView T1C4R1 = (ImageView) findViewById(R.id.T1C4R1);
final ImageView T1C2R3 = (ImageView) findViewById(R.id.T1C2R3);
final ImageView T1C1R4 = (ImageView) findViewById(R.id.T1C1R4);
final ImageView T2C4R1 = (ImageView) findViewById(R.id.T2C4R1);
final ImageView T2C2R3 = (ImageView) findViewById(R.id.T2C2R3);
final ImageView T2C1R5 = (ImageView) findViewById(R.id.T2C1R5);
final ImageView T4C1R1 = (ImageView) findViewById(R.id.T4C1R1);
final ImageView T4C2R2 = (ImageView) findViewById(R.id.T4C2R2);
final ImageView T4C5R5 = (ImageView) findViewById(R.id.T4C5R5);
final ImageView T3C4R5 = (ImageView) findViewById(R.id.T3C4R5);
final ImageView T3C2R3 = (ImageView) findViewById(R.id.T3C2R3);
final ImageView T3C1R1 = (ImageView) findViewById(R.id.T3C1R1);
final ImageView[][] dressImageView =
{
{T1C4R1, T1C2R3, T1C1R4},
{T2C4R1,T2C2R3, T2C1R5} ,
{T4C1R1, T4C2R2,T4C5R5},
{T3C4R5, T3C2R3, T3C1R1}
};
final ImageView [] dayCell = {T1C4R4 ,T2C4R5, T4C4R1, T3C4R1 };
// get the Intent that started this Activity
Intent in = getIntent();
// get the Bundle that stores the data of this Activity
Bundle b = in.getExtras();
// getting data from bundle
conditions = b.getIntArray("condition");
days = b.getStringArray("day");
// show data to layout
txtTest.setText("[Condition: " + conditions[0] + ", " + conditions[1] + ", " + conditions[2] + ", " + conditions[3] + "]");
for (int c = 0; c < conditions.length; c++) {
//dayWeatherCondtion(dc, days[dc]);
weatherCondition(c, conditions[c]);
}
btnPrevious.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
btnNext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Bundle b = new Bundle();
b.putInt("condition", conditions[1]);
b.putString("day", days[1]);
// Creating Intent object
Intent in = new Intent(SecondActivity.this, DetailActivity.class);
// Storing bundle object into intent
in.putExtras(b);
startActivity(in);
}
});
}
private void weatherCondition(int day, int condition) {
switch(condition){
case 10:
{
//String dress_array[] = ;
weatherDress(day, getResources().getStringArray(R.array.five));
break;
}
case 5:
case 6:
{
// String dress_array[] = getResources().getStringArray(R.array.five);
weatherDress(day, getResources().getStringArray(R.array.five));
break;
}
}
}
private void weatherDress(int day, String[] dress_array) {
for(int d = 0; d < dress_array.length; d++ ){
//ImageView temp = dressImageView[d];
dressImageView[day][d].setImageDrawable(getDrawableByName(SecondActivity.this, dress_array[d]));
}
}
public Drawable getDrawableByName(Context context, String name){
Resources resources = context.getResources();
final int resourceId = resources.getIdentifier(name, "drawable",
context.getPackageName());
return resources.getDrawable(resourceId);
}
}
答案 0 :(得分:1)
您的私有方法似乎正在尝试访问 null 的第一个final ImageView T1C4R4;
。
当您创建最终变量时,您必须初始化它,因为您将无法使用其他值重新分配它。
答案 1 :(得分:1)
Vyas的
我不知道你想要用这个代码实现什么,但是我可以为你解决问题
您需要在使用该对象的地方递归传递dressImageView对象的引用
例如
weatherCondition(c, conditions[c], dressImageView);
.....
case 10:
{
//String dress_array[] = ;
weatherDress(day, getResources().getStringArray(R.array.five),dressImageView);
break;
}
....