正如标题所述,我在尝试切换到新活动时会出现黑屏。奇怪的是我能够切换到不同的(几乎相同的活动)而没有任何问题。 (它正在模拟器上运行。)
我最好的假设是新活动的onCreate()
方法中的某些内容(但是其他活动的onCreate
方法或多或少相同并且工作正常)或者由于某种原因它未能成功尝试一遍又一遍地开始新活动并耗尽内存,但我无法弄清楚是什么或为什么。
这是切换到新活动的代码,第一个嵌套if语句转到新活动没有问题,第二个给出黑屏:
menu.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
//check which menu item was pressed
String group = (String)((PBMMenuItem)parent.getExpandableListAdapter().getGroup(groupPosition)).name;
String child = (String)parent.getExpandableListAdapter().getChild(groupPosition, childPosition);
String addSession = getString(R.string.addsession);
//set up intent
Intent intent;
if(group.equals(getString(R.string.cashgames))) {
if(child.equals(getString(R.string.addsession)))
{
intent = new Intent(v.getContext(), AddCashGame.class);
startActivity(intent);
}
else if (child.equals(getString(R.string.viewsessions)))
{
intent = new Intent(v.getContext(), ViewSessions.class);
startActivity(intent);
}
}
//else if(group.equals(getString(R.string.tournaments)))
//{
//
//}
return true;
}
});
这是无法正常启动的活动的类代码:
public class ViewSessions extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**Fragment managing the behaviors, interactions and presentation of the navigation drawer.*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**Used to store the last screen title. For use in {@link #restoreActionBar()}.*/
private CharSequence mTitle;
private final Context C = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_sessions);
mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));
}
public void openNavDrawer(View view)
{
mNavigationDrawerFragment.getDrawerLayout().openDrawer(Gravity.LEFT);
}
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
.commit();
}
public void onSectionAttached(int number) {
}
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.view_sessions, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_view_sessions, container, false);
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((ViewSessions) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
}
这是活动的XML:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="pokerbankrollmanager.com.pokerbankrollmanager.ViewSessions">
<FrameLayout android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
style="@style/pokertable_main"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical|right">
</RelativeLayout>
<LinearLayout
style="@style/pokertable_menu"
android:orientation="horizontal"
android:layout_width="10dp"
android:layout_height="match_parent"
android:layout_gravity="left|center_vertical"
android:weightSum="1">
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageButton"
android:layout_gravity="center_vertical"
android:src="@drawable/right_facing_arrow_icon"
android:onClick="openNavDrawer"
android:background="@android:color/transparent" />
</LinearLayout>
</FrameLayout>
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
If you're not building against API 17 or higher, use
android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
the container. -->
<fragment android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
android:layout_gravity="start"
android:name="pokerbankrollmanager.com.pokerbankrollmanager.NavigationDrawerFragment"
tools:layout="@layout/fragment_navigation_drawer" />
当我点击切换到新活动并获得黑屏时,我的Logcat会疯狂打印这样的消息:
07-15 21:07:27.092 1511-1522 / pbm.com.pbm I / art:背景部分并发标记扫描GC释放14484(873KB)AllocSpace对象,61(1464KB)LOS对象,5%免费,33MB / 35MB,暂停1.617ms总计109.674ms
07-15 21:07:27.614 1511-1522 / pbm.com.pbm W / art:暂停所有线程:7.923ms
07-15 21:07:27.644 1511-1522 / pbm.com.pbm I / art:背景部分并发标记扫描GC释放8838(541KB)AllocSpace对象,41(984KB)LOS对象,5%免费,34MB / 36MB,暂停11.304ms总计76.280ms
07-15 21:07:31.270 1511-1518 / pbm.com.pbm W / art:暂停所有线程:9.136ms
07-15 21:07:31.782 1511-1518 / pbm.com.pbm W / art:暂停所有线程:20.296ms
答案 0 :(得分:2)
您好yitzih我认为您的XML文件中的错误行是您在此行中提供错误的上下文
tools:context="pokerbankrollmanager.com.pokerbankrollmanager.AddCashGame">
这应该是
tools:context="pokerbankrollmanager.com.pokerbankrollmanager.ViewSessions">`