2014-04-30 45 views
0

我粘貼所有的代碼,因爲我不知道我錯了哪裏了。 該應用程序工作正常,直到我讓//mViewPager.setCurrentItem(tab.getPosition());開始行動,應用程序不會再開始了,我試圖按照開發者網站上的步驟進行操作。對這個問題的解釋也將被讚賞。 謝謝! createActionBar();應用程序崩潰時設置刷卡視圖

mViewPager = (ViewPager) findViewById(R.id.pager); 

package com.basel.ribbit; 

import android.app.ActionBar; 
import android.app.ActionBar.Tab; 
import android.app.FragmentTransaction; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 

import com.parse.ParseAnalytics; 
import com.parse.ParseUser; 

public class MainActivity extends FragmentActivity { 



    /** 
    * The {@link android.support.v4.view.PagerAdapter} that will provide 
    * fragments for each of the sections. We use a {@link 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.v13.app.FragmentStatePagerAdapter}. 
    */ 
    SectionsPagerAdapter mSectionsPagerAdapter; 

    /** 
    * The {@link ViewPager} that will host the section contents. 
    */ 
    ViewPager mViewPager; 
    public static final String TAG = MainActivity.class.getSimpleName(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     //createActionBar(); 

     ParseAnalytics.trackAppOpened(getIntent()); 
     createActionBar(); 

     ParseUser currentUser = ParseUser.getCurrentUser(); 

     if(currentUser == null){ 
      navigateToLogin();}else{ 
       Log.i(TAG,"A NEW USER HAS LOGGED IN " + currentUser.getUsername()); 
      } 


     // Create the adapter that will return a fragment for each of the three 
     // primary sections of the activity. 
     mSectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager()); 

     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.pager); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 
     setPagerOnItemSelected(); 


    } 

    private void setPagerOnItemSelected() { 
     mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 

      @Override 
      public void onPageSelected(int position) { 
       // TODO Auto-generated method stub 
      // getActionBar().setSelectedNavigationItem(position); 

      } 

      @Override 
      public void onPageScrolled(int arg0, float arg1, int arg2) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onPageScrollStateChanged(int arg0) { 
       // TODO Auto-generated method stub 

      } 
     }); 
    } 

private void createActionBar() { 
     final ActionBar actionBar = getActionBar(); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
     ActionBar.TabListener tabListener = new ActionBar.TabListener() { 

      @Override 
      public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void onTabSelected(Tab tab, FragmentTransaction ft) { 
       // TODO Auto-generated method stub 
      //mViewPager.setCurrentItem(tab.getPosition()); 
      } 

      @Override 
      public void onTabReselected(Tab tab, FragmentTransaction ft) { 
       // TODO Auto-generated method stub 

      } 
     }; 
     actionBar.addTab(actionBar.newTab().setText("Hello").setTabListener(tabListener)); 
     actionBar.addTab(actionBar.newTab().setText("Hi").setTabListener(tabListener)); 



    } 

    private void navigateToLogin() { 
     Intent intent = new Intent (this, LoginActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 
     startActivity(intent); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 


    } 


    @Override 
    public boolean onMenuItemSelected(int featureId, MenuItem item) { 
     // TODO Auto-generated method stub 
     int id = item.getItemId(); 
     if(id == R.id.action_logout){ 
      ParseUser.logOut(); 
      navigateToLogin(); 

     } 
     return super.onMenuItemSelected(featureId, item); 


    enter code here 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

} 
+0

請包含日誌貓的堆棧跟蹤。 – PearsonArtPhoto

回答

1

移動此行你崩潰,因爲你沒有初始化mViewPager着呢,你要使用它。如果你等到你設置了mViewPager後,它應該可以工作。

另一種可能性是,您沒有在activity_main.xml的ID爲pager的ViewPager。

+0

就像夢一樣工作!謝謝! –

+0

已經嘗試,但它不會讓我,因爲我沒有足夠的聲譽,我會在適當的時間回到這裏:) –

相關問題