我在Android开发方面没有那么多工作,所以这就是我寻求帮助的原因。我以前通过不同的活动为我的应用程序实现了选项卡,一切正常。
现在或者我想要滑动手势工作我切换到带有片段的标签。这工作正常,直到我的内存不足以及从这些选项卡启动的一些活动。我的MainMenuActivity有什么问题,或者片段或布局文件中存在问题吗?
我使用min API 10并在我的应用程序中实现了android-support-v7-appcompat。
这是我的MainmenuActivity.java
public class MainMenuActivity extends ActionBarActivity implements ActionBar.TabListener{
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Team", "League", "You", "Office" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu_lay);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getSupportActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
// Restore the previously serialized current tab position.
if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
getSupportActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
// Serialize the current tab position.
outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getSupportActionBar()
.getSelectedNavigationIndex());
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
这是我用来从片段中打开新活动的代码。
public void DisplayTraining(){
Intent myIntent = new Intent(getActivity(), TrainingActivity.class);
startActivity(myIntent);
}
LogCat中显示的错误列在下面
02-12 19:48:06.512: E/AndroidRuntime(456): FATAL EXCEPTION: main
02-12 19:48:06.512: E/AndroidRuntime(456): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fc.manager14/com.fc.manager14.StadiumActivity}: android.view.InflateException: Binary XML file line #51: Error inflating class <unknown>
02-12 19:48:06.512: E/AndroidRuntime(456): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-12 19:48:06.512: E/AndroidRuntime(456): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-12 19:48:06.512: E/AndroidRuntime(456): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-12 19:48:06.512: E/AndroidRuntime(456): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-12 19:48:06.512: E/AndroidRuntime(456): at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 19:48:06.512: E/AndroidRuntime(456): at android.os.Looper.loop(Looper.java:123)
02-12 19:48:06.512: E/AndroidRuntime(456): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-12 19:48:06.512: E/AndroidRuntime(456): at java.lang.reflect.Method.invokeNative(Native Method)
02-12 19:48:06.512: E/AndroidRuntime(456): at java.lang.reflect.Method.invoke(Method.java:507)
02-12 19:48:06.512: E/AndroidRuntime(456): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-12 19:48:06.512: E/AndroidRuntime(456): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-12 19:48:06.512: E/AndroidRuntime(456): at dalvik.system.NativeStart.main(Native Method)
02-12 19:48:06.512: E/AndroidRuntime(456): Caused by: android.view.InflateException: Binary XML file line #51: Error inflating class <unknown>
02-12 19:48:06.512: E/AndroidRuntime(456): at android.view.LayoutInflater.createView(LayoutInflater.java:518)
02-12 19:48:06.512: E/AndroidRuntime(456): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
02-12 19:48:06.512: E/AndroidRuntime(456): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
02-12 19:48:06.512: E/AndroidRuntime(456): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
02-12 19:48:06.512: E/AndroidRuntime(456): at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
02-12 19:48:06.512: E/AndroidRuntime(456): at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
02-12 19:48:06.512: E/AndroidRuntime(456): at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
02-12 19:48:06.512: E/AndroidRuntime(456): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
02-12 19:48:06.512: E/AndroidRuntime(456): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
02-12 19:48:06.512: E/AndroidRuntime(456): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
02-12 19:48:06.512: E/AndroidRuntime(456): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
02-12 19:48:06.512: E/AndroidRuntime(456): at android.app.Activity.setContentView(Activity.java:1657)
02-12 19:48:06.512: E/AndroidRuntime(456): at com.fc.manager14.StadiumActivity.onCreate(StadiumActivity.java:37)
02-12 19:48:06.512: E/AndroidRuntime(456): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-12 19:48:06.512: E/AndroidRuntime(456): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-12 19:48:06.512: E/AndroidRuntime(456): ... 11 more
02-12 19:48:06.512: E/AndroidRuntime(456): Caused by: java.lang.reflect.InvocationTargetException
02-12 19:48:06.512: E/AndroidRuntime(456): at java.lang.reflect.Constructor.constructNative(Native Method)
02-12 19:48:06.512: E/AndroidRuntime(456): at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
02-12 19:48:06.512: E/AndroidRuntime(456): at android.view.LayoutInflater.createView(LayoutInflater.java:505)
02-12 19:48:06.512: E/AndroidRuntime(456): ... 25 more
02-12 19:48:06.512: E/AndroidRuntime(456): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
02-12 19:48:06.512: E/AndroidRuntime(456): at android.graphics.Bitmap.nativeCreate(Native Method)
02-12 19:48:06.512: E/AndroidRuntime(456): at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
02-12 19:48:06.512: E/AndroidRuntime(456): at android.graphics.Bitmap.createBitmap(Bitmap.java:444)
02-12 19:48:06.512: E/AndroidRuntime(456): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349)
02-12 19:48:06.512: E/AndroidRuntime(456): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:498)
02-12 19:48:06.512: E/AndroidRuntime(456): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:473)
02-12 19:48:06.512: E/AndroidRuntime(456): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
02-12 19:48:06.512: E/AndroidRuntime(456): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
02-12 19:48:06.512: E/AndroidRuntime(456): at android.content.res.Resources.loadDrawable(Resources.java:1709)
02-12 19:48:06.512: E/AndroidRuntime(456): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
02-12 19:48:06.512: E/AndroidRuntime(456): at android.widget.ImageView.<init>(ImageView.java:118)
02-12 19:48:06.512: E/AndroidRuntime(456): at android.widget.ImageView.<init>(ImageView.java:108)
答案 0 :(得分:0)
您的异常是在setContentView()
指令中生成的,该指令会使您的布局膨胀,之后会出现内存不足错误。这可能是因为你的布局中有一些可绘制的内容需要花费太多的内存来呈现。
如果你说当你使用Activity
代替一切都没问题的话,我会开始删除所有的drawable并开始逐一看看会发生什么,这样你就能看到那里瓶颈是。
但这并不意味着是否只能用于某些drawables尺寸。也许你的Fragment
在它不应该被启动多次时,所以另外加上一些Log.d()
行来控制它,你会发现瓶颈在哪里。