2017-03-05 86 views
0

我修改了我的MainActivity代碼,當應用程序啓動時打開導航抽屜。但是那個嘗試沒有奏效,我撤消了所有更改。即使運行應用程序時出現錯誤。如何才能解決這個錯誤?也幫助我在應用程序啓動時打開導航抽屜。獲取我的android工作室項目的運行時錯誤

這是我用於此目的

static final int DRAWER_DELAY = 200; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    ... 
    // Building NavDrawer logic here. Just a method call would be best. 
    ... 
    new Handler().postDelayed(openDrawerRunnable(), DRAWER_DELAY); 
} 

private Runnable openDrawerRunnable() { 
    return new Runnable() { 

     @Override 
     public void run() { 
      drawerLayout.openDrawer(Gravity.LEFT); 
     } 
    } 
} 

代碼這是我的MainActivity代碼

package com.example.mohammedalukkal.ktucses4; 

import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.design.widget.CoordinatorLayout; 
import android.support.design.widget.FloatingActionButton; 
import android.support.v7.widget.CardView; 
import android.text.Layout; 
import android.text.style.BackgroundColorSpan; 
import android.view.View; 
import android.support.design.widget.NavigationView; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.WindowManager; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.FrameLayout; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.app.FragmentTransaction; 

public class MainActivity extends AppCompatActivity 
      implements NavigationView.OnNavigationItemSelectedListener { 
     private static long time; 
     FloatingActionButton fab,fab_univer,fab_drive,fab_web; 
     TextView university,ktuweb,drive; 
     Animation Fabopen,Fabclose,Fabclockwise,Fabanticlockwise,dimon,dimoff; 
     FragmentTransaction fragmentTransaction; 
     boolean isopen = false; 
     ImageView dim; 


     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
      setSupportActionBar(toolbar); 

      fab = (FloatingActionButton) findViewById(R.id.fab); 
      university = (TextView) findViewById(R.id.university); 
      ktuweb = (TextView) findViewById(R.id.ktuweb); 
      drive = (TextView) findViewById(R.id.drive); 
      fab_drive = (FloatingActionButton) findViewById(R.id.fab_drive); 
      fab_web = (FloatingActionButton) findViewById(R.id.fab_web); 
      fab_univer = (FloatingActionButton) findViewById(R.id.fab_univer); 
      Fabopen = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fab_open); 
      Fabclose = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fab_close); 
      Fabclockwise = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate_clockwise); 
      Fabanticlockwise = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate_anticlockwise); 
      dim = (ImageView) findViewById(R.id.dim); 
      dimon = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.dim_on); 
      dimoff = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.dim_off); 

      fab.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        if (isopen) { 
         university.startAnimation(Fabclose); 
         ktuweb.startAnimation(Fabclose); 
         drive.startAnimation(Fabclose); 
         fab.startAnimation(Fabanticlockwise); 
         fab_univer.startAnimation(Fabclose); 
         fab_web.startAnimation(Fabclose); 
         fab_drive.startAnimation(Fabclose); 
         dim.startAnimation(dimoff); 
         dim.setClickable(false); 
         fab_web.setClickable(false); 
         fab_univer.setClickable(false); 
         fab_drive.setClickable(false); 
         isopen = false; 


        } else { 
         university.startAnimation(Fabopen); 
         ktuweb.startAnimation(Fabopen); 
         drive.startAnimation(Fabopen); 
         fab.startAnimation(Fabclockwise); 
         fab_univer.startAnimation(Fabopen); 
         fab_web.startAnimation(Fabopen); 
         fab_drive.startAnimation(Fabopen); 
         dim.startAnimation(dimon); 
         dim.setClickable(true); 
         fab_web.setClickable(true); 
         fab_univer.setClickable(true); 
         fab_drive.setClickable(true); 
         isopen = true; 


        } 

       } 
      }); 

      fab_univer.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 

        startActivity(new Intent(getApplicationContext(),University.class)); 

        if (isopen) 
        { 
         university.startAnimation(Fabclose); 
         ktuweb.startAnimation(Fabclose); 
         drive.startAnimation(Fabclose); 
         fab.startAnimation(Fabanticlockwise); 
         fab_univer.startAnimation(Fabclose); 
         fab_web.startAnimation(Fabclose); 
         fab_drive.startAnimation(Fabclose); 
         dim.startAnimation(dimoff); 
         dim.setClickable(false); 
         fab_web.setClickable(false); 
         fab_univer.setClickable(false); 
         fab_drive.setClickable(false); 
         isopen = false; 
        } 

       } 
      }); 
      fab_web.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        startActivity(new Intent(getApplicationContext(),Ktuweb.class)); 

        if (isopen) 
        { 
         university.startAnimation(Fabclose); 
         ktuweb.startAnimation(Fabclose); 
         drive.startAnimation(Fabclose); 
         fab.startAnimation(Fabanticlockwise); 
         fab_univer.startAnimation(Fabclose); 
         fab_web.startAnimation(Fabclose); 
         fab_drive.startAnimation(Fabclose); 
         dim.startAnimation(dimoff); 
         dim.setClickable(false); 
         fab_web.setClickable(false); 
         fab_univer.setClickable(false); 
         fab_drive.setClickable(false); 
         isopen = false; 
        } 
       } 
      }); 
      fab_drive.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        startActivity(new Intent(getApplicationContext(),Drive.class)); 

        if (isopen) 
        { 
         university.startAnimation(Fabclose); 
         ktuweb.startAnimation(Fabclose); 
         drive.startAnimation(Fabclose); 
         fab.startAnimation(Fabanticlockwise); 
         fab_univer.startAnimation(Fabclose); 
         fab_web.startAnimation(Fabclose); 
         fab_drive.startAnimation(Fabclose); 
         dim.startAnimation(dimoff); 
         dim.setClickable(false); 
         fab_web.setClickable(false); 
         fab_univer.setClickable(false); 
         fab_drive.setClickable(false); 
         isopen = false; 
        } 
       } 
      }); 



      final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
      ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
      drawer.addDrawerListener(toggle); 
      toggle.syncState(); 

      NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
      navigationView.setNavigationItemSelectedListener(this); 

      dim.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        if (isopen) 
        { 
         university.startAnimation(Fabclose); 
         ktuweb.startAnimation(Fabclose); 
         drive.startAnimation(Fabclose); 
         fab.startAnimation(Fabanticlockwise); 
         fab_univer.startAnimation(Fabclose); 
         fab_web.startAnimation(Fabclose); 
         fab_drive.startAnimation(Fabclose); 
         dim.startAnimation(dimoff); 
         dim.setClickable(false); 
         fab_web.setClickable(false); 
         fab_univer.setClickable(false); 
         fab_drive.setClickable(false); 
         isopen = false; 
        } 
       } 

      }); 

     } 

     @Override 
     public void onBackPressed() { 
      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
      if (drawer.isDrawerOpen(GravityCompat.START)) { 
       drawer.closeDrawer(GravityCompat.START); 
      } 
      else if (isopen) { 
       university.startAnimation(Fabclose); 
       ktuweb.startAnimation(Fabclose); 
       drive.startAnimation(Fabclose); 
       fab.startAnimation(Fabanticlockwise); 
       fab_univer.startAnimation(Fabclose); 
       fab_web.startAnimation(Fabclose); 
       fab_drive.startAnimation(Fabclose); 
       dim.startAnimation(dimoff); 
       dim.setClickable(false); 
       fab_web.setClickable(false); 
       fab_univer.setClickable(false); 
       fab_drive.setClickable(false); 
       isopen = false; 
      } 
      else { 
       if (time + 2000 > System.currentTimeMillis()) { 
        super.onBackPressed(); 
       } else { 
        Toast.makeText(MainActivity.this, "Press back again to exit", Toast.LENGTH_SHORT).show(); 
       } 
       time = System.currentTimeMillis(); 
      } 
     } 

     @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; 
     } 





     @SuppressWarnings("StatementWithEmptyBody") 
     @Override 
     public boolean onNavigationItemSelected(MenuItem item) { 
      // Handle navigation view model clicks here. 
      int id = item.getItemId(); 

      if (id == R.id.sub_1) { 

       fragmentTransaction = getFragmentManager().beginTransaction(); 
       fragmentTransaction.replace(R.id.frame,new ma202Fragment()); 
       fragmentTransaction.commit(); 
       getSupportActionBar().setTitle("MA202"); 
       getSupportActionBar().setSubtitle("Probability distributions,Transforms and Numerical Methods"); 


      } else if (id == R.id.sub_2) { 

       fragmentTransaction = getFragmentManager().beginTransaction(); 
       fragmentTransaction.replace(R.id.frame,new cs202Fragment()); 
       fragmentTransaction.commit(); 
       getSupportActionBar().setTitle("CS202"); 
       getSupportActionBar().setSubtitle("Computer Organization and Architecture"); 

      } else if (id == R.id.sub_3) { 

       fragmentTransaction = getFragmentManager().beginTransaction(); 
       fragmentTransaction.replace(R.id.frame,new cs204Fragment()); 
       fragmentTransaction.commit(); 
       getSupportActionBar().setTitle("CS204"); 
       getSupportActionBar().setSubtitle("Operating Systems"); 

      } else if (id == R.id.sub_4) { 

       fragmentTransaction = getFragmentManager().beginTransaction(); 
       fragmentTransaction.replace(R.id.frame,new cs206Fragment()); 
       fragmentTransaction.commit(); 
       getSupportActionBar().setTitle("CS206"); 
       getSupportActionBar().setSubtitle("Object Oriented Design and Programming"); 

      } else if (id == R.id.sub_5) { 

       fragmentTransaction = getFragmentManager().beginTransaction(); 
       fragmentTransaction.replace(R.id.frame,new cs208Fragment()); 
       fragmentTransaction.commit(); 
       getSupportActionBar().setTitle("CS208"); 
       getSupportActionBar().setSubtitle("Principles of data base design"); 

      } else if (id == R.id.sub_6) { 

       fragmentTransaction = getFragmentManager().beginTransaction(); 
       fragmentTransaction.replace(R.id.frame,new hs200Fragment()); 
       fragmentTransaction.commit(); 
       getSupportActionBar().setTitle("HS200"); 
       getSupportActionBar().setSubtitle("Buisness Economics"); 

      } else if (id == R.id.sub_7) { 

       fragmentTransaction = getFragmentManager().beginTransaction(); 
       fragmentTransaction.replace(R.id.frame,new hs210Fragment()); 
       fragmentTransaction.commit(); 
       getSupportActionBar().setTitle("HS210"); 
       getSupportActionBar().setSubtitle("Life Skills"); 

      } else if (id == R.id.lab_1) { 

       fragmentTransaction = getFragmentManager().beginTransaction(); 
       fragmentTransaction.replace(R.id.frame,new cs232Fragment()); 
       fragmentTransaction.commit(); 
       getSupportActionBar().setTitle("CS232"); 
       getSupportActionBar().setSubtitle("Free and open system software lab"); 

      } else if (id == R.id.lab_2) { 

       fragmentTransaction = getFragmentManager().beginTransaction(); 
       fragmentTransaction.replace(R.id.frame,new cs234Fragment()); 
       fragmentTransaction.commit(); 
       getSupportActionBar().setTitle("CS234"); 
       getSupportActionBar().setSubtitle("Digital systems lab"); 

      } else if (id == R.id.nav_about) { 

       startActivity(new Intent(getApplicationContext(),About.class)); 

      } else if (id == R.id.nav_share) { 

       Intent myIntent = new Intent(Intent.ACTION_SEND); 
       myIntent.setType("text/plain"); 
       String shareBody = "KTU CSE S4 app download from the given link : link (use chrome browser)"; 
       String shareSub = "Download KTU CSE S4 app"; 
       myIntent.putExtra(Intent.EXTRA_SUBJECT,shareSub); 
       myIntent.putExtra(Intent.EXTRA_TEXT,shareBody); 
       startActivity(Intent.createChooser(myIntent, "Share KTU CSE S4 app via")); 

      } 

      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
      drawer.closeDrawer(GravityCompat.START); 

      if (isopen) 
      { 
       university.startAnimation(Fabclose); 
       ktuweb.startAnimation(Fabclose); 
       drive.startAnimation(Fabclose); 
       fab.startAnimation(Fabanticlockwise); 
       fab_univer.startAnimation(Fabclose); 
       fab_web.startAnimation(Fabclose); 
       fab_drive.startAnimation(Fabclose); 
       dim.startAnimation(dimoff); 
       dim.setClickable(false); 
       fab_web.setClickable(false); 
       fab_univer.setClickable(false); 
       fab_drive.setClickable(false); 
       isopen = false; 
      } 
      return true; 
     } 
    } 
+1

請回答這裏。鏈接 http://stackoverflow.com/questions/18475419/how-to-open-navigationdrawer-on-first-app-start –

回答

0
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // your other code... 
    // setContentView(...) initialize drawer and stuff like that... 

    // use thread for performance 
    Thread t = new Thread(new Runnable() { 

     @Override 
     public void run() { 

      SharedPreferences sp = Context.getSharedPreferences("yoursharedprefs", 0); 
      boolean isFirstStart = sp.getBoolean("key", true); 
      // we will not get a value at first start, so true will be returned 

      // if it was the first app start 
      if(isFirstStart) { 
       mDrawerLayout.openDrawer(mDrawerList); 
       Editor e = sp.edit(); 
       // we save the value "false", indicating that it is no longer the first appstart 
       e.putBoolean("key", false); 
       e.commit(); 
      } 
     }   
    }); 

    t.start(); 
} 
相關問題