0

我正在制作一个记事本类型的应用程序,用户可以在其中在输入文本时拍摄和放置图片。

在我的应用程序中,我有一个button,editTextimageView. 当它开始时,只有buttoneditText。用户输入editText并且当他按下 时button,相机打开。拍摄的照片被放置在editText(用户输入的最后一行下方)的正下方。在图片下会editText出现一个新的,用户可以在其中输入文本。当他再次按下按钮时,另一张照片被拍摄,位于第二张下方,editText第三张editText可供用户输入文本。

因此,每次用户按下按钮拍照时,editText图像下方都会出现一个新的。这不断循环,允许用户输入文本和图像,以及他想要的任意数量的图像和段落。

但这并没有发生。我只能输入一次文字并拍一张照片。没有第二editText个出现。这里出了什么问题。

交替editText和的代码imageView位于 onActivityResult()

mainActivity.java

package com.example.nirvan.cameraexample3;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.media.Image;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity
{


ImageView myImage;
EditText textVar;
int flag=0;             //flag==0 means place img below text, ==1 means below textVar

private String pictureImagePath = "";
Uri outputFileUri;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button imgButton=(Button) findViewById(R.id.imgButton);


    View.OnClickListener imgButtonClickListener=new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            String imageFileName = timeStamp + ".jpg";
            File storageDir = Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES);
            pictureImagePath = storageDir.getAbsolutePath() + "/" + imageFileName;
            File file = new File(pictureImagePath);
            outputFileUri = Uri.fromFile(file);
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            startActivityForResult(cameraIntent, 1);
        }
    };
    imgButton.setOnClickListener(imgButtonClickListener);


}



protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    Log.d("TAG","CUSTOOOM");
    RelativeLayout relativeLayout=(RelativeLayout) findViewById(R.id.relativeLayout);
    myImage=new ImageView(this);
    myImage.setId(+1);




    if (requestCode == 1 && resultCode == RESULT_OK)
    {
        File imgFile = new File(pictureImagePath);
       if (imgFile.exists())
        {
            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
           // myImage = (ImageView) findViewById(R.id.imageViewTest);

            //this code ensures the imageView is placed below the editText
            RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams
                    (RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

            if(flag==0)
               {
                   relativeParams.addRule(RelativeLayout.BELOW, R.id.text);
                   flag=1;
               }
            else if(flag==1)
                relativeParams.addRule(RelativeLayout.BELOW, textVar.getId());

            //myImage.setLayoutParams(relativeParams);
            relativeLayout.addView(myImage,relativeParams);
            //
            myImage.setImageBitmap(myBitmap);


            //place the new editText below the imgaeView just placed
            relativeParams = new RelativeLayout.LayoutParams
                    (RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
            relativeParams.addRule(RelativeLayout.BELOW, myImage.getId());
            textVar=new EditText(this);
            textVar.setId(+2    );
            relativeLayout.addView(textVar,relativeParams);
            textVar.setText("NEW");

        }
    }//






}



}

mainActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/relativeLayout"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.nirvan.cameraexample3.MainActivity">





<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="IMG"
    android:id="@+id/imgButton"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/text"
    android:layout_below="@id/imgButton"
    />

</RelativeLayout>

更新 - 接受Petrumo的建议,我修改了代码。尽管它仍然没有按应有的方式工作。拍摄第一张图像后,图像editText下方不会显示第二张图像。我已确保该方法的每次迭代的idstextView和simageView都不同。

