这是一个制作Android应用程序的程序,其中有一些动作按钮和三个导航标签。不幸的是,应用程序每次都会关闭。
这是我的LogCat
09-18 00:15:40.157: D/AndroidRuntime(634): Shutting down VM
09-18 00:15:40.157: W/dalvikvm(634): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
09-18 00:15:40.187: E/AndroidRuntime(634): FATAL EXCEPTION: main
09-18 00:15:40.187: E/AndroidRuntime(634): java.lang.NullPointerException
09-18 00:15:40.187: E/AndroidRuntime(634): at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:394)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:389)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.support.v4.view.ViewPager.populate(ViewPager.java:982)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.view.View.measure(View.java:15172)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.view.View.measure(View.java:15172)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.view.View.measure(View.java:15172)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.view.View.measure(View.java:15172)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-18 00:15:40.187: E/AndroidRuntime(634): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.view.View.measure(View.java:15172)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.view.Choreographer.doFrame(Choreographer.java:525)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.os.Handler.handleCallback(Handler.java:615)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.os.Handler.dispatchMessage(Handler.java:92)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.os.Looper.loop(Looper.java:137)
09-18 00:15:40.187: E/AndroidRuntime(634): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-18 00:15:40.187: E/AndroidRuntime(634): at java.lang.reflect.Method.invokeNative(Native Method)
09-18 00:15:40.187: E/AndroidRuntime(634): at java.lang.reflect.Method.invoke(Method.java:511)
09-18 00:15:40.187: E/AndroidRuntime(634): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-18 00:15:40.187: E/AndroidRuntime(634): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-18 00:15:40.187: E/AndroidRuntime(634): at dalvik.system.NativeStart.main(Native Method)
09-18 00:15:44.757: I/Process(634): Sending signal. PID: 634 SIG: 9
这是MainActivity:
package info.androidhive.actionbar;
import info.androidhive.tabsswipe.adapter.TabsPagerAdapter;
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.SearchView;
// @SuppressLint("NewApi")
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
// action bar
private ActionBar actionBar;
// Refresh menu item
private MenuItem refreshMenuItem;
// Tab titles
private String[] tabs = { "Home", "Genre", "Mood" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar = getActionBar();
// Hide the action bar title
actionBar.setDisplayShowTitleEnabled(false);
// Initialization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
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));
}
/**
* 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 boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
// Associate search-able configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
/**
* On selecting action bar icons
* */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_search:
// search action
return true;
case R.id.action_favourite:
// load favourite
Fav();
return true;
case R.id.action_refresh:
// refresh
refreshMenuItem = item;
// load the data from server
new SyncData().execute();
return true;
case R.id.action_help:
// help action
return true;
case R.id.action_check_updates:
// check for updates action
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/**
* Launching new activity
* */
private void Fav() {
Intent i = new Intent(MainActivity.this, Favourite.class);
startActivity(i);
}
/**
* Async task to load the data from server
* **/
private class SyncData extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
// set the progress bar view
refreshMenuItem.setActionView(R.layout.action_progressbar);
// refreshMenuItem.expandActionView();
}
@Override
protected String doInBackground(String... params) {
// not making real request in this demo
// for now we use a timer to wait for sometime
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// refreshMenuItem.collapseActionView();
// remove the progress bar view
refreshMenuItem.setActionView(null);
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@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) {
// TODO Auto-generated method stub
};
}
有一个名为info.androidhive.tabsswipe的包和名为info.androidhive.actionbar的主包
答案 0 :(得分:0)
你在活动资源xml文件中添加了这样的东西吗?
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>