私は簡単な電話帳を作成しています。
問題が何であるかわかりません。"Private TextView name = new TextView(this);"という行にアプリがクラッシュする
私はMainActivityとAbonentという2つのクラスを持っています。 AbonentはMainActivityに必要なすべての情報を与え、この情報を画面に出力する必要がありますが、 "private TextView name = new TextView(this);"という行になると、 Abonentクラスのアプリケーションがクラッシュするここで
は私のコードです:
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;
}
}
'Abonent'は' MainActivity'を拡張してはいけません**そうでなければ** newを作るべきではありません(新しいコードを作成しないでください)それはアクティビティクラスなのでAbonentです。 –