当我在mainactivity中输入viewflipper编码时强制关闭

时间:2017-04-12 15:40:38

标签: java android

public class MainActivity extends AppCompatActivity{

private ViewFlipper viewFlipper;
private Animation fadeIn, fadeOut;

DrawerLayout mDrawerLayout;
NavigationView mNavigationView;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;


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

    viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
    fadeIn = AnimationUtils.loadAnimation(this,R.anim.fade_in) ;
    fadeOut = AnimationUtils.loadAnimation(this,R.anim.fade_out);

    viewFlipper.setInAnimation(fadeIn);
    viewFlipper.setOutAnimation(fadeOut);

    viewFlipper.setAutoStart(true);
    viewFlipper.setFlipInterval(5000);
    viewFlipper.startFlipping();


    /**
     *Setup the DrawerLayout and NavigationView
     */
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;


    /**
     * Lets inflate the very first fragment
     * Here , we are inflating the TabFragment as the first Fragment
     */

    mFragmentManager = getSupportFragmentManager();
    mFragmentTransaction = mFragmentManager.beginTransaction();
    mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
    /**
     * Setup click events on the Navigation View Items.
     */


    mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {



        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            mDrawerLayout.closeDrawers();

            if (menuItem.getItemId() == R.id.nav_item_sent) {
                FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.containerView,new SentFragment()).commit();

            }

            if (menuItem.getItemId() == R.id.nav_item_inbox) {
                FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
                xfragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
            }

            return false;
        }

    });
    /**
     * Setup Drawer Toggle of the Toolbar
     */
    android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
    ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout, toolbar,R.string.app_name,
            R.string.app_name);


    mDrawerLayout.setDrawerListener(mDrawerToggle);

    mDrawerToggle.syncState();
}

**海家伙我无法修复我的代码。当我运行我的应用程序总是强制关闭,因为我把我的viewflipper放在mainactivity上。

就像我的viewflipper片段一样。

对不起英语不好希望你能理解**

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.aksarajawa.kamuselektronikhinggil, PID: 2674
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aksarajawa.kamuselektronikhinggil/com.aksarajawa.kamuselektronikhinggil.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ViewFlipper.setInAnimation(android.view.animation.Animation)' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ViewFlipper.setInAnimation(android.view.animation.Animation)' on a null object reference
                      at com.aksarajawa.kamuselektronikhinggil.MainActivity.onCreate(MainActivity.java:35)
                      at android.app.Activity.performCreate(Activity.java:6662)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6077) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                      at com.android.i

nternal.os.ZygoteInit.main(ZygoteInit.java:755)

0 个答案:

没有答案