2015-04-07 48 views
0

這是我第一個使用分段和頁面刷卡的應用程序。我正嘗試創建三個帶按鈕和編輯文本的佈局。用戶將能夠在頁面之間滑動。應該沒有標籤。問題是當我將應用上傳到手機時,它崩潰了。我經歷了一些教程並應用相同的東西,除了他們沒有添加按鈕或編輯文本。在下面的代碼中,我只包含第一個片段,因爲所有其他片段都是相似的。使用視圖分頁器打開之前刷卡應用程序崩潰 - Android

主要活動的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> 

第一塊碎片:

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) { 

     } 
    } 
} 

第一塊碎片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; 
    } 

} 

登錄貓:

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

崩潰似乎在onCreate中的MainActivity.java的第34行。你可以發佈該文件嗎? – daentech

+0

我的不好,我忘了添加它:D – Kito312

+0

它看起來像你的ActionBar從'getActionBar()'返回null。在使用支持FragmentActivity時,可以使用'getSupportActionBar()'來解決這個問題。你的AppTheme是否指定有一個操作欄? – daentech

回答

0

如果用ActionBarActivity更換FragmentActivity和使用getSupportActionBar()代替getActionBar()應該解決的問題。