0

我对 Java 非常缺乏经验,并且接近我的第一个 Android 应用程序的完成,当时我最担心的事情发生了,我遇到了一个我不知道如何解释的错误。

该应用程序的基础是从抽屉菜单中选择的 5 个列表。每个列表都会打开带有内容的静态 XML 视图。所有这一切都在肖像中顺利进行。在横向中,视图是一个双窗格,左侧是列表,右侧是内容。到达那里也很好。但是当我在一个列表中进入横向时,我得到一个错误,然后尝试在另一个列表中进入横向。有人可以帮我弄清楚发生了什么吗?

主要活动

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

public class MainActivity extends SherlockFragmentActivity {

    Fragment rotcfraglist = new RotcFragList();
    Fragment afafraglist = new AfaFragList();
    Fragment bmtfraglist = new BmtFragList();
    Fragment docsfraglist = new DocsFragList();
    Fragment extrafraglist = new ExtraFragList();

.... *drawer set up stuff ....

// ListView click listener in the navigation drawer
    private class DrawerItemClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            selectItem(position);
        }
    }

    private void selectItem(int position) {

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

        View fullFrame = findViewById(R.id.full_frame);
        View detailsFrame = findViewById(R.id.details);
        View listFrame = findViewById(R.id.list_frame);

        // Destroy Backstack first
        fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

        // Locate Position
        switch (position) {
        ///ROTC
        case 0:
            ft.remove(docsfraglist);
            ft.remove(extrafraglist);

            if (fullFrame != null && fullFrame.getVisibility() == View.VISIBLE) {
                fullFrame.setVisibility(View.GONE);
            }
            if (detailsFrame != null
                    && detailsFrame.getVisibility() == View.GONE) {
                detailsFrame.setVisibility(View.VISIBLE);
            }
            if (listFrame != null && listFrame.getVisibility() == View.GONE) {
                listFrame.setVisibility(View.VISIBLE);
            }

        ft.replace(R.id.list_frame, rotcfraglist);
            break;
        ///AFA
        case 1:
            ft.remove(docsfraglist);
            ft.remove(extrafraglist);

            if (fullFrame != null && fullFrame.getVisibility() == View.VISIBLE) {
                fullFrame.setVisibility(View.GONE);
            }
            if (detailsFrame != null
                    && detailsFrame.getVisibility() == View.GONE) {
                detailsFrame.setVisibility(View.VISIBLE);
            }
            if (listFrame != null && listFrame.getVisibility() == View.GONE) {
                listFrame.setVisibility(View.VISIBLE);
            }

            ft.replace(R.id.list_frame, afafraglist);
            break;
        ///BMT
        case 2:
            ft.remove(docsfraglist);
            ft.remove(extrafraglist);

            if (fullFrame != null && fullFrame.getVisibility() == View.VISIBLE) {
                fullFrame.setVisibility(View.GONE);
            }
            if (detailsFrame != null
                    && detailsFrame.getVisibility() == View.GONE) {
                detailsFrame.setVisibility(View.VISIBLE);
            }
            if (listFrame != null && listFrame.getVisibility() == View.GONE) {
                listFrame.setVisibility(View.VISIBLE);
            }

            ft.replace(R.id.list_frame, bmtfraglist);
            break;
        ///Docs
        case 3:
            ft.remove(rotcfraglist);
            ft.remove(afafraglist);
            ft.remove(bmtfraglist);

            if (detailsFrame != null
                    && detailsFrame.getVisibility() == View.VISIBLE) {
                detailsFrame.setVisibility(View.GONE);
            }
            if (fullFrame != null && fullFrame.getVisibility() == View.GONE) {
                fullFrame.setVisibility(View.VISIBLE);
            }
            if (listFrame != null && listFrame.getVisibility() == View.VISIBLE) {
                listFrame.setVisibility(View.GONE);
            }

            ft.replace(R.id.full_frame, docsfraglist);
            break;
        ///Extra
        case 4:
            ft.remove(rotcfraglist);
            ft.remove(afafraglist);
            ft.remove(bmtfraglist);

            if (detailsFrame != null
                    && detailsFrame.getVisibility() == View.VISIBLE) {
                detailsFrame.setVisibility(View.GONE);
            }
            if (fullFrame != null && fullFrame.getVisibility() == View.GONE) {
                fullFrame.setVisibility(View.VISIBLE);
            }
            if (listFrame != null && listFrame.getVisibility() == View.VISIBLE) {
                listFrame.setVisibility(View.GONE);
            }

            ft.replace(R.id.full_frame, extrafraglist);
            break;
        }
        ft.commit();
        mDrawerList.setItemChecked(position, true);

        // Get the title followed by the position
        setTitle(title[position]);
        // Close drawer
        mDrawerLayout.closeDrawer(mDrawerList);
    }

