Android:以编程方式添加ImageView时出现崩溃?

时间:2014-08-23 10:50:24

标签: android android-linearlayout android-imageview onclicklistener

当我第一次以编程方式添加ImageView时,请显示ImageView,但第二次让我崩溃:

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

我的Layout Xml是:

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

错误:

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

2 个答案:

答案 0 :(得分:1)

我仔细查看了您的代码,希望您的聊天应用是:

enter image description here

如果是这样,您应该只添加ImageView一次,以便编辑您的代码:

boolean flgaimage = false; (In you activity)

并修改您的ClickListener

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

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

答案 1 :(得分:0)

问题是每次调用onClick时都试图添加相同的imageView实例。解决方案是您必须在imageView.setImageResource(R.drawable.chat_logo)

之前添加以下行
imageView = new ImageView(Context)

我希望它有所帮助