2016-06-20 15 views
-2

私はいくつかの断片でメニューの引き出しを含む私のメインを持っています。その後、メインページを表示する前にホストページ。ここでAndroidエラー[nullヌルオブジェクトリファレンスで[null android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)]

package thyroid.com.thyroid; 


import thyroid.com.thyroid.adapter.NavDrawerListAdapter; 
import thyroid.com.thyroid.model.NavDrawerItem; 

import java.util.ArrayList; 

import android.support.v7.app.ActionBarActivity; 
import android.app.Activity; 
import android.app.Fragment; 
import android.app.FragmentManager; 
import android.content.res.Configuration; 
import android.content.res.TypedArray; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.v4.app.ActionBarDrawerToggle; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBar; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ListView; 

import com.google.android.gms.appindexing.Action; 
import com.google.android.gms.appindexing.AppIndex; 
import com.google.android.gms.common.api.GoogleApiClient; 


public class MainActivity extends Activity { 
    private DrawerLayout mDrawerLayout; 
    private ListView mDrawerList; 
    private ActionBarDrawerToggle mDrawerToggle; 

    // nav drawer title 
    private CharSequence mDrawerTitle; 

    // used to store app title 
    private CharSequence mTitle; 

    // slide menu items 
    private String[] navMenuTitles; 
    private TypedArray navMenuIcons; 

    private ArrayList<NavDrawerItem> navDrawerItems; 
    private NavDrawerListAdapter adapter; 
    /** 
    * ATTENTION: This was auto-generated to implement the App Indexing API. 
    * See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
    private GoogleApiClient client; 

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

     mTitle = mDrawerTitle = getTitle(); 

     // load slide menu items 
     navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items); 

     // nav drawer icons from resources 
     navMenuIcons = getResources() 
       .obtainTypedArray(R.array.nav_drawer_icons); 

     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     mDrawerList = (ListView) findViewById(R.id.list_slidermenu); 

     navDrawerItems = new ArrayList<NavDrawerItem>(); 

     // adding nav drawer items to array 
     // Home 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1))); 
     // Find People 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1))); 
     // Photos 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1))); 
     // Communities, Will add a counter here 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1), true, "22")); 
     // Pages 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1))); 
     // What's hot, We will add a counter here 
     navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+")); 


     // Recycle the typed array 
     navMenuIcons.recycle(); 

     mDrawerList.setOnItemClickListener(new SlideMenuClickListener()); 

     // setting the nav drawer list adapter 
     adapter = new NavDrawerListAdapter(getApplicationContext(), 
       navDrawerItems); 
     mDrawerList.setAdapter(adapter); 

     // enabling action bar app icon and behaving it as toggle button 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setHomeButtonEnabled(true); 

     mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 
       R.drawable.ic_drawer, //nav menu toggle icon 
       R.string.app_name, // nav drawer open - description for accessibility 
       R.string.app_name // nav drawer close - description for accessibility 
     ) { 
      public void onDrawerClosed(View view) { 
       getSupportActionBar().setTitle(mTitle); 
       // calling onPrepareOptionsMenu() to show action bar icons 
       invalidateOptionsMenu(); 
      } 

      public void onDrawerOpened(View drawerView) { 
       getSupportActionBar().setTitle(mDrawerTitle); 
       // calling onPrepareOptionsMenu() to hide action bar icons 
       invalidateOptionsMenu(); 
      } 
     }; 
     mDrawerLayout.setDrawerListener(mDrawerToggle); 

     if (savedInstanceState == null) { 
      // on first time display view for first nav item 
      displayView(0); 
     } 
     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
    } 

    private ActionBar getSupportActionBar() { 
     return null; 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client.connect(); 
     Action viewAction = Action.newAction(
       Action.TYPE_VIEW, // TODO: choose an action type. 
       "Main Page", // TODO: Define a title for the content shown. 
       // TODO: If you have web page content that matches this app activity's content, 
       // make sure this auto-generated web page URL is correct. 
       // Otherwise, set the URL to null. 
       Uri.parse("http://host/path"), 
       // TODO: Make sure this auto-generated app URL is correct. 
       Uri.parse("android-app://thyroid.com.thyroid/http/host/path") 
     ); 
     AppIndex.AppIndexApi.start(client, viewAction); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     Action viewAction = Action.newAction(
       Action.TYPE_VIEW, // TODO: choose an action type. 
       "Main Page", // TODO: Define a title for the content shown. 
       // TODO: If you have web page content that matches this app activity's content, 
       // make sure this auto-generated web page URL is correct. 
       // Otherwise, set the URL to null. 
       Uri.parse("http://host/path"), 
       // TODO: Make sure this auto-generated app URL is correct. 
       Uri.parse("android-app://thyroid.com.thyroid/http/host/path") 
     ); 
     AppIndex.AppIndexApi.end(client, viewAction); 
     client.disconnect(); 
    } 

    /** 
    * Slide menu item click listener 
    */ 
    private class SlideMenuClickListener implements 
      ListView.OnItemClickListener { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, 
           long id) { 
      // display view for selected nav drawer item 
      displayView(position); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // toggle nav drawer on selecting action bar app icon/title 
     if (mDrawerToggle.onOptionsItemSelected(item)) { 
      return true; 
     } 
     // Handle action bar actions click 
     switch (item.getItemId()) { 
      case R.id.action_settings: 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

    /* * 
    * Called when invalidateOptionsMenu() is triggered 
    */ 
    @Override 
    public boolean onPrepareOptionsMenu(Menu menu) { 
     // if nav drawer is opened, hide the action items 
     boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList); 
     menu.findItem(R.id.action_settings).setVisible(!drawerOpen); 
     return super.onPrepareOptionsMenu(menu); 
    } 

    /** 
    * Diplaying fragment view for selected nav drawer list item 
    */ 
    private void displayView(int position) { 
     // update the main content by replacing fragments 
     Fragment fragment = null; 
     switch (position) { 
      case 0: 
       fragment = new LoginFragment(); 
       break; 
      case 1: 
       fragment = new RegisterFragment(); 
       break; 
      case 2: 
       fragment = new LoginFragment(); 
       break; 
      case 3: 
       fragment = new LoginFragment(); 
       break; 
      case 4: 
       fragment = new LoginFragment(); 
       break; 
      case 5: 
       fragment = new LoginFragment(); 
       break; 

      default: 
       break; 
     } 

     if (fragment != null) { 
      FragmentManager fragmentManager = getFragmentManager(); 
      fragmentManager.beginTransaction() 
        .replace(R.id.frame_container, fragment).commit(); 

      // update selected item and title, then close the drawer 
      mDrawerList.setItemChecked(position, true); 
      mDrawerList.setSelection(position); 
      setTitle(navMenuTitles[position]); 
      mDrawerLayout.closeDrawer(mDrawerList); 
     } else { 
      // error in creating fragment 
      Log.e("MainActivity", "Error in creating fragment"); 
     } 
    } 

    @Override 
    public void setTitle(CharSequence title) { 
     mTitle = title; 
     getSupportActionBar().setTitle(mTitle); 
    } 

    /** 
    * When using the ActionBarDrawerToggle, you must call it during 
    * onPostCreate() and onConfigurationChanged()... 
    */ 

    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 
     // Sync the toggle state after onRestoreInstanceState has occurred. 
     mDrawerToggle.syncState(); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     // Pass any configuration change to the drawer toggls 
     mDrawerToggle.onConfigurationChanged(newConfig); 
    } 

} 