列表片段之一

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.app.SherlockListFragment;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class BmtFragList extends SherlockListFragment {

boolean mDualPane;
static int mCurCheckPosition = 0;
public final String [] TITLES = {
    "Core Values",
    "AF Mission",
    "Airman's Creed",
    "Air Force Song",
    "Ranks AF Enlisted",
    "Ranks AF Officer",
    "Chain of Command",
    "Reporting Statement",
    "Phonetic Alphabet"
};

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    setListAdapter(new ArrayAdapter<String>(getActivity(),
            R.layout.simple_list_item_checkable_1, android.R.id.text1, TITLES));

    // Check landscape or not
    View detailsFrame = getActivity().findViewById(R.id.details);
    mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;

    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    if (savedInstanceState != null) {
        mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
        showDetails(mCurCheckPosition);
    }

    if (mDualPane) {
        showDetails(mCurCheckPosition);
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putInt("curChoice", mCurCheckPosition);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    showDetails(position);
}

// If landscape, replace details. If not, replace list.
void showDetails(int position) {

    FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
    mCurCheckPosition = position;

    if (mDualPane) {
        getListView().setItemChecked(position, true);

        BmtDetailsFragment details = (BmtDetailsFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.details);

        if (details == null || details.getShownIndex() != position) {
            details = BmtDetailsFragment.newInstance(position);
            ft.replace(R.id.details, details);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            ft.commit();
        }
    } else {
        getListView().setItemChecked(position, true);

        Intent intent = new Intent();
        intent.setClass(getActivity(), DetailsActivity.class);
        intent.putExtra("position", position);
        startActivity(intent);
    }
}

public static class DetailsActivity extends SherlockFragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        setTheme(MainActivity.getThemeId());

        super.onCreate(savedInstanceState);

        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            finish();
            return;
        }
        if (savedInstanceState == null) {
            BmtDetailsFragment details = new BmtDetailsFragment();
            details.setArguments(getIntent().getExtras());
            getSupportFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
        }
    }
}

public static class BmtDetailsFragment extends SherlockFragment {

    public static BmtDetailsFragment newInstance(int position) {
        BmtDetailsFragment f = new BmtDetailsFragment();

        // Make index input an argument
        Bundle args = new Bundle();
        args.putInt("position", position);
        f.setArguments(args);

        return f;
    }

    public int getShownIndex() {
        return getArguments().getInt("position", 0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        if (container == null) {
        return null;
        }

        View newContent = getView();
        int position = getShownIndex();

        switch (position) {
        case 0:
            newContent = inflater.inflate(R.layout.corevalues, container, false);
            break;
        case 1:
            newContent = inflater.inflate(R.layout.afmission, container, false);
            break;
        case 2:
            newContent = inflater.inflate(R.layout.airmanscreed, container, false);
            break;

    ....more cases.....

        }
        return newContent;
    }
}
}

主要活动景观布局

<android.support.v4.widget.DrawerLayout     xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<FrameLayout
    android:id="@+id/full_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/list_frame"
        android:layout_width="180dp"
        android:layout_height="match_parent" />

    <FrameLayout
        android:id="@+id/details"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_toRightOf="@id/list_frame" >
    </FrameLayout>
