0

您好 Android 开发者,

活动重新启动后,我面临一些视图的奇怪行为 - 有些视图不是“可见的”,但它们是经过布局的并对触摸动作做出反应。

我试图让应用程序遵循 Android 生命周期指南。我将 Activity 放到后台并让系统关闭我的 Activity。然后我导航回我的活动,该活动被重新创建。要损坏的数据没有问题(每次更改都保存在数据库中),但视图是。有一些正确显示,但TableView内的所有内容(位于ScrollView内)都不可见。如果我在任何未显示的视图上调用getVisibility(),我就会知道它是可见的。正如我上面提到的,视图不是“可见的”,而是对触摸和滚动事件做出反应,就像它们被正确显示一样。

这也很难(不可能)调试,因为当应用程序关闭时,调试器断开连接。但是无论如何,娱乐遵循相同的回调方法- onCreate()onStart()onResume(),所以一旦创建,为什么其他时间会出现问题?对我来说唯一的区别是,新创建 Activity 时onCreate(Bundle)中有一个null Bundle,而系统重新创建它时不为 null。如果活动仅停止(在后台)但没有被系统关闭,则一切正常。

我还尝试覆盖onSaveInstanceState()onRestoreInstanceState()没有任何东西可以保存和恢复(没有调用超级实现),但它没有效果。

我正在使用 Android 4.1.1 和模拟器 2.1。

有人有什么主意吗?

谢谢啤酒

以下是(链接到)屏幕截图,以便更好地理解:
系统关闭
活动之前 重新创建活动之后

以下是布局文件:

    <!-- Header --> 
    <LinearLayout
        android:id="@+id/startlistHeaderLayout" xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        >
        <TextView android:id="@+id/textStartlistName"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:lines="1"
            android:gravity="left"
            android:text=""
            />
        <TextView android:id="@+id/textStarttime"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:lines="1"
            android:gravity="right"
            android:text=""
            android:layout_weight="1"
            />
    </LinearLayout>

    <!-- Results --> 
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@id/resultsScrollView"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:fillViewport="true"
        android:saveEnabled="false"
        >
        <TableLayout
            android:id="@id/timingLapTableLayout"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:stretchColumns="2"
            android:shrinkColumns="2"
            android:saveEnabled="false"
            >
            <!-- rows are added in code -->
        </TableLayout>
    </ScrollView>
</LinearLayout>

<!-- R.layout.timing_row -->
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@id/timingTableRow"
    android:saveEnabled="false"
    >
    <TextView
        android:id="@id/textTimingPosition"
        android:text=""
        android:gravity="right"
        android:paddingLeft="4dip"
        android:saveEnabled="false"
        />
    <TextView
        android:id="@id/textTimingBib"
        android:text=""
        android:gravity="right"
        android:paddingLeft="1dip"
        android:saveEnabled="false"
        />
    <TextView
        android:id="@id/textTimingName"
        android:text=""
        android:lines="1"
        android:ellipsize="end"
        android:gravity="left"
        android:paddingLeft="1dip"
        android:paddingRight="5dip"
        android:saveEnabled="false"
        />
    <TextView
        android:id="@id/textTimingBehind1"
        android:text=""
        android:gravity="right"
        android:paddingRight="5dip"
        android:saveEnabled="false"
        />
    <TextView
        android:id="@id/textTimingBehind2"
        android:text=""
        android:gravity="right"
        android:paddingRight="5dip"
        android:saveEnabled="false"
        />
</TableRow>

这是活动代码的一部分:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(null);
    loadPreferencesTimingTheme();
    if (ownTheme) {
        this.setTheme(theme);
    } else {
        this.setTheme(MainActivity.theme);
    }
}

protected void onStart() {
    super.onStart();
    View lapView;
    View v;
    int sid;
    setContentView(R.layout.startlist_edit);
    sid = getIntent().getIntExtra(TimingActivity.STARTLIST_ID, -1);
    if (sid == -1) {
        Toast.makeText(this, "Sorry, could not load startlist.", Toast.LENGTH_LONG);
        this.finish();
        return;
    }
    tdb = new TimingDB(this);
    sl = tdb.getStartlist(sid); // get Startlist from db into memory
    // hide unnecessary columns
    v = findViewById(R.id.timingLapTableLayout);
    ((TableLayout) v).setColumnCollapsed(0, true);
    loadStartlist(sl); // inflate rows with startlist data and set listeners
    // load the preferences and set preferences listener
    loadPreferences();
    PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(prefsChangeListener);
}

public void onStop() {
    super.onStop();
    PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(prefsChangeListener);
    if (tdb != null) tdb.close();
}

protected void onSaveInstanceState (Bundle outState) {
    // super.onSaveInstanceState(outState);
}

protected void onRestoreInstanceState (Bundle savedInstanceState) {
    // super.onRestoreInstanceState(savedInstanceState);
}

protected void onDestroy() {
    super.onDestroy();
    if (tdb != null) tdb.close();
}

