Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.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 当我通过一个';视图';由';findViewById()';是否将LinearLayout类的addView()作为参数?_Java_Android - Fatal编程技术网

Java 当我通过一个';视图';由';findViewById()';是否将LinearLayout类的addView()作为参数?

Java 当我通过一个';视图';由';findViewById()';是否将LinearLayout类的addView()作为参数?,java,android,Java,Android,代码如下。。。它是一个android项目的主要活动类。我已在“ant”中使用调试模式编译并创建了一个.apk文件,并将其安装在我的android设备上。运行已安装的应用程序时,会显示一条错误消息: 不幸的是,app1已无法正常工作 主要活动类 package arkk.app1; import android.app.Activity; import android.os.Bundle; import android.widget.*; import a

代码如下。。。它是一个android项目的主要活动类。我已在“
ant
”中使用调试模式编译并创建了一个
.apk
文件,并将其安装在我的android设备上。
运行已安装的应用程序时,会显示一条错误消息:

不幸的是,app1已无法正常工作

主要活动类

    package arkk.app1;

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.*;
    import android.view.*;
    import android.view.ViewGroup.*;

    public class Main_Activity extends Activity
    {
        TextView tV;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            LinearLayout v=new LinearLayout(this);
            Button button1=(Button) findViewById(R.id.button1);
            Button button2=new Button(this);
            tV=(TextView) findViewById(R.id.textView);
            v.addView(tV);
            v.addView(button1);
            v.addView(button2,new               
            LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
            setContentView(v);
        }
        public void actionPerformed(View view) {
            tV.setText("Button1");

        }
    }
如果第19行(
v.addView(tV)
)和第20行(
v.addView(button1)
)被注释掉,一切都很好(应用程序在我的设备上运行)

但是,如果我在我的设备(android kitkat)上编译并运行应用程序,而不注释第19行和第20行,则会出现上述错误

所以我发现是第19行和第20行导致了错误,但无法找出原因

我刚刚开始学习android编程,正在使用“
ApacheAnt
”在命令提示符的帮助下构建我的项目。
我已经在这个网站上查看了
LinearLayout类的文档:

通过它,我能够验证“
addView(View View)
”是
View类的方法,并且
LinearLayout扩展了View

所以现在我已经不知所措了,这段代码有什么问题吗。我猜运行时异常正在发生?…但这只是一个猜测


提前感谢您的回答

为了使用findViewById查找视图,您必须膨胀视图或在包含它们的主视图上调用setContentView

在onCreate中调用super之后,只需调用setContentView(R.layout.your_活动)。您的_活动将是该活动的xml文件

        Button button1=(Button) findViewById(R.id.button1);
在上面的一行中,findViewById不知道要在哪个视图中搜索才能找到button1。这是因为setContentView是在这一行之后调用的。因此,在此之前,您必须为包含的视图调用setContentView

或者,膨胀视图并在该视图上调用findViewById

View v = LayoutInflater.from(getActivity()).inflate(R.layout.your_activity, null);
Button b = (Button) v.findViewById(...);
tV=(TextView) v.findViewById(R.id.textView);

嗯。。。根据索鲁沙的建议并做了更多的研究后,我发现
v.addView(tV)
(第19行)和
v.addView(button1)
(第20行)引发的异常是
非法状态异常,导致应用程序崩溃。
修改后的代码如下

    package arkk.app1;

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.*;
    import android.view.*;
    import android.view.ViewGroup.*;

    public class Main_Activity extends Activity
    {
        TextView tV;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            LinearLayout v=new LinearLayout(this);

            View vCustom =                                 
LayoutInflater.from(this).inflate(R.layout.main, null);
            Button button1=(Button) vCustom.findViewById(R.id.button1);
            Button button2=new Button(this);
            tV=(TextView) vCustom.findViewById(R.id.textView);
            try {
                v.addView(tV);
                v.addView(button1);
            }catch(Exception e) {
                button2.setText(e.toString());
            }

            v.addView(button2,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
            setContentView(v);

        }
        public void actionPerformed(View view) {
            tV.setText("Button1");

        }
    }
引发此异常的原因是
textView
button1
已经是
LinearLayout
视图的子视图,该视图在通过行-

查看vCustom=LayoutInflater.from(this).充气(R.layout.main,null)
当我们尝试将这两个视图添加到新的父视图(或容器)时,v会发生
IllegalStateException


我已经在按钮(button2)上显示了输出,并使用try-catch语句捕获异常并在按钮上显示它

这可能是由于已经添加了视图,但是-请发布描述崩溃的日志猫。不幸的是,您告诉我们“应用程序已停止工作”并没有透露任何关于崩溃的信息,也没有帮助我们帮助您。这是否意味着在xml布局中声明的视图对象不能被视为那些以编程方式声明的视图对象?不,它们是相同的,可以被视为相同的。您必须记住的唯一一件事是,如果不首先膨胀资源,您绝对无法访问放入xml中的对象。setContentView会在显示视图之前对其进行充气。另一种方法是像我在回答中提到的那样手动膨胀它。为了添加更多细节,xml只保存属性而不保存对象。当您调用findViewById时,Android会查找已经绘制的对象。因此,必须首先通过膨胀视图对象所属的位置来创建视图对象。否则,如果尚未绘制,则返回null。这就是setContentView的基本功能。它在布局文件中绘制对象,以便您以后可以访问它们。