Reputation: 3
I already looked for some answer on the internet but i didn't find so i'm asking for your help I want to add a TextView everytime i click on a button but with a different value. For exemple, first click adds a TextView with the number 1, second click adds a TextView with the number 2. My app crashs everytime i call the method.
Here the code
public class Activity_Planification_Actu extends AppCompatActivity {
LinearLayout mparent;
LayoutInflater layoutInflater;
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page_planification);
mparent = findViewById(R.id.Layout1);
layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
}
public void boutonTest(View v) {
View myview = layoutInflater.inflate(R.layout.semaine_planif, null, false);
TextView tv = (TextView) myview.findViewById(R.id.case_semaine);
tv.setText(i);
i++;
mparent.addView(myview);
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/include_entete_accueil"
layout="@layout/tabdebut"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="1647dp"
android:layout_height="930dp"
android:orientation="vertical"
app:layout_constraintStart_toEndOf="@+id/include_entete_accueil"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="@+id/Layout1"
android:layout_width="match_parent"
android:layout_height="53dp"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:id="@+id/Layout2"
android:layout_width="match_parent"
android:layout_height="82dp"
android:orientation="horizontal"></LinearLayout>
<LinearLayout
android:id="@+id/Layout3"
android:layout_width="match_parent"
android:layout_height="94dp"
android:orientation="horizontal"></LinearLayout>
<LinearLayout
android:id="@+id/Layout4"
android:layout_width="match_parent"
android:layout_height="69dp"
android:orientation="horizontal"></LinearLayout>
<LinearLayout
android:id="@+id/Layout5"
android:layout_width="match_parent"
android:layout_height="87dp"
android:orientation="horizontal"></LinearLayout>
<LinearLayout
android:id="@+id/Layout6"
android:layout_width="match_parent"
android:layout_height="84dp"
android:orientation="horizontal"></LinearLayout>
<LinearLayout
android:id="@+id/Layout7"
android:layout_width="match_parent"
android:layout_height="91dp"
android:orientation="horizontal"></LinearLayout>
<LinearLayout
android:id="@+id/Layout8"
android:layout_width="match_parent"
android:layout_height="81dp"
android:orientation="horizontal"></LinearLayout>
<LinearLayout
android:id="@+id/Layout9"
android:layout_width="match_parent"
android:layout_height="63dp"
android:orientation="horizontal"></LinearLayout>
<LinearLayout
android:id="@+id/Layout10"
android:layout_width="match_parent"
android:layout_height="72dp"
android:orientation="horizontal"></LinearLayout>
<LinearLayout
android:id="@+id/Layout11"
android:layout_width="match_parent"
android:layout_height="82dp"
android:orientation="horizontal"></LinearLayout>
<LinearLayout
android:id="@+id/Layout12"
android:layout_width="match_parent"
android:layout_height="71dp"
android:orientation="horizontal"></LinearLayout>
</LinearLayout>
<Button
android:id="@+id/boutontest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:onClick="boutonTest"
android:text="Button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/include_entete_accueil" />
</android.support.constraint.ConstraintLayout>
06-17 17:25:10.766 6841-6841/com.imsnancydaunot.imscoaching E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.imsnancydaunot.imscoaching, PID: 6841
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
at android.content.res.Resources.getText(Resources.java:312)
at android.widget.TextView.setText(TextView.java:4417)
at categ_planification.Activity_Planification_Actu.boutonTest(Activity_Planification_Actu.java:34)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
https://i.sstatic.net/02iKs.png
To be more precised i want right to the textview another textview but with a different text
Upvotes: 0
Views: 36
Reputation: 854
Your problem in:
tv.setText(i);
You call to setText with int
argument and Android think that you pass String's resource id like tv.setText(R.string.some_text)
and
cant find resource which id is equals to i
i.e. 0
that's why this crash happend .
You should use:
tv.setText(String.valueOf(i));
//or
tv.setText("" + i);
Upvotes: 1
Reputation: 422
provide Logcat
or
try this can do with loop
LinearLayout my_ll = (LinearLayout) findViewById(R.id.Layout1);
for(int i=0;i<your_number_of_textviews;i++)
{
TextView text = new TextView(this);
text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
text.setText("text"+i);
my_ll.addView(text);
}
every button click logic
LinearLayout my_ll = (LinearLayout) findViewById(R.id.Layout1);
int clickcount=0; //set as global
yourbutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
clickcount=clickcount+1;
if(clickcount==0)
{
//check how many times clicked and so on
//here, you can add textview without loop
TextView text = new TextView(this);
text.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
text.setText("text"+clickcount);
my_ll.addView(text);
}
});
Upvotes: 0