0

I try to develop a simple Android App with one Button which generates new TextViews on each click.

 import android.app.Activity;
 import android.os.Bundle;
 import android.text.Layout;
 import android.view.View;
 import android.widget.Button;
 import android.widget.TextView;

 public class CreateTV extends Activity {

    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       setContentView(R.layout.main);
       Button mCreate = (Button)findViewById(R.id.btnCreate);

       mCreate.setOnClickListener(new View.OnClickListener() {

       @Override
       public void onClick(View v) {
            // TODO Auto-generated method stub
            ((Button) v).setText("Clicked");
            TextView mTV1 = new TextView(this);
       }
   });
 }
}

My code is wrong because of:

               TextView mTV1 = new TextView(this);

I could find some similar examples, which generate objects programmatically in onCreate(). But I want to generate and modify new objects in onClick(). Would anybody please help?

Community
  • 1
  • 1
Behy
  • 483
  • 7
  • 23
  • 1
    The reason `(this)` doesn't work is because you are inside the `onClick` method of the button, so 'this' refers to the click listener, not the activity. Try changing it to `new TextView(getActivity())`, and see if it works differently. – AdamMc331 Mar 27 '15 at 14:50
  • use `CreateTV.this`. – EpicPandaForce Mar 27 '15 at 15:01

4 Answers4

2

Change

TextView mTV1 = new TextView(this);

to

TextView mTV1 = new TextView(CreateTV.this);
M D
  • 47,665
  • 9
  • 93
  • 114
1

Views can only be instantiated with a context as parameter

As you can see in the documentation a TextView needs the context to be created. TextView(Context context)

Since you are trying to create a TextView inside a ClickListener you can not use this as a reference to a Context-extending object.

As McAdam331 pointed out, use new TextView(getActivity), this works because Activity extends Context.

Community
  • 1
  • 1
AKroell
  • 622
  • 4
  • 18
0

In addition to change TextView mTV1 = new TextView(this); to TextView mTV1 = new TextView(CreateTV.this);, you must add the TextView within a view like the following:

import android.app.Activity;
import android.os.Bundle;
import android.text.Layout;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class CreateTV extends Activity {

public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

   setContentView(R.layout.main);
   Button mCreate = (Button)findViewById(R.id.btnCreate);

   mCreate.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View v) {
        // TODO Auto-generated method stub
        ((Button) v).setText("Clicked");
        TextView mTV1 = new TextView(CreateTV.this);
        addContentView(mTV1);
   }
});
}
}
Lennon Spirlandelli
  • 3,131
  • 5
  • 26
  • 51
0

I would prefer adding a Context, setting it to final and then call the Textview using the Context.

Example:

public class CreateTV extends Activity {

public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);

   setContentView(R.layout.main);
   Button mCreate = (Button)findViewById(R.id.btnCreate);

   final Context mContext = this;
   mCreate.setOnClickListener(new View.OnClickListener() {


   @Override
   public void onClick(View v) {
        // TODO Auto-generated method stub
        ((Button) v).setText("Clicked");
        TextView mTV1 = new TextView(mContext);
        addContentView(mTV1);
   }
});
}
}

If you want to use the Context outside the onCreate method (and within Listeners) you can define a Context.

 private Context context;
 public void onCreate(....) {
   this.context = this;
 }

private void aMethod() {
  context....
}

Theres another way doing such cool stuff. Create a Class and extends it by Application.

public class MainApplication extends Application {
       public static Context getContext() { return this; }
}

Then add the MainApplication to your Manifest.

<application
    android:name=".MainApplication"
 >

and access it from everywhere with MainApplication.getContext();

Emanuel
  • 8,027
  • 2
  • 37
  • 56
  • thats prety cool! based on this method it should be possible to address different layouts in the multiple-layout Apps; I mean by defining individual classes for each layout and defining public contexts in each class. Then it would be possible to add objects to different layouts by one button click, Isn't it? – Behy Mar 27 '15 at 15:37
  • nope. Not if you define it within a class which extends Application – Emanuel Mar 27 '15 at 16:07