我对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>
logcat的
对不起代码墙,但我真的迷失在这里。我的猜测是它与列表旁边的详细信息帧有关,因为错误指向每个类中的DetailsActivity,它负责设置该帧中的内容。也许我需要清除框架才能将新细节加载到同一个地方?我不知道怎么回事。任何帮助将不胜感激!!!我不希望看到我的第一个应用程序像这样分崩离析,我感到恶心,想着它。
答案 0 :(得分:0)
找出解决方案!也许它不是最漂亮的,但它有效,而且比崩溃更好。
我确定每个listfragment都不喜欢在景观中“共享”内容框架,所以我将每个列表都自己的框架布局放入其中。调用新列表时,其他帧布局的可见性设置为“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>