</RelativeLayout>

<ListView
    android:id="@+id/listview_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#262626"
    android:choiceMode="singleChoice"
    android:divider="#5C5C5C"
    android:dividerHeight="1dp" />

</android.support.v4.widget.DrawerLayout>

日志猫

在此处输入图像描述

对不起,代码墙,但我真的迷路了。我的猜测是它与列表旁边的 detailsframe 有关,因为错误指向每个类中的 DetailsActivity,该类负责设置该框架中的内容。也许我需要先清除框架,然后才能将包含新细节的新列表加载到同一个地方?我不知道该怎么做。任何帮助将不胜感激!!!我不想看到我的第一个应用程序就这样崩溃了,想想就觉得恶心。

4

1 回答 1

0

想出了解决办法!也许它不是最漂亮的,但它确实有效,而且比崩溃要好。

我确定我不喜欢在横向中“共享”内容框架的每个列表片段,所以我为每个列表设置了它自己的框架布局来放入它的片段。当调用一个新列表时,其他框架布局的可见性设置为“GONE”,并且基本上被置于后台。稍后回调时,他们会显示他们之前持有的内容,这是一个很好的奖励。

这是 MainActivity 中的更改

    // ListView click listener in the navigation drawer
    private class DrawerItemClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            selectItem(position);
        }
    }

    private void selectItem(int position) {

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

        View fullFrame = findViewById(R.id.full_frame);
        View detailsFrameRotc = findViewById(R.id.details_rotc);
        View listFrame = findViewById(R.id.list_frame);
        View detailsFrameAfa = findViewById(R.id.details_afa);
        View detailsFrameBmt = findViewById(R.id.details_bmt);

        // Destroy Backstack first
        fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

        // Locate Position
        switch (position) {
        ///ROTC
        case 0:
            ft.replace(R.id.list_frame, rotcfraglist);
            ft.remove(docsfraglist);
            ft.remove(extrafraglist);

            if (fullFrame != null && fullFrame.getVisibility() == View.VISIBLE) {
                fullFrame.setVisibility(View.GONE);
            }
            if (detailsFrameAfa != null
                    && detailsFrameAfa.getVisibility() == View.VISIBLE) {
                detailsFrameAfa.setVisibility(View.GONE);
            }
            if (detailsFrameBmt != null
                    && detailsFrameBmt.getVisibility() == View.VISIBLE) {
                detailsFrameBmt.setVisibility(View.GONE);
            }
            if (detailsFrameRotc != null
                    && detailsFrameRotc.getVisibility() == View.GONE) {
                detailsFrameRotc.setVisibility(View.VISIBLE);
            }
            if (listFrame != null && listFrame.getVisibility() == View.GONE) {
                listFrame.setVisibility(View.VISIBLE);
            }

            break;
        ///AFA
        case 1:
            ft.replace(R.id.list_frame, afafraglist);
            ft.remove(docsfraglist);
            ft.remove(extrafraglist);

            if (fullFrame != null && fullFrame.getVisibility() == View.VISIBLE) {
                fullFrame.setVisibility(View.GONE);
            }
            if (detailsFrameRotc != null
                    && detailsFrameRotc.getVisibility() == View.VISIBLE) {
                detailsFrameRotc.setVisibility(View.GONE);
            }
            if (detailsFrameBmt != null
                    && detailsFrameBmt.getVisibility() == View.VISIBLE) {
                detailsFrameBmt.setVisibility(View.GONE);
            }
            if (detailsFrameAfa != null
                    && detailsFrameAfa.getVisibility() == View.GONE) {
                detailsFrameAfa.setVisibility(View.VISIBLE);
            }
            if (listFrame != null && listFrame.getVisibility() == View.GONE) {
                listFrame.setVisibility(View.VISIBLE);
            }

            break;
        ///BMT
        case 2:
            ft.replace(R.id.list_frame, bmtfraglist);
            ft.remove(docsfraglist);
            ft.remove(extrafraglist);

            if (fullFrame != null && fullFrame.getVisibility() == View.VISIBLE) {
                fullFrame.setVisibility(View.GONE);
            }
            if (detailsFrameAfa != null
                    && detailsFrameAfa.getVisibility() == View.VISIBLE) {
                detailsFrameAfa.setVisibility(View.GONE);
            }
            if (detailsFrameRotc != null
                    && detailsFrameRotc.getVisibility() == View.VISIBLE) {
                detailsFrameRotc.setVisibility(View.GONE);
            }
            if (detailsFrameBmt != null
                    && detailsFrameBmt.getVisibility() == View.GONE) {
                detailsFrameBmt.setVisibility(View.VISIBLE);
            }
            if (listFrame != null && listFrame.getVisibility() == View.GONE) {
                listFrame.setVisibility(View.VISIBLE);
            }

            break;
        ///Docs
        case 3:
            ft.replace(R.id.full_frame, docsfraglist);
            ft.remove(rotcfraglist);
            ft.remove(afafraglist);
            ft.remove(bmtfraglist);

            if (fullFrame != null && fullFrame.getVisibility() == View.GONE) {
                fullFrame.setVisibility(View.VISIBLE);
            }
            if (listFrame != null && listFrame.getVisibility() == View.VISIBLE) {
                listFrame.setVisibility(View.GONE);
            }
            if (detailsFrameAfa != null
                    && detailsFrameAfa.getVisibility() == View.VISIBLE) {
                detailsFrameAfa.setVisibility(View.GONE);
            }
            if (detailsFrameBmt != null
                    && detailsFrameBmt.getVisibility() == View.VISIBLE) {
                detailsFrameBmt.setVisibility(View.GONE);
            }
            if (detailsFrameRotc != null
                    && detailsFrameRotc.getVisibility() == View.VISIBLE) {
                detailsFrameRotc.setVisibility(View.GONE);
            }

            break;
        ///Extra
        case 4:
            ft.replace(R.id.full_frame, extrafraglist);
            ft.remove(rotcfraglist);
            ft.remove(afafraglist);
            ft.remove(bmtfraglist);

            if (fullFrame != null && fullFrame.getVisibility() == View.GONE) {
                fullFrame.setVisibility(View.VISIBLE);
            }
            if (listFrame != null && listFrame.getVisibility() == View.VISIBLE) {
                listFrame.setVisibility(View.GONE);
            }
            if (detailsFrameAfa != null
                    && detailsFrameAfa.getVisibility() == View.VISIBLE) {
                detailsFrameAfa.setVisibility(View.GONE);
            }
            if (detailsFrameBmt != null
                    && detailsFrameBmt.getVisibility() == View.VISIBLE) {
                detailsFrameBmt.setVisibility(View.GONE);
            }
            if (detailsFrameRotc != null
                    && detailsFrameRotc.getVisibility() == View.VISIBLE) {
                detailsFrameRotc.setVisibility(View.GONE);
            }

            break;
        }
        ft.commit();
        mDrawerList.setItemChecked(position, true);

        // Get the title followed by the position
        setTitle(title[position]);
        // Close drawer
        mDrawerLayout.closeDrawer(mDrawerList);
    }

还有我的横向布局

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<FrameLayout
    android:id="@+id/full_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/list_frame"
        android:layout_width="180dp"
        android:layout_height="match_parent" />

    <FrameLayout
        android:id="@+id/details_rotc"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_toRightOf="@id/list_frame" >
    </FrameLayout>

    <FrameLayout
        android:id="@+id/details_afa"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_toRightOf="@id/list_frame" >
    </FrameLayout>

    <FrameLayout
        android:id="@+id/details_bmt"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_toRightOf="@id/list_frame" >
    </FrameLayout>
</RelativeLayout>

<ListView
    android:id="@+id/listview_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#262626"
    android:choiceMode="singleChoice"
    android:divider="#5C5C5C"
    android:dividerHeight="1dp" />

</android.support.v4.widget.DrawerLayout>
于 2013-10-07T14:41:10.220 回答