2015-07-10 96 views
-1

我的MainActivity:我的应用程序已停止工作?

public class MainActivity extends FragmentActivity implements ActionBar.TabListener { 
    private ViewPager viewPager; 
    private ActionBar actionBar; 
    private TabsFragmentPagerAdapter tabsAdapter; 
    private String[] days = new String[]{"Monday","Tuesday"}; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     viewPager = (ViewPager) findViewById(R.id.viewPager); 
     tabsAdapter = new TabsFragmentPagerAdapter(getSupportFragmentManager()); 
     viewPager.setAdapter(tabsAdapter); 
     actionBar = getActionBar(); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 
     for(int i=0; i<3; i++){ 
      actionBar.addTab(actionBar.newTab().setText(days[i]).setTabListener(this)); 
     } 
     viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { 

      @Override 
      public void onPageSelected(int arg) { 
       // TODO Auto-generated method stub 
       actionBar.setSelectedNavigationItem(arg); 
      } 

      @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 

      } 
     }); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

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

    } 

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

    @Override 
    public void onTabUnselected(Tab arg0, FragmentTransaction arg1) { 
     // TODO Auto-generated method stub 
} 

我的清单:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.muhammad_taha.tablelayout" > 

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

而我下面的代码:

public class MondayFragment extends Fragment { 
     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      // TODO Auto-generated method stub 
      View view = inflater.inflate(R.layout.fragment_monday, container, false); 
      return view; 
     } 

    } 

而下一个:

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class TuesdayFragment extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     View view = inflater.inflate(R.layout.fragment_tuesday, container, false); 
     return view; 
    } 

} 

而对于堆栈跟踪排序的:

import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentPagerAdapter; 

public class TabsFragmentPagerAdapter extends FragmentPagerAdapter { 

    public TabsFragmentPagerAdapter(FragmentManager fm) { 
     super(fm); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public Fragment getItem(int index) { 
     // TODO Auto-generated method stub 
     if(index == 0) 
      return new MondayFragment(); 
     if(index == 1) 
      return new TuesdayFragment(); 


     return null; 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return 3; 
    } 

} 

我的XML文件,第一页显示:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="#00fff8" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="Monday" 
     android:textSize="40dp" 
     android:gravity="center" /> 

</LinearLayout> 

我的XML文件显示第二个选项卡:年底

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="#59ff85" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="Tuesday" 
     android:textSize="40dp" 
     android:gravity="center" /> 

</LinearLayout> 

嗯,我想告诉你,我坚持那从3天开始。请更正此代码。 “TabListener”界面不可用。 但通过忽略它,并在几毫秒后运行我的应用程序,然后bhoooo“不幸的是你的应用程序已停止工作”............ 请帮助我.....并且写出正确的代码我..........现在大脑完成了......而我完成了。我真的不知道后台发生了什么。我想帮助解决这个问题。如果我知道什么是问题,我可以自己解决。如果您知道解决方案,我需要帮助来发现问题。如果你需要其他文件告诉我,我会加载它。 但帮助我。

+4

嗨,它真的很难在这里得到一些帮助,如果你发布这样一个问题...大写它想大喊一声:和犯规很好地解释你的问题...... –

+0

对不起......关于那个 –

+0

我是新人。对不起..... –

回答

1

您的getCount实施错误。 getItem是基于0的,当位置是2时,它返回null,但它不能返回null。更改

@Override 
public int getCount() { 
    return 3; 
} 

@Override 
public int getCount() { 
    return 2; 
} 
+0

感谢您的纠正,但仍然存在同样的问题..... –

+1

发布异常的栈跟踪 – Blackbelt

+0

对不起!没有得到..... –