onActivityResult() 更新

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    Log.d("TAG","CUSTOOOM");
    RelativeLayout relativeLayout=(RelativeLayout) findViewById(R.id.relativeLayout);
    myImage=new ImageView(this);
    myImage.setId(id1++);
    textVar.setId(id2++);




    if (requestCode == 1 && resultCode == RESULT_OK)
    {
        File imgFile = new File(pictureImagePath);
       if (imgFile.exists())
        {
            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
           // myImage = (ImageView) findViewById(R.id.imageViewTest);

            //this code ensures the imageView is placed below the editText
            RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams
                    (RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

            if(flag==0)
               {
                   relativeParams.addRule(RelativeLayout.BELOW, R.id.text);
                   flag=1;
               }
            else if(flag==1)
                relativeParams.addRule(RelativeLayout.BELOW, textVar.getId());




            //myImage.setLayoutParams(relativeParams);
            relativeLayout.addView(myImage,relativeParams);
            //
            myImage.setImageBitmap(myBitmap);


            //place the new editText below the imgaeView just placed
            relativeParams = new RelativeLayout.LayoutParams
                    (RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
            relativeParams.addRule(RelativeLayout.BELOW, myImage.getId());
            textVar=new EditText(this);
            //textVar.setId(id2++);

            relativeLayout.addView(textVar,relativeParams);
            textVar.setText("NEW");

        }
    }//






}

UPDATE2:所以我只是onActivityResult用Petrumo's ..替换了我的代码,它可以工作。s 出现在editText另一个下方。但是当我插入imageView代码时,应用程序在拍照后立即崩溃。

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    RelativeLayout relativeLayout=(RelativeLayout) findViewById(R.id.relativeLayout);
    RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams
            (RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    if(textVar != null)
    {
        relativeParams.addRule(RelativeLayout.BELOW, textVar.getId());
    }
    else
    {
        relativeParams.addRule(RelativeLayout.BELOW, R.id.text);
    }

    myImage=new ImageView(this);
    myImage.setId(id1++);
    //set image

        File imgFile = new File(pictureImagePath);
        if (imgFile.exists() && requestCode == 1 && resultCode == RESULT_OK)
        {
            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
            relativeLayout.addView(myImage, relativeParams);
            myImage.setImageBitmap(myBitmap);


        }

    //



    textVar=new EditText(MainActivity.this);
    textVar.setId(id2++);
    relativeParams.addRule(RelativeLayout.BELOW, myImage.getId());
    relativeLayout.addView(textVar, relativeParams);
    textVar.setText("NEW");
    relativeLayout.addView(textVar, relativeParams);




}

日志

 FATAL EXCEPTION: main
                                                                               Process: com.example.nirvan.cameraexample3, PID: 27598
                                                                               java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.nirvan.cameraexample3/com.example.nirvan.cameraexample3.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
                                                                                   at android.app.ActivityThread.deliverResults(ActivityThread.java:3432)
                                                                                   at android.app.ActivityThread.handleSendResult(ActivityThread.java:3475)
                                                                                   at android.app.ActivityThread.access$1300(ActivityThread.java:139)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                   at android.os.Looper.loop(Looper.java:136)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5086)
                                                                                   at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                   at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                                                   at dalvik.system.NativeStart.main(Native Method)
                                                                                Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
                                                                                   at android.view.ViewGroup.addViewInner(ViewGroup.java:3564)
                                                                                   at android.view.ViewGroup.addView(ViewGroup.java:3417)
                                                                                   at android.view.ViewGroup.addView(ViewGroup.java:3393)
                                                                                   at com.example.nirvan.cameraexample3.MainActivity.onActivityResult(MainActivity.java:105)
                                                                                   at android.app.Activity.dispatchActivityResult(Activity.java:5446)
                                                                                   at android.app.ActivityThread.deliverResults(ActivityThread.java:3428)
                                                                                   at android.app.ActivityThread.handleSendResult(ActivityThread.java:3475) 
                                                                                   at android.app.ActivityThread.access$1300(ActivityThread.java:139) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                   at android.os.Looper.loop(Looper.java:136) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5086) 
                                                                                   at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                       at java.lang.reflect.Method.invoke(Method.java:515)
4

1 回答 1

0

使用的代码有几个问题:

  • 使用相同的 id textVar.setId(+2 );

这应该是什么

textVar.setId(id++);

其中 id 是一个全局变量

  • 同样需要应用于图像 id,如果你想将它放在下面,它需要是唯一的并且被下一个使用

我简化了您的解决方案并使其正常工作,您可以将其用作基础并在两者之间添加您的图像:

RelativeLayout relativeLayout=(RelativeLayout) findViewById(R.id.relativeLayout);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams
 (RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
  if(textVar != null)
  {
    relativeParams.addRule(RelativeLayout.BELOW, textVar.getId());
  } else {
    relativeParams.addRule(RelativeLayout.BELOW, R.id.text);
  }

  textVar=new EditText(MainActivity.this);
  textVar.setId(id++);
  textVar.setText("NEW");


 relativeLayout.addView(textVar, relativeParams);

如果您只想在下面添加它们,一种更简单的方法是使用具有垂直方向的 LinearLayout,或者更优雅的解决方案将其更改为带有适配器的列表,如评论中建议的那样。

于 2017-01-03T16:11:27.647 回答