private void loadStartlist(StartList sl) {
    // set startlist name & time
    View v = findViewById(R.id.textStarttime);
    ((TextView) v).setText(SimpleDateFormats.ddmmyyyyhhmmss.format(new Date(sl.getStartTime())));
    v = findViewById(R.id.textStartlistName);
    ((TextView) v).setText(sl.getName());
    // set header onclicklistener
    v = findViewById(R.id.startlistHeaderLayout);
    v.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // show dialog to change startlist name and time
            Bundle args = new Bundle();
            args.putString(KEY_NAME, ((TextView) findViewById(R.id.textStartlistName)).getText().toString());
            args.putString(KEY_STTIME, ((TextView) v.findViewById(R.id.textStarttime)).getText().toString());
            if (Build.VERSION.SDK_INT < 8) {
                dialogBundle = args;
                showDialog(DIALOG_EDIT_STARTLIST);
            } else {
                showDialog(DIALOG_EDIT_STARTLIST, args);
            }
        }
    });
    // add competitors to startlist
    int cnt = sl.getCompetitorsCount();
    for (int i = 0; i < cnt; i++) {
        Competitor c = sl.getCompetitorByIndex(i);
        appendStartlistRow(c);
    }
}

public void appendStartlistRow(final Competitor c) {
    TableRow inflatedView = (TableRow) getLayoutInflater().inflate(R.layout.timing_row, null);
    inflatedView.setTag(TAG_COMPETITOR, c);
    inflatedView.setTag(new Integer(c.getBib()));
    inflatedView.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            TableLayout tl = (TableLayout) findViewById(R.id.timingLapTableLayout);
            Bundle args = new Bundle();
            args.putInt(KEY_INDEX, tl.indexOfChild(v));
            if (Build.VERSION.SDK_INT < 8) {
                dialogBundle = args;
                showDialog(DIALOG_EDIT_COMPETITOR);
            } else {
                showDialog(DIALOG_EDIT_COMPETITOR, args);
            }
        }
    });
    // set up texts
    TextView text = (TextView) inflatedView.findViewById(R.id.textTimingBib);
    text.setText(c.getBib() + "");
    text = (TextView) inflatedView.findViewById(R.id.textTimingName);
    text.setText(c.getName());
    text = (TextView) inflatedView.findViewById(R.id.textTimingBehind1);
    text.setText(getFormatedStartTime(sl, c, Settings.STARTTIME_ABSOLUTE));
    text = (TextView) inflatedView.findViewById(R.id.textTimingBehind2);
    text.setText(getFormatedStartTime(sl, c, Settings.STARTTIME_RELATIVE));
    // append the row
    appendStartlistRow((ViewGroup) inflatedView);
}

public void appendStartlistRow(ViewGroup row) {
    // set background and text colors
    if (((((Integer) row.getTag()).intValue()) % 2) == Defs.VIEW_EVEN) {
        row.setBackgroundColor(Defs.COLOR_BACKGROUND_EVEN);
        for (int j = 0; j < row.getChildCount(); j++) {
            ((TextView) (row.getChildAt(j))).setTextColor(Defs.COLOR_TEXT_EVEN);
        }
    } else {
        row.setBackgroundColor(Defs.COLOR_BACKGROUND_ODD);
        for (int j = 0; j < row.getChildCount(); j++) {
            ((TextView) (row.getChildAt(j))).setTextColor(Defs.COLOR_TEXT_ODD);
        }
    }
    ((TableLayout) findViewById(R.id.timingLapTableLayout)).addView(row);
}

Defs.java:

public class Defs {
    protected static final int VIEW_EVEN = 0;
    protected static final int VIEW_ODD = 1;
    protected static int COLOR_BACKGROUND_EVEN;
    protected static int COLOR_BACKGROUND_ODD;
    protected static int COLOR_BACKGROUND_SELECTED;
    protected static int COLOR_BACKGROUND_SPYED;
    protected static int COLOR_TEXT_ODD;
    protected static int COLOR_TEXT_EVEN;
    protected static int COLOR_TEXT_SELECTED;
    protected static int COLOR_TEXT_SPYED;

    private static boolean isInitialized = false;

    protected static void init(Context c) {
        if (isInitialized) return;
        COLOR_BACKGROUND_EVEN = c.getResources().getColor(R.color.background_darker);
        COLOR_BACKGROUND_ODD = c.getResources().getColor(R.color.background_lighter);
        COLOR_BACKGROUND_SELECTED = c.getResources().getColor(R.color.background_selected);
        COLOR_BACKGROUND_SPYED = c.getResources().getColor(R.color.background_spyed);
        COLOR_TEXT_ODD = c.getResources().getColor(R.color.text_lighter);
        COLOR_TEXT_EVEN = c.getResources().getColor(R.color.text_darker);
        COLOR_TEXT_SELECTED = c.getResources().getColor(R.color.text_selected);
        COLOR_TEXT_SPYED = c.getResources().getColor(R.color.text_spyed);
        isInitialized = true;
    }
}
4

1 回答 1

0

如果没有任何代码,几乎不可能对问题本身发表太​​多看法。但我确实想说(更适合作为评论,但还不能这样做)对于长列表(如你的似乎基于屏幕截图),使用ListView自定义的实际ArrayAdapter可能是一个更好的解决方案。ListView滚动时重用视图,从而减少不必要的视图创建。此外,一旦您学会了如何使用ListView,您可能会发现它比手动添加表格行更容易。如果您不熟悉这些,请在此处查看教程: http ://www.vogella.com/articles/AndroidListView/article.html

而且,如果您决定从 table 更改为 a ListView,那么问题(无论是什么)也可能会消失。你永远不会知道。:)

于 2013-10-28T19:01:40.707 回答