我正在开发一款带有启动画面的Android应用。我想要另一个活动,即在启动画面后不久启动制表选项卡,然后进入主要活动。
当我运行以下代码时,我的启动画面工作正常但是应用程序停止而没有显示制表符或主要活动。我该如何更改代码才能使其正常工作?任何建议都会受到高度重视。
我的主要活动:MainActivity.java
package lk.sidadiya.sidadiya;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = new Intent(MainActivity.this, WelcomeScreen.class);
startActivity(i);
// close this activity
finish();
}
我的标签:WelcomeScreen.java
package lk.sidadiya.sidadiya;
import lk.sidadiya.tabswipe.adapter.TabsPagerAdapter;
import lk.sidadiya.sidadiya.R;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class WelcomeScreen extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Top Rated", "Games", "Movies" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initilization
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 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) {
}
}
我的启动画面:SplashScreen.java
package lk.sidadiya.sidadiya;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class SplashScreen extends Activity {
// Splash screen timer
private static int SPLASH_TIME_OUT = 3000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
@Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="lk.sidadiya.sidadiya"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- Splash screen -->
<activity
android:name="lk.sidadiya.sidadiya.SplashScreen"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Welcome screen -->
<activity
android:name="lk.sidadiya.sidadiya.WelcomeScreen"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black" >
<intent-filter>
<action android:name="lk.sidadiya.sidadiya.WelcomeScreen" />
<!-- <category android:name="android.intent.category.DEFAULT" /> -->
</intent-filter>
</activity>
<!-- Main activity -->
<activity
android:name="lk.sidadiya.sidadiya.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.LAUNCHER" />
</intent-filter>
</activity>
-->
</application>
</manifest>
logcat的:
04-20 12:09:21.327: I/Process(1684): Sending signal. PID: 1684 SIG: 9
04-20 12:27:56.267: D/dalvikvm(1755): GC_FOR_ALLOC freed 54K, 5% free 2808K/2948K, paused 300ms, total 303ms
04-20 12:27:57.737: D/gralloc_goldfish(1755): Emulator without GPU emulation detected.
04-20 12:28:01.567: D/dalvikvm(1755): GC_FOR_ALLOC freed 9K, 3% free 3311K/3404K, paused 230ms, total 231ms
04-20 12:28:02.997: I/Choreographer(1755): Skipped 38 frames! The application may be doing too much work on its main thread.
04-20 12:28:04.037: D/AndroidRuntime(1755): Shutting down VM
04-20 12:28:04.037: W/dalvikvm(1755): threadid=1: thread exiting with uncaught exception (group=0xb2ad3ba8)
04-20 12:28:04.187: E/AndroidRuntime(1755): FATAL EXCEPTION: main
04-20 12:28:04.187: E/AndroidRuntime(1755): Process: lk.sidadiya.sidadiya, PID: 1755
04-20 12:28:04.187: E/AndroidRuntime(1755): java.lang.RuntimeException: Unable to start activity ComponentInfo{lk.sidadiya.sidadiya/lk.sidadiya.sidadiya.WelcomeScreen}: java.lang.NullPointerException
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.os.Handler.dispatchMessage(Handler.java:102)
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.os.Looper.loop(Looper.java:136)
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-20 12:28:04.187: E/AndroidRuntime(1755): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 12:28:04.187: E/AndroidRuntime(1755): at java.lang.reflect.Method.invoke(Method.java:515)
04-20 12:28:04.187: E/AndroidRuntime(1755): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-20 12:28:04.187: E/AndroidRuntime(1755): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-20 12:28:04.187: E/AndroidRuntime(1755): at dalvik.system.NativeStart.main(Native Method)
04-20 12:28:04.187: E/AndroidRuntime(1755): Caused by: java.lang.NullPointerException
04-20 12:28:04.187: E/AndroidRuntime(1755): at lk.sidadiya.sidadiya.WelcomeScreen.onCreate(WelcomeScreen.java:34)
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.app.Activity.performCreate(Activity.java:5231)
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-20 12:28:04.187: E/AndroidRuntime(1755): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-20 12:28:04.187: E/AndroidRuntime(1755): ... 11 more
activity_main.xml中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>