这是我的第一个使用片段和页面滑动的应用。我正在尝试使用按钮和编辑文本创建三个布局。用户将能够在页面之间滑动。应该没有标签。问题是当我将应用程序上传到手机时,它会崩溃。我经历了一些教程并应用了相同的东西,除了他们没有添加按钮或编辑文本。在下面的代码中,我只包含了第一个片段,因为所有其他片段都是相似的。
主要活动Java:
package com.example.ibm.exoskeleton_2;
import com.example.ibm.exoskeleton_2.TabsPagerAdapter;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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 MainActivity 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);
// 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) {
}
}
Android Manifist:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ibm.exoskeleton_2" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
First Fragment:
package com.example.ibm.exoskeleton_2;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* Created by IBM on 07-Apr-15.
*/
public class MainPage extends Fragment {
Button start1,start2,start3;
Button play1,play2,play3;
Button stop1,stop2,stop3;
Button tour;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main_page, container, false);
start1 = (Button) rootView.findViewById(R.id.Start1Button);
start2 = (Button) rootView.findViewById(R.id.Start2Button);
start3 = (Button) rootView.findViewById(R.id.Start3Button);
play1 = (Button) rootView.findViewById(R.id.Video1Button);
play2 = (Button) rootView.findViewById(R.id.Video2Button);
play3 = (Button) rootView.findViewById(R.id.Video3Button);
stop1 = (Button) rootView.findViewById(R.id.Stop1Button);
stop2 = (Button) rootView.findViewById(R.id.Stop2Button);
stop3 = (Button) rootView.findViewById(R.id.Stop3Button);
tour = (Button) rootView.findViewById(R.id.TourButton);
return rootView;
}
public class ControllerFragment extends Fragment implements View.OnClickListener {
@Override
public void onClick(View v) {
}
}
}
First Fragment XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Welcome"
android:layout_marginTop="20dp"
android:textSize="60dp"
android:id="@+id/textView2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text ="Tour"
android:id="@+id/TourButton"
android:layout_weight="0.10"
android:textSize="30dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text ="Exercise 1"
android:textSize="35dp"
android:layout_margin="15dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.16">
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/Video1Button"
android:text= "Play"
android:layout_weight ="1"
android:textSize="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/Start1Button"
android:text= "Start"
android:layout_weight ="1"
android:textSize="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/Stop1Button"
android:text= "Stop"
android:layout_weight ="1"
android:textSize="20dp"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exercise 2"
android:textSize="35dp"
android:layout_margin="15dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.19">
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/Video2Button"
android:text= "Play"
android:layout_weight ="1"
android:textSize="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/Start2Button"
android:text= "Start"
android:layout_weight ="1"
android:textSize="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/Stop2Button"
android:text= "Stop"
android:layout_weight ="1"
android:textSize="20dp"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exercise 3"
android:textSize="35dp"
android:layout_margin="15dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.17">
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/Video3Button"
android:text= "Play"
android:layout_weight ="1"
android:textSize="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/Start3Button"
android:text= "Start"
android:layout_weight ="1"
android:textSize="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/Stop3Button"
android:text= "Stop"
android:layout_weight ="1"
android:textSize="20dp"/>
</LinearLayout>
</LinearLayout>
标签页适配器:
package com.example.ibm.exoskeleton_2;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new ExtraExercise();
case 1:
return new MainPage();
case 2:
return new OnYourOwn();
}
return null;
}
@Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
}
Log Cat:
04-07 18:56:09.588 29377-29377/com.example.ibm.exoskeleton_2 I/Process﹕ Sending signal. PID: 29377 SIG: 9
04-07 18:57:44.871 30095-30095/com.example.ibm.exoskeleton_2 I/SELinux﹕ Function: selinux_android_load_priority [0], There is no sepolicy file.
04-07 18:57:44.901 30095-30095/com.example.ibm.exoskeleton_2 I/SELinux﹕ Function: selinux_android_load_priority , spota verifySig and checkHash pass. priority version is VE=SEPF_GT-I9505_4.4.2_0040
04-07 18:57:44.901 30095-30095/com.example.ibm.exoskeleton_2 I/SELinux﹕ selinux_android_seapp_context_reload: seapp_contexts file is loaded from /data/security/spota/seapp_contexts
04-07 18:57:44.901 30095-30095/com.example.ibm.exoskeleton_2 E/dalvikvm﹕ >>>>> Normal User
04-07 18:57:44.901 30095-30095/com.example.ibm.exoskeleton_2 E/dalvikvm﹕ >>>>> com.example.ibm.exoskeleton_2 [ userId:0 | appId:10253 ]
04-07 18:57:44.901 30095-30095/com.example.ibm.exoskeleton_2 D/dalvikvm﹕ Late-enabling CheckJNI
04-07 18:57:45.001 30095-30095/com.example.ibm.exoskeleton_2 W/ApplicationPackageManager﹕ getCSCPackageItemText()
04-07 18:57:45.011 30095-30095/com.example.ibm.exoskeleton_2 I/PersonaManager﹕ getPersonaService() name persona_policy
04-07 18:57:45.051 30095-30095/com.example.ibm.exoskeleton_2 D/AndroidRuntime﹕ Shutting down VM
04-07 18:57:45.051 30095-30095/com.example.ibm.exoskeleton_2 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x418c1da0)
04-07 18:57:45.051 30095-30095/com.example.ibm.exoskeleton_2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.ibm.exoskeleton_2, PID: 30095
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ibm.exoskeleton_2/com.example.ibm.exoskeleton_2.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.ibm.exoskeleton_2.MainActivity.onCreate(MainActivity.java:34)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
04-07 18:57:46.522 30095-30095/com.example.ibm.exoskeleton_2 I/Process﹕ Sending signal. PID: 30095 SIG: 9
04-07 19:34:52.119 356-356/com.example.ibm.exoskeleton_2 D/AndroidRuntime﹕ Shutting down VM
04-07 19:34:52.119 356-356/com.example.ibm.exoskeleton_2 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x418c1da0)
04-07 19:34:52.119 356-356/com.example.ibm.exoskeleton_2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.ibm.exoskeleton_2, PID: 356
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ibm.exoskeleton_2/com.example.ibm.exoskeleton_2.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.ibm.exoskeleton_2.MainActivity.onCreate(MainActivity.java:34)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
04-07 19:42:48.253 1897-1897/com.example.ibm.exoskeleton_2 W/ApplicationPackageManager﹕ getCSCPackageItemText()
04-07 19:42:48.253 1897-1897/com.example.ibm.exoskeleton_2 I/PersonaManager﹕ getPersonaService() name persona_policy
04-07 19:42:48.303 1897-1897/com.example.ibm.exoskeleton_2 D/AndroidRuntime﹕ Shutting down VM
04-07 19:42:48.303 1897-1897/com.example.ibm.exoskeleton_2 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x418c1da0)
04-07 19:42:48.303 1897-1897/com.example.ibm.exoskeleton_2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.ibm.exoskeleton_2, PID: 1897
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ibm.exoskeleton_2/com.example.ibm.exoskeleton_2.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.ibm.exoskeleton_2.MainActivity.onCreate(MainActivity.java:34)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
如果您将FragmentActivity
替换为ActionBarActivity
并使用getSupportActionBar()
代替getActionBar()
来解决问题。