A.A
A.A

Reputation: 1148

Android : Crash when adding ImageView programmatically?

When i am adding ImageView programmatically for first time show me ImageView but for second time get me crash :

    imgtest = (ImageView) findViewById(R.id.someID);
            imgtest.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    Medtconversation = edtconversation.getText().toString();
                    edtconversation.setText("");
                    if(Medtconversation.length() > 0)
                    {
                    if (flagTyp == true) {

I add Image View Here------->imageView.setImageResource(R.drawable.chat_logo);
                    --->imageView.setLayoutParams(new LayoutParams(
                    --->        LayoutParams.MATCH_PARENT,
                    --->        LayoutParams.WRAP_CONTENT));
                    --->rilative.addView(imageView);<---- Line 286

                        txtviewUser = new TextView(ConversationPage.this);
                        txtviewUser.setText(Medtconversation);
                        txtviewUser.setTextColor(Color.BLUE);
                        txtviewUser.setTypeface(null, Typeface.BOLD);
                        txtviewUser.setTextSize(TypedValue.COMPLEX_UNIT_SP,25);
                        txtviewUser.setPadding(20, 5, 20, 10);

                        rilative.setBackgroundResource(R.drawable.conversation_shap);
                        rilative.addView(txtviewUser);

                        MyTaskParams params = new MyTaskParams(Integer
                                .parseInt(UserID), 0, Integer.parseInt(IDEtegh),
                                Integer.parseInt(IDSharee), UserNAME, NSharee,
                                NEtegh, Medtconversation, URL);
                        MySendMessage sendmsg = new MySendMessage();
                        try {
                            String Rest = sendmsg.execute(params).get();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        } catch (ExecutionException e) {
                            e.printStackTrace();
                        }
                        flagTyp = false;
                    }
                    }

And my Layout Xml is :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/scrollID"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <LinearLayout
                android:id="@+id/linearone"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/brown"
                android:orientation="vertical" >
            </LinearLayout>

            <LinearLayout
                android:id="@+id/lineartwo"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="true"
        android:orientation="horizontal"
        android:paddingBottom="5dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:weightSum="1.5" 
        android:background="@drawable/bottom_conversation">

           <TextView
            android:id="@+id/isTyping"
            android:layout_width="0dip"
            android:layout_weight="0.5"
            android:layout_height="wrap_content"
            android:hint="Chat" />

        <EditText
            android:id="@+id/txtInpuConversation"
            android:layout_width="200dip"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:hint="@string/edt_Conversation" >

            <requestFocus />
        </EditText>
        <ImageButton
            android:id="@+id/someID"
            android:layout_width="0dip"
            android:layout_weight="0.5"
            android:layout_height="wrap_content"
            android:background="@drawable/background_selector"
            android:contentDescription="@null"
            android:layout_marginTop="5dp"
            android:src="@drawable/social_send_now" />
    </LinearLayout>

</LinearLayout>

Error :

by Log tag : AndroidRuntime
by Log Message : at com.test.onlinechattwo.ConversationPage$3.onClick(ConversationPage.java:286)

Upvotes: 0

Views: 459

Answers (2)

user2396269
user2396269

Reputation:

I see your code by carefully, I hope that your chat app is:

enter image description here

If such is, you should add ImageView just one time so edit your code :

boolean flgaimage = false; (In you activity)

And edit your ClickListener :

       .........
        if (flagTyp == true) {

            if(flgaimage == false)
            {
                imageView.setLayoutParams(new LayoutParams(
                        LayoutParams.MATCH_PARENT,
                        LayoutParams.WRAP_CONTENT));
                rilative.addView(imageView);
                flgaimage = true;
            }
        .............

Upvotes: 1

hoomi
hoomi

Reputation: 1912

The problem is that you are trying to add the same instance of imageView every time the onClick is called. The solution is that you have to add the following line before imageView.setImageResource(R.drawable.chat_logo)

imageView = new ImageView(Context)

I hope that it helps

Upvotes: 0

Related Questions