java - 动态向 Activity 添加 View 时出现错误

标签 java android

当我开始主要 Activity 时,我收到以下错误堆栈:

12-10 01:46:41.257: E/AndroidRuntime(849): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.projetofinal_pesquisa_de_campo/br.domBosco.projetofinal_pesquisa_de_campo.MainActivity}: java.lang.NullPointerException
12-10 01:46:41.257: E/AndroidRuntime(849):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
12-10 01:46:41.257: E/AndroidRuntime(849):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-10 01:46:41.257: E/AndroidRuntime(849):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-10 01:46:41.257: E/AndroidRuntime(849):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-10 01:46:41.257: E/AndroidRuntime(849):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-10 01:46:41.257: E/AndroidRuntime(849):  at android.os.Looper.loop(Looper.java:123)
12-10 01:46:41.257: E/AndroidRuntime(849):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-10 01:46:41.257: E/AndroidRuntime(849):  at java.lang.reflect.Method.invokeNative(Native Method)
12-10 01:46:41.257: E/AndroidRuntime(849):  at java.lang.reflect.Method.invoke(Method.java:507)
12-10 01:46:41.257: E/AndroidRuntime(849):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-10 01:46:41.257: E/AndroidRuntime(849):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-10 01:46:41.257: E/AndroidRuntime(849):  at dalvik.system.NativeStart.main(Native Method)
12-10 01:46:41.257: E/AndroidRuntime(849): Caused by: java.lang.NullPointerException
12-10 01:46:41.257: E/AndroidRuntime(849):  at br.domBosco.projetofinal_pesquisa_de_campo.MainActivity.onCreate(MainActivity.java:46)

我不明白为什么会发生这种情况,搜索了几个小时试图解决这个问题。

这是我的主要 Activity :

public class MainActivity extends Activity {

    LinearLayout container;

    @Override   
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//      LeitorXml lxml = new LeitorXml();
//      try {
//          Questionario questionario = lxml.getQuestionariosFromXml2(this);
//      } catch (XmlPullParserException e) {
//          // TODO Auto-generated catch block
//          e.printStackTrace();
//      }

        container = (LinearLayout) findViewById(R.id.svQuestionario);

        LayoutInflater layoutInflater = 
        (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View viewDinamica = layoutInflater.inflate(R.layout.campos_dinamicos, null);
        //setContentView(R.layout.campos_dinamicos);
        TextView lblPergunta = (TextView)viewDinamica.findViewById(R.id.lblCampoDinamico);
        lblPergunta.setText("Uma Pergunta apenas");
        EditText txtPergunta = (EditText)viewDinamica.findViewById(R.id.txtCampoDinamico);

        container.addView(viewDinamica);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

这是我的 xml 布局配置文件:

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/lblNomeQuestionario"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <ScrollView
        android:id="@+id/svQuestionario"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/lblNomeQuestionario"
        android:layout_below="@id/lblNomeQuestionario"
        android:layout_marginTop="18dp" >

        <LinearLayout
            android:id="@+id/container"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"            
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

这是我的campos_dinamicos.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/lblCampoDinamico"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/txtCampoDinamico"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/lblCampoDinamico"        
        android:ems="10" />    

</RelativeLayout>

我尝试这样做: http://android-er.blogspot.com.br/2013/05/add-and-remove-view-dynamically.html

有人可以帮我解决这个问题吗?我对android编程非常菜鸟,所以也许这是一个非常简单的错误!谢谢!

最佳答案

<ScrollView
    android:id="@+id/svQuestionario"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@id/lblNomeQuestionario"
    android:layout_below="@id/lblNomeQuestionario"
    android:layout_marginTop="18dp" >
<LinearLayout
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"            
    android:orientation="vertical" >
</LinearLayout>

并且您正在将 R.id.svQuestionario 转换为 LinearLayout。这将导致ClassCastException。 此外,您不能向 ScrollView 添加另一个子项。因此,要解决此问题,请使用

container = (LinearLayout) findViewById(R.id.container);

关于java - 动态向 Activity 添加 View 时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20484680/

相关文章:

java - Swing JComponents 对齐形式

java - 使用 Java 找不到匹配的构造函数

java - 为什么我不能将非常规类型 Arraylist<> 注册到 ArrayList<> 中?

java - 将 JSONArray 转换为 arrayList

java - org.apache.axiom.om.util.AXIOMUtil 无法解析

java - 在 ImageView 之上显示动画

android - 如何更改 EditText 和 Button 的高度?

java - 使用没有 fragment 的选项卡布局

java - 在父 View 中多次动态包含 XML 相对布局

java - 使用 setOnItemClickListener,类型 TextView 未定义?