简体   繁体   中英

Getting error when dynamically adding views to activity

When I start my main activity, I get the following error stack:

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)

I can't figure it out why this is happening, searched hours trying to resolve this.

Here my MainActivity:

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

}

Here are my xml layout configuration files:

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>

Here my 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>

I tried to do like this example: http://android-er.blogspot.com.br/2013/05/add-and-remove-view-dynamically.html

Can someone help me with this? I very noob programing to android, so maybe it's a very simple error!! Thank you!

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

And you are casting R.id.svQuestionario as LinearLayout . This will cause ClassCastException . Also, you cannot add another child to the ScrollView . So, To fix this, use

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM