2

我想做一些类似于 EditContact 活动的事情(使用简单的添加按钮添加各种号码以联系)。

我的第一种方法是创建我动态添加到活动布局的自定义布局(动态添加组件的活动冻结)用户 Budius 提到使用带有自定义适配器的列表视图来实现解决方案。所以我尝试了它,但行为就像我的第一次尝试一样结束。

添加 3-4 个条目后,活动将冻结。(在 Galaxy s3 和模拟器上)

也许你有一个想法?

日志猫:

11-01 15:42:33.214: D/dalvikvm(712): GC_CONCURRENT freed 148K, 3% free 8283K/8519K, paused 74ms+102ms, total 242ms
11-01 15:42:33.733: I/Choreographer(712): Skipped 93 frames!  The application may be doing too much work on its main thread.
11-01 15:42:42.293: I/Choreographer(712): Skipped 49 frames!  The application may be doing too much work on its main thread.
11-01 15:42:44.946: D/dalvikvm(712): GC_CONCURRENT freed 72K, 2% free 8672K/8839K, paused 16ms+36ms, total 224ms
11-01 15:42:45.593: I/Choreographer(712): Skipped 66 frames!  The application may be doing too much work on its main thread.
11-01 15:42:48.913: I/Choreographer(712): Skipped 35 frames!  The application may be doing too much work on its main thread.
11-01 15:42:49.093: I/Choreographer(712): Skipped 42 frames!  The application may be doing too much work on its main thread.
11-01 15:42:55.083: I/Choreographer(712): Skipped 125 frames!  The application may be doing too much work on its main thread.
11-01 15:42:55.343: I/Choreographer(712): Skipped 68 frames!  The application may be doing too much work on its main thread.
11-01 15:42:55.653: I/Choreographer(712): Skipped 40 frames!  The application may be doing too much work on its main thread.
11-01 15:43:02.553: D/dalvikvm(712): GC_CONCURRENT freed 318K, 5% free 8821K/9223K, paused 16ms+68ms, total 231ms
11-01 15:43:02.863: D/dalvikvm(712): GC_FOR_ALLOC freed 418K, 7% free 8800K/9415K, paused 73ms, total 76ms
11-01 15:43:02.883: I/Choreographer(712): Skipped 224 frames!  The application may be doing too much work on its main thread.
11-01 15:43:03.013: D/dalvikvm(712): GC_CONCURRENT freed 397K, 7% free 8804K/9415K, paused 74ms+6ms, total 113ms
11-01 15:43:03.013: D/dalvikvm(712): WAIT_FOR_CONCURRENT_GC blocked 33ms
11-01 15:43:03.063: D/dalvikvm(712): GC_FOR_ALLOC freed 386K, 7% free 8802K/9415K, paused 41ms, total 41ms
11-01 15:43:03.183: D/dalvikvm(712): GC_CONCURRENT freed 388K, 7% free 8806K/9415K, paused 75ms+7ms, total 119ms
11-01 15:43:03.183: D/dalvikvm(712): WAIT_FOR_CONCURRENT_GC blocked 35ms
11-01 15:43:03.243: D/dalvikvm(712): GC_FOR_ALLOC freed 378K, 7% free 8803K/9415K, paused 51ms, total 51ms
11-01 15:43:03.433: I/Choreographer(712): Skipped 142 frames!  The application may be doing too much work on its main thread.
comment:..... adding the 4. row to custom adapter and listview ..... 
11-01 15:36:31.662: I/Choreographer(712): Skipped 36 frames!  The application may be doing too much work on its main thread.
11-01 15:36:32.542: D/dalvikvm(712): GC_CONCURRENT freed 415K, 23% free 9057K/11719K, paused 18ms+15ms, total 139ms
11-01 15:36:32.554: D/dalvikvm(712): WAIT_FOR_CONCURRENT_GC blocked 123ms
11-01 15:36:32.613: D/dalvikvm(712): GC_FOR_ALLOC freed 492K, 23% free 9046K/11719K, paused 56ms, total 56ms
11-01 15:36:32.753: D/dalvikvm(712): GC_CONCURRENT freed 449K, 23% free 9078K/11719K, paused 76ms+7ms, total 134ms
11-01 15:36:32.753: D/dalvikvm(712): WAIT_FOR_CONCURRENT_GC blocked 48ms
11-01 15:36:32.813: D/dalvikvm(712): GC_FOR_ALLOC freed 514K, 23% free 9046K/11719K, paused 54ms, total 54ms
11-01 15:36:32.943: D/dalvikvm(712): GC_CONCURRENT freed 451K, 23% free 9078K/11719K, paused 76ms+8ms, total 128ms
11-01 15:36:32.943: D/dalvikvm(712): WAIT_FOR_CONCURRENT_GC blocked 44ms
11-01 15:36:33.013: D/dalvikvm(712): GC_FOR_ALLOC freed 516K, 23% free 9046K/11719K, paused 53ms, total 53ms
11-01 15:36:33.143: D/dalvikvm(712): GC_CONCURRENT freed 453K, 23% free 9078K/11719K, paused 75ms+9ms, total 127ms
11-01 15:36:33.143: D/dalvikvm(712): WAIT_FOR_CONCURRENT_GC blocked 41ms
11-01 15:36:33.193: D/dalvikvm(712): GC_FOR_ALLOC freed 518K, 23% free 9046K/11719K, paused 50ms, total 50ms
11-01 15:36:33.333: D/dalvikvm(712): GC_CONCURRENT freed 454K, 23% free 9078K/11719K, paused 75ms+8ms, total 137ms
11-01 15:36:33.333: D/dalvikvm(712): WAIT_FOR_CONCURRENT_GC blocked 59ms
11-01 15:36:33.393: D/dalvikvm(712): GC_FOR_ALLOC freed 520K, 23% free 9046K/11719K, paused 56ms, total 56ms
11-01 15:36:33.533: D/dalvikvm(712): GC_CONCURRENT freed 456K, 23% free 9079K/11719K, paused 75ms+8ms, total 129ms
11-01 15:36:33.533: D/dalvikvm(712): WAIT_FOR_CONCURRENT_GC blocked 52ms
comment:..... many of this .....
11-01 15:40:05.064: D/dalvikvm(712): GC_FOR_ALLOC freed 451K, 23% free 9135K/11719K, paused 97ms, total 97ms
11-01 15:40:05.204: D/dalvikvm(712): GC_CONCURRENT freed 451K, 23% free 9135K/11719K, paused 76ms+6ms, total 136ms
11-01 15:40:05.204: D/dalvikvm(712): WAIT_FOR_CONCURRENT_GC blocked 47ms
11-01 15:40:05.293: D/dalvikvm(712): GC_FOR_ALLOC freed 451K, 23% free 9135K/11719K, paused 66ms, total 66ms
11-01 15:40:05.443: D/dalvikvm(712): GC_CONCURRENT freed 451K, 23% free 9135K/11719K, paused 76ms+6ms, total 148ms
11-01 15:40:05.443: D/dalvikvm(712): WAIT_FOR_CONCURRENT_GC blocked 67ms
11-01 15:40:05.513: D/dalvikvm(712): GC_FOR_ALLOC freed 451K, 23% free 9135K/11719K, paused 59ms, total 59ms
11-01 15:40:05.643: D/dalvikvm(712): GC_CONCURRENT freed 451K, 23% free 9135K/11719K, paused 76ms+7ms, total 127ms
11-01 15:40:05.643: D/dalvikvm(712): WAIT_FOR_CONCURRENT_GC blocked 41ms
11-01 15:40:05.703: D/dalvikvm(712): GC_FOR_ALLOC freed 451K, 23% free 9135K/11719K, paused 56ms, total 56ms
11-01 15:40:05.833: D/dalvikvm(712): GC_CONCURRENT freed 451K, 23% free 9135K/11719K, paused 75ms+7ms, total 127ms
11-01 15:40:05.833: D/dalvikvm(712): WAIT_FOR_CONCURRENT_GC blocked 42ms
11-01 15:40:05.913: D/dalvikvm(712): GC_FOR_ALLOC freed 451K, 23% free 9135K/11719K, paused 63ms, total 63ms
11-01 15:40:06.114: D/dalvikvm(712): GC_CONCURRENT freed 451K, 23% free 9135K/11719K, paused 76ms+39ms, total 196ms
11-01 15:40:06.114: D/dalvikvm(712): WAIT_FOR_CONCURRENT_GC blocked 118ms
11-01 15:40:06.963: E/Trace(734): error opening trace file: No such file or directory (2)
11-01 15:40:07.953: D/gralloc_goldfish(734): Emulator without GPU emulation detected.

