动态向活动添加视图时出错

时间:2013-12-10 02:04:59

标签: java android

当我开始主要活动时,我得到以下错误堆栈:

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)

我无法弄清楚为什么会发生这种情况,搜索时间会尝试解决此问题。

这是我的主要活动:

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

有人可以帮我吗?我非常noob编程到android,所以也许这是一个非常简单的错误!谢谢!

1 个答案:

答案 0 :(得分:3)

<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);