フラグメントの1:

私はそれを getSupportActionBar().setDisplayHomeAsUpEnabled(true); を変更しようとしましたが、それでもさえ public class MainActivity extends Activity {または public class MainActivity extends AppcompactActivity {

と何も私のメインが存在しないことをAttempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference

について多少の誤差があります

そして今、.classはメインの前の "ホストページ"です:

package thyroid.com.thyroid; 


import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 

import static thyroid.com.thyroid.R.id.button; 


public class SplashScreen extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.splashscreen); 

     Button button = (Button) findViewById(R.id.button); 
     assert button != null; 
     button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(v.getContext(),MainActivity.class); 
       startActivityForResult(intent,0); 
      } 
     }); 




     Thread timerThread = new Thread() { 
      public void run() { 
       try { 
        sleep(6000); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } finally { 
        Intent intent = new Intent(SplashScreen.this, MainActivity.class); 
        startActivity(intent); 
       } 
      } 
     }; 
     timerThread.start(); 
    } 

    @Override 
    protected void onPause() { 
     // TODO Auto-generated method stub 
     super.onPause(); 
     finish(); 
    } 


} 

これは私のエラーです:あなたのコードで

06-20 11:23:33.603 29939-29939/thyroid.com.thyroid E/AndroidRuntime: FATAL EXCEPTION: main 
                  Process: thyroid.com.thyroid, PID: 29939 
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{thyroid.com.thyroid/thyroid.com.thyroid.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference 
                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) 
                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
                   at android.app.ActivityThread.access$800(ActivityThread.java:144) 
                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                   at android.os.Looper.loop(Looper.java:135) 
                   at android.app.ActivityThread.main(ActivityThread.java:5221) 
                   at java.lang.reflect.Method.invoke(Native Method) 
                   at java.lang.reflect.Method.invoke(Method.java:372) 
                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898) 
                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693) 
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference 
                   at thyroid.com.thyroid.MainActivity.onCreate(MainActivity.java:101) 
                   at android.app.Activity.performCreate(Activity.java:5976) 
                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 
                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
                   at android.app.ActivityThread.access$800(ActivityThread.java:144) 
                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                   at android.os.Looper.loop(Looper.java:135) 
                   at android.app.ActivityThread.main(ActivityThread.java:5221) 
                   at java.lang.reflect.Method.invoke(Native Method) 
                   at java.lang.reflect.Method.invoke(Method.java:372) 
                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898) 
                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693) 
+0

まあ、私は既に質問がたくさんありますが、スティルブロックをチェックしています:( – zouarv42

答えて

0

あなたはそのため

private ActionBar getSupportActionBar() { 
    return null; 
} 

を持って、getSupportActionBar().setDisplayHomeAsUpEnabled(true)は、NPEをスローします。あなたは実際にそのメソッドを実装する必要があります...

+0

答えがThxであるため、私の "public class MainActivity extendsアクティビティ{"またはそれを変更する必要がありますか? – zouarv42

関連する問題