这是我的活动:

package com.float.rezepte;

import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.float.utilities.ImageProcessing;
import com.float.utilities.Utilities;

public class AddRecipeActivity extends SherlockActivity {

private EditText editTextName;
private EditText editTextDescription;
private EditText editTextDuration;
private EditText editTextPortions;
private EditText editTextPortionsUnit;
private RatingBar ratingBarDifficutly;
private ImageView imageViewPicture;
private static final int SELECT_PICTURE = 1;
private static final int TAKE_PICTURE = 2;
public boolean deletePicture = false;
private String selectedImagePath;
private byte[] recipePicture = null;
private Recipe recipe;
private ProgressDialog dialog;
private IngredientAdapter ingredientAdapter;
private ImageButton imageButtonAddIngredient;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_recipe);
    ConnectView();
    ArrayList<Ingredient> values;

    if (savedInstanceState != null)
    {
        recipe = savedInstanceState.getParcelable("recipe");
        editTextName.setText(recipe.getName());
        editTextDescription.setText(recipe.getDescription());
        editTextDuration.setText(String.valueOf(recipe.getDuration()));
        editTextPortions.setText(String.valueOf(recipe.getPortions()));
        editTextPortionsUnit.setText(recipe.getPortionsUnit());
        ratingBarDifficutly.setRating(recipe.getDifficulty());
        values = (ArrayList<Ingredient>) recipe.getIngredientList();
    }else{
        recipe = new Recipe();
        values = new ArrayList<Ingredient>();
    }

    ingredientAdapter = new IngredientAdapter(this, values); 
    final ListView listViewIngredients = (ListView) findViewById(R.id.listViewIngredients);
    listViewIngredients.setAdapter(ingredientAdapter);

    imageButtonAddIngredient.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            ingredientAdapter.addItem(new Ingredient());
            ingredientAdapter.notifyDataSetChanged();
            Utilities.setListViewHeightBasedOnChildren(listViewIngredients);
        }
    });

    final ViewTreeObserver observer = imageViewPicture.getViewTreeObserver();
       observer.addOnGlobalLayoutListener (new OnGlobalLayoutListener () {
            @Override
            public void onGlobalLayout() {          
                imageViewPicture.getViewTreeObserver().addOnGlobalLayoutListener(this);
                if(recipe.getPicture() != null){
                    imageViewPicture.setImageBitmap(ImageProcessing.scaleImage(recipe.getPicture(), imageViewPicture.getWidth(), imageViewPicture.getHeight()));
                }
            }
        });
}

private void ConnectView(){
    editTextName = (EditText) findViewById(R.id.editTextName);
    editTextDescription = (EditText) findViewById(R.id.editTextDescription);
    editTextDuration = (EditText) findViewById(R.id.editTextDuration);
    editTextPortions = (EditText) findViewById(R.id.editTextPortions);
    editTextPortionsUnit = (EditText) findViewById(R.id.editTextPortionsUnit);
    ratingBarDifficutly = (RatingBar) findViewById(R.id.ratingBarDifficulty);
    imageViewPicture = (ImageView) findViewById(R.id.imageViewPicture);
    imageButtonAddIngredient = (ImageButton) findViewById(R.id.imageButtonAddIngredient);
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
  savedInstanceState.putParcelable("recipe", copyUserInputToRecipe());
}

private Recipe copyUserInputToRecipe(){
    recipe.setName(editTextName.getText().toString());
    recipe.setDescription(editTextDescription.getText().toString());
    recipe.setDifficulty(ratingBarDifficutly.getRating());
    if(!editTextDuration.getText().toString().equals("")){
        recipe.setDuration(Integer.parseInt(editTextDuration.getText().toString()));
    }
    if(!editTextPortions.getText().toString().equals("")){
        recipe.setPortions(Integer.parseInt(editTextPortions.getText().toString()));
    }
    recipe.setPortionsUnit(editTextPortionsUnit.getText().toString());
    if(recipePicture != null){
        recipe.setPicture(recipePicture);
    }
    return recipe;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.activity_add_recipe, menu);
    return true;
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(android.R.anim.fade_in,
            android.R.anim.fade_out);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_cancel_recipe:
        finish();
        break;
    case R.id.menu_save_recipe:
            Intent returnIntent = new Intent();
            returnIntent.putExtra("recipe", recipe);
            setResult(RESULT_OK, returnIntent);
            finish();

        break;
    }
    return super.onOptionsItemSelected(item);
}
}

编辑: ViewTreeObserver 是问题所在。删除它后,没有冻结出现。

4

4 回答 4

1

两件事情:

  1. 您在适配器中的 addItem 方法的实现不是线程保存。使其同步。您的 getView 方法中还有一个 data.remove(...) 调用 - 再次以线程不安全的方式完成。

  2. 当相应的文本更改时,您会修改您的成分对象。在您的成分类的实现中,您是否还更改了关联的 EditText 组件的内容?如果是这样,您将进入一个无限循环,每次调用 editText.setText(...) 时都会调用 TextWatcher。SGS 3 是一个四核 CPU 设备,因此它可以实时处理多达 4 个线程(一个是永久用于 UI,所以留下另外 3 个)。

解决这些问题,看看你是否能抓住局势的尾巴。对于 SGS 3 来说,操作的重量应该不是什么大问题——它有足够的能量来应对。

希望这可以帮助。

于 2012-11-01T23:25:12.570 回答
0

如果我不得不猜测,似乎您的适配器方法中有文本观察器类,更重要的是getView(). 所以,我假设它正在为每一行制作课程,并一直在同时聆听每一行。根据您拥有的行数,如果文本观察者天生就可能会占用大量资源。你应该找到一种不同的方法来实现它。也许在您的适配器之外进行设置(为了安全起见),或者一些构造函数工作,或者让适配器以某种方式实现它。或者我会做什么 - 只听所选行中的视图。并在未选中时关闭。值得一试。

编辑:有很多方法。但我认为最简单的方法BaseAdapter是在您想要收听的每件事上设置一个标签, position然后onFocusChanged我想(如果OnClick给出问题),如果标签与位置匹配,则执行您的操作。但是,如果没有这个,如果它表现良好,那么所有这些可能都是不必要的。据我们所知,监听器方法已经被很好地优化了。只是当时想不出别的解释。

于 2012-11-01T22:39:49.313 回答
0

因此,您存储图像字节以在设备方向更改时重用...

您可以通过覆盖来检测设备方向的变化

    onConfigurationChanged(Configuration newConfig) 

活动中的方法。阅读 http://developer.android.com/reference/android/app/Activity.html#onConfigurationChanged(android.content.res.Configuration )

http://developer.android.com/reference/android/content/res/Configuration.html

确保指定

    android:configChanges="orientation" 

在该活动的清单条目中。有了这个,您可以检测设备是纵向还是横向。最重要的是,这只会在设备方向改变时触发(不像全局布局监听器,它会随着屏幕上的每一个细微变化而触发)。

希望这会有所帮助。

于 2012-11-13T02:34:23.533 回答
0

如果您在主 ui 中进行更新,或者通过线程进行更新,或者在主 ui 线程上授予网络策略权限,这些事情就会发生。

唯一的方法是使用 AsyncTask。通过这个和在 asynctask 设置适配器视图等结束时获取您的数据。

于 2017-08-16T07:39:21.440 回答