Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当出现“private TextView name=new TextView(this);”一行时,应用程序崩溃_Java_Android_Android Layout - Fatal编程技术网

Java 当出现“private TextView name=new TextView(this);”一行时,应用程序崩溃

Java 当出现“private TextView name=new TextView(this);”一行时,应用程序崩溃,java,android,android-layout,Java,Android,Android Layout,我正在创建一个简单的电话簿。 我不知道是什么问题 我有两个班:主要活动和永久性;ABONT向MainActivity提供所有必要的信息,然后该信息应输出到屏幕,但当涉及到行private TextView name=new TextView this时;在Abonent类中,应用程序崩溃 这是我的密码: public class MainActivity extends AppCompatActivity { LinearLayout main, submain; LinearL

我正在创建一个简单的电话簿。 我不知道是什么问题

我有两个班:主要活动和永久性;ABONT向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);

        }
    }
}

在调用onCreate之前,上下文/活动尚未就绪。所以不要试图像那样实例化它们。首先,您应该调用super.onCreate作为onCreate方法中的第一件事,而不是在onCreate上这样做。总是也不例外

无论如何,错误是因为对它的引用不可用,无法获取创建新视图所需的上下文

您也不需要Abonent extends MainActivity,主要是因为我认为您不了解自己在那里做什么

但是,如果要为电话簿制作联系人列表,则需要ListView&ArrayAdapter,而不是动态添加视图的线性布局

首先,我建议你阅读。从页面顶部阅读更多介绍,而不是深入代码

ABONT不应扩展MainActivity,或者您不应创建新的ABONT,因为它是一个活动类。
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;
    }

}