我正在制作一本简单的电话簿 我不知道是什么问题。
我有两个类:MainActivity和Abonent; Abonent将所有必要的信息提供给MainActivity,然后将此信息输出到屏幕,但是当涉及到“private TextView name = new TextView(this);”这一行时,在应用程序类中,应用程序崩溃。
这是我的代码:
public class MainActivity extends AppCompatActivity {
LinearLayout main, submain;
LinearLayout.LayoutParams main_param, submain_param;
protected void onCreate(Bundle savedInstanceState) {
//Initializing
main = new LinearLayout(this);
main.setOrientation(LinearLayout.VERTICAL);
main.setBackgroundColor(getColor(R.color.myblue));
main_param = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
super.onCreate(savedInstanceState);
setContentView(main, main_param);
for(int i = 0; i < 4; i++) {
Abonent abn = new Abonent("Kathrine", 0x7f020054 + i , "+38096" + i);
submain= new LinearLayout(this);
submain.setOrientation(LinearLayout.HORIZONTAL);
submain_param = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
submain_param.bottomMargin = 40;
main.addView(abn.getName(), abn.name_param);
submain.addView(abn.getIcon(), abn.icon_param);
submain.addView(abn.getNumber(), abn.number_param);
main.addView(submain, submain_param);
}
}
}
public class Abonent extends MainActivity{
public Abonent(String name, int iconId, String number){
setName(name);
setNumber(number);
setIcon(iconId);
}
final LinearLayout.LayoutParams name_param = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final LinearLayout.LayoutParams icon_param = new LinearLayout.LayoutParams(250, 250);
final LinearLayout.LayoutParams number_param = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
private TextView name = new TextView(this);
private ImageView icon = new ImageView(this);
private TextView number = new TextView(this);
//private Button call = new Button(this);
private SpannableString content;
private void setName(String name){
content = new SpannableString(name);
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
this.name.setText(content);
this.name.setTextColor(getResources().getColor(R.color.myblack));
this.name.setTextSize(30);
this.name_param.leftMargin = 40;
}
private void setNumber(String number){
content = new SpannableString(number);
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
this.number.setText(content);
this.number.setTextColor(getResources().getColor(R.color.myblack));
this.number.setBackgroundColor(getResources().getColor(R.color.myblue));
this.number.setTextSize(30);
this.number_param.leftMargin = 40;
}
private void setIcon(int iconId){
this.icon.setImageResource(iconId);
}
public TextView getName(){
return this.name;
}
public TextView getNumber(){
return this.name;
}
public ImageView getIcon(){
return this.icon;
}
}
答案 0 :(得分:2)
首先,您应该在super.onCreate
方法中首先调用onCreate
。总是。没有例外。
无论如何,错误是因为this
的引用无法获得以您完成的方式创建新视图所需的Context
。
你也不需要Abonent extends MainActivity
,主要是因为我不认为你明白你在那里做什么。
但是,如果要为电话簿制作联系人列表,则需要ListView&amp; ArrayAdapter,而不是具有动态添加视图的LinearLayout。
对于初学者,我建议您阅读Android ListView - Using a Custom ArrayAdapter。 (请阅读页面顶部,了解更多介绍,而不是潜入代码)
答案 1 :(得分:0)
在调用onCreate
之前,上下文/活动尚未就绪。所以不要试图像那样实例化它们。而是在onCreate