选项卡式android应用程序在转到另一个选项卡(android->;java->;android studio)时崩溃
我用android开发的预览应用程序声明,当转到另一个选项卡时,该应用程序在模拟器中崩溃。 以下是日志输出:选项卡式android应用程序在转到另一个选项卡(android->;java->;android studio)时崩溃,java,android,android-fragments,crash,tabbed,Java,Android,Android Fragments,Crash,Tabbed,我用android开发的预览应用程序声明,当转到另一个选项卡时,该应用程序在模拟器中崩溃。 以下是日志输出: 01-04 11:02:19.103 2495-2495/com.example.android.effectivenavigation D/AndroidRuntime﹕ Shutting down VM 01-04 11:02:19.103 2495-2495/com.example.android.effectivenavigation W/dalvi
01-04 11:02:19.103 2495-2495/com.example.android.effectivenavigation D/AndroidRuntime﹕ Shutting down VM
01-04 11:02:19.103 2495-2495/com.example.android.effectivenavigation W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb0ccfb20)
01-04 11:02:19.103 2495-2495/com.example.android.effectivenavigation E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.android.effectivenavigation, PID: 2495
java.lang.NullPointerException
at com.example.android.effectivenavigation.MainActivity$AppSectionsPagerAdapter$DummySectionFragment.onCreateView(MainActivity.java:194)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:555)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:495)
at com.example.android.effectivenavigation.MainActivity.onTabSelected(MainActivity.java:100)
at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:577)
at com.android.internal.app.ActionBarImpl$TabImpl.select(ActionBarImpl.java:1105)
at com.android.internal.widget.ScrollingTabContainerView$TabClickListener.onClick(ScrollingTabContainerView.java:547)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
下面是该应用程序的主要活动
/*
* Copyright 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.effectivenavigation;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements ActionBar.TabListener {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the
* three primary sections of the app. We use a {@link android.support.v4.app.FragmentPagerAdapter}
* derivative, which will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
AppSectionsPagerAdapter mAppSectionsPagerAdapter;
/**
* The {@link ViewPager} that will display the three primary sections of the app, one at a
* time.
*/
ViewPager mViewPager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three primary sections
// of the app.
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
// Set up the action bar.
final ActionBar actionBar = getActionBar();
// Specify that the Home/Up button should not be enabled, since there is no hierarchical
// parent.
actionBar.setHomeButtonEnabled(false);
// Specify that we will be displaying tabs in the action bar.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set up the ViewPager, attaching the adapter and setting up a listener for when the
// user swipes between sections.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// When swiping between different app sections, select the corresponding tab.
// We can also use ActionBar.Tab#select() to do this if we have a reference to the
// Tab.
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by the adapter.
// Also specify this Activity object, which implements the TabListener interface, as the
// listener for when this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mAppSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
* sections of the app.
*/
public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
// The first section of the app is the most interesting -- it offers
// a launchpad into the other demonstrations in this example application.
return new slaapkamer();
case 1:
return new woonkamer();
default:
// The other sections of the app are dummy placeholders.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
fragment.setArguments(args);
return fragment;
}
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
if (position == 0) {
return "slaapkamer";
} else if (position == 1) {
return "woonkamer";
} else {
return "geavanceerd";
}
}
/**
* A fragment that launches other parts of the demo application.
*/
public static class slaapkamer extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_section_launchpad, container, false);
//place the shit that should happen
return rootView;
}
}
public static class woonkamer extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_section_launchpad2, container, false);
//place the shit that should happen
return rootView;
}
}
/**
* A dummy fragment representing a section of the app, but that simply displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_section_dummy, container, false);
Bundle args = getArguments();
((TextView) rootView.findViewById(android.R.id.text1)).setText(
getString(R.string.dummy_section_text, args.getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
}
}
/*
*版权所有2012安卓开源项目
*
*根据Apache许可证2.0版(以下简称“许可证”)获得许可;
*除非遵守许可证,否则不得使用此文件。
*您可以通过以下方式获得许可证副本:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*除非适用法律要求或书面同意,软件
*根据许可证进行的分发是按“原样”进行分发的,
*无任何明示或暗示的保证或条件。
*请参阅许可证以了解管理权限和权限的特定语言
*许可证下的限制。
*/
包com.example.android.effectivenavigation;
导入android.app.ActionBar;
导入android.app.FragmentTransaction;
导入android.content.Intent;
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.support.v4.app.FragmentActivity;
导入android.support.v4.app.FragmentManager;
导入android.support.v4.app.FragmentPagerAdapter;
导入android.support.v4.view.ViewPager;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.TextView;
公共类MainActivity扩展FragmentActivity实现ActionBar.TabListener{
/**
*{@link android.support.v4.view.PagerAdapter},它将为每个
*应用程序的三个主要部分。我们使用{@linkandroid.support.v4.app.FragmentPagerAdapter}
*派生,它将把每个加载的片段都保存在内存中。如果这变得太内存化
*因此,最好切换到{@linkandroid.support.v4.app.FragmentStatePagerAdapter}。
*/
应用分区SPAGERAAdapter映射分区SPAGERAAdapter;
/**
*{@link ViewPager}将显示应用程序的三个主要部分,一次一个
*时间。
*/
ViewPager mViewPager;
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建适配器,该适配器将为三个主要部分中的每个部分返回一个片段
//应用程序的名称。
mAppSectionsPagerAdapter=新的AppSectionsPagerAdapter(getSupportFragmentManager());
//设置操作栏。
最终ActionBar ActionBar=getActionBar();
//指定不应启用Home/Up按钮,因为没有层次结构
//家长。
actionBar.setHomeButtonEnabled(假);
//指定我们将在操作栏中显示选项卡。
actionBar.setNavigationMode(actionBar.NAVIGATION\u MODE\u选项卡);
//设置ViewPager,连接适配器并设置侦听器,以便
//用户在分区之间滑动。
mViewPager=(ViewPager)findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(新的ViewPager.SimpleOnPageChangeListener(){
@凌驾
已选择页面上的公共无效(内部位置){
//在不同的应用程序分区之间滑动时,请选择相应的选项卡。
//如果有对
//标签。
actionBar.setSelectedNavigationItem(位置);
}
});
//对于应用程序中的每个部分,在操作栏中添加一个选项卡。
对于(int i=0;i/**
* A dummy fragment representing a section of the app, but that simply displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_section_dummy, container, false);
Bundle args = getArguments();
if (args != null) {
((TextView) rootView.findViewById(R.id.text1)).setText(
getString(R.string.dummy_section_text, args.getInt(ARG_SECTION_NUMBER)));
}
return rootView;
}
}