Andrea Montanari
Andrea Montanari

Reputation: 98

Add TextView dynamically in a different Activity (RelativeLayout)

I was trying to add a new TextView every time I click on a button, in a new activity (already created), but I encountered multiple errors such as Null Pointer Exception and Illegal State Exception too.

This is the code I used in the button activity, and it's activated when I press that button. In the code I'm recalling the layout activity_position which is the activity I would like to add the TextView to.

 RelativeLayout rl = (RelativeLayout)findViewById(R.layout.activity_position);
        TextView tv = new TextView(this);
        tv.setId(10);
        tv.setText(addr);
        tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        rl.addView(tv);

Running this code gets me only errors (on rl.addView(tv) line) and I can't figure it out why. This is the XML of the activity where I would like to add the TextViews dynamically.

    <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=".PositionActivity" >

02-18 12:21:47.348: E/AndroidRuntime(4995): FATAL EXCEPTION: main
02-18 12:21:47.348: E/AndroidRuntime(4995): java.lang.IllegalStateException: Could not execute method of the activity
02-18 12:21:47.348: E/AndroidRuntime(4995):     at android.view.View$1.onClick(View.java:3758)
02-18 12:21:47.348: E/AndroidRuntime(4995):     at android.view.View.performClick(View.java:4377)
02-18 12:21:47.348: E/AndroidRuntime(4995):     at android.view.View$PerformClick.run(View.java:18044)
02-18 12:21:47.348: E/AndroidRuntime(4995):     at android.os.Handler.handleCallback(Handler.java:725)
02-18 12:21:47.348: E/AndroidRuntime(4995):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-18 12:21:47.348: E/AndroidRuntime(4995):     at android.os.Looper.loop(Looper.java:137)
02-18 12:21:47.348: E/AndroidRuntime(4995):     at android.app.ActivityThread.main(ActivityThread.java:5306)
02-18 12:21:47.348: E/AndroidRuntime(4995):     at java.lang.reflect.Method.invokeNative(Native Method)
02-18 12:21:47.348: E/AndroidRuntime(4995):     at java.lang.reflect.Method.invoke(Method.java:511)
02-18 12:21:47.348: E/AndroidRuntime(4995):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
02-18 12:21:47.348: E/AndroidRuntime(4995):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
02-18 12:21:47.348: E/AndroidRuntime(4995):     at dalvik.system.NativeStart.main(Native Method)
02-18 12:21:47.348: E/AndroidRuntime(4995): Caused by: java.lang.reflect.InvocationTargetException
02-18 12:21:47.348: E/AndroidRuntime(4995):     at java.lang.reflect.Method.invokeNative(Native Method)
02-18 12:21:47.348: E/AndroidRuntime(4995):     at java.lang.reflect.Method.invoke(Method.java:511)
02-18 12:21:47.348: E/AndroidRuntime(4995):     at android.view.View$1.onClick(View.java:3753)
02-18 12:21:47.348: E/AndroidRuntime(4995):     ... 11 more
02-18 12:21:47.348: E/AndroidRuntime(4995): Caused by: java.lang.NullPointerException
02-18 12:21:47.348: E/AndroidRuntime(4995):     at com.example.placeholder.LocatingActivity.getCoords(LocatingActivity.java:101)

EDIT: added the LogCat

Can you help me out?

Thank you!

FULL CODE: https://drive.google.com/folderview?id=0B3uaHczyJIVQVXhMR1lyN3JnWjQ&usp=sharing

Upvotes: 0

Views: 986

Answers (3)

Ivan Bartsov
Ivan Bartsov

Reputation: 21036

The root exception in your logcat output is in LocatingActivity, line 101. In that activity there are no OnClickListeners, so let's see if I got it right: you're trying to modify contents of an Activity from another Activity (I assume first one is under second one in the stack)?

You can't do that, ever. You can only manipulate views/layouts of the activity that's currently active (i.e. shown), all other activities are paused and may have even been destroyed.

I suggest you read this: http://developer.android.com/guide/components/activities.html along with other basic documentation on the site.

Upvotes: 0

Gimer
Gimer

Reputation: 13

Full answer:

    import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Test extends Activity {

    private LinearLayout LL;
    private RelativeLayout RL;
    private Button btn;


    int click_counter = 0;


    public void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(R.layout.activity_test);

        RL = (RelativeLayout)findViewById(R.id.relative_layout);
        btn = (Button)findViewById(R.id.btn);

        LL = new LinearLayout(this);
        LL.setOrientation(LinearLayout.VERTICAL);
        RL.addView(LL);

        // Listener
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView tv = new TextView(getApplicationContext());
                tv.setText("Click: " + ++click_counter);
                LL.addView(tv);
            }
        });



    } // onCreate()





}

and layout:

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

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Button" />

</RelativeLayout>

Upvotes: 0

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

Currently you are trying to cast R.layout.activity_position to RelativeLayout. assign id to RelativeLayout then use R.id.relatv_layout to in code as:

in xml :

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relatv_layout"
     ....
    tools:context=".PositionActivity" >

In code use R.layout.relatv_layout to initialize rl :

RelativeLayout rl = (RelativeLayout)findViewById(R.id.relatv_layout);

Upvotes: 2

Related Questions