0

when i run my application emulator simply says "unfortunately,myapp has stopped". this is the logchat

--------- beginning of crash 01-12 19:18:23.589 3469-3469/eu.masconsult.template.recipes.green E/AndroidRuntime: FATAL EXCEPTION: main
Process: eu.masconsult.template.recipes.green, PID: 3469
java.lang.RuntimeException: Unable to start activity ComponentInfo{eu.masconsult.template.recipes.green/eu.masconsult.template.recipes.ui.RecipeItemListActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayShowTitleEnabled(boolean)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayShowTitleEnabled(boolean)' on a null object reference
    at eu.masconsult.template.recipes.ui.RecipeItemListActivity.onCreate(RecipeItemListActivity.java:74)
    at android.app.Activity.performCreate(Activity.java:6237)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  01-12 19:18:25.210 3469-3476/eu.masconsult.template.recipes.green W/art: Suspending all threads took: 41.292ms 01-12 19:18:26.642 3469-3476/eu.masconsult.template.recipes.green W/art: Suspending all threads took: 22.021ms 01-12 19:18:27.875 3469-3525/eu.masconsult.template.recipes.green E/ImportRecipesOperation: Error while importing recipes. 
        com.robotoworks.mechanoid.net.UnexpectedHttpStatusException: Expected Status 200,  Actual Status 403
            at com.robotoworks.mechanoid.net.HttpUrlConnectionResponse.checkResponseCodeOk(HttpUrlConnectionResponse.java:58)
            at eu.masconsult.template.recipes.ops.ImportRecipesOperation.readRecipes(ImportRecipesOperation.java:101)
            at eu.masconsult.template.recipes.ops.ImportRecipesOperation.onExecute(ImportRecipesOperation.java:53)
            at eu.masconsult.template.recipes.ops.AbstractImportRecipesOperation.execute(AbstractImportRecipesOperation.java:63)
            at com.robotoworks.mechanoid.ops.OperationProcessor$OperationRunnable.run(OperationProcessor.java:264)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:148)
            at android.os.HandlerThread.run(HandlerThread.java:61) 01-12 19:18:27.875 3469-3525/eu.masconsult.template.recipes.green D/ImportRecipesOperation: Try with last successfull updateUrl. 01-12 19:18:30.213 3469-3525/eu.masconsult.template.recipes.green D/ImportRecipesOperation: Getting recipes from: http://s3.amazonaws.com/masconsult-template-recipes/recipes-update.json 01-12 19:18:35.275 3469-3476/eu.masconsult.template.recipes.green W/art: Suspending all threads took: 6.016ms 01-12 19:18:40.201 3469-3476/eu.masconsult.template.recipes.green W/art: Suspending all threads took: 776.201ms 01-12 19:18:40.636 3469-3476/eu.masconsult.template.recipes.green W/art: Suspending all threads took: 220.969ms 01-12 19:18:41.431 3469-3476/eu.masconsult.template.recipes.green W/art: Suspending all threads took: 13.152ms 01-12 19:18:43.451 3469-3525/eu.masconsult.template.recipes.green D/ImportRecipesOperation: Found 1 recipes 01-12 19:18:45.706 3469-3476/eu.masconsult.template.recipes.green W/art: Suspending all threads took: 242.773ms 01-12 19:18:47.391 3469-3476/eu.masconsult.template.recipes.green W/art: Suspending all threads took: 439.048ms 01-12 19:18:47.972 3469-3476/eu.masconsult.template.recipes.green W/art: Suspending all threads took: 507.589ms 01-12 19:18:48.676 3469-3525/eu.masconsult.template.recipes.green D/ImportRecipesOperation: Inserted 0 recipes and updated 1 recipes in db for 5 seconds 01-12 19:20:00.649 3469-3476/eu.masconsult.template.recipes.green W/art: Suspending all threads took: 18.235ms 01-12 19:20:59.500 3469-3476/eu.masconsult.template.recipes.green W/art: Suspending all threads took: 17.266ms

and this is my java file:

RecipeItemListActivity.java

/**
 * An activity representing a list of RecipeItems. This activity has different presentations for
 * handset and tablet-size devices. On handsets, the activity presents a list of items, which when
 * touched, lead to a {@link RecipeItemDetailActivity} representing item details. On tablets, the
 * activity presents the list of items and item details side-by-side using two vertical panes.
 * <p>
 * The activity makes heavy use of fragments. The list of items is a {@link RecipeItemListFragment}
 * and the item details (if present) is a {@link RecipeItemDetailFragment}.
 * <p>
 * This activity also implements the required {@link RecipeItemListFragment.Callbacks} interface to
 * listen for item selections.
 */
public class RecipeItemListActivity extends ActionBarActivity implements
        RecipeItemListFragment.Callbacks, OnNavigationListener, LoaderCallbacks<Cursor>,
        OnActionExpandListener, OnSuggestionListener {

    private static final String EXTRA_RECIPE_ID = "recipeID";

    private static final int FAVORITES_ITEM_INDEX = 1;
    private static final int ALL_RECIPES_ITEM_INDEX = 0;

    /**
     * Whether or not the activity is in two-pane mode, i.e. running on a tablet device.
     */
    private boolean mTwoPane;
    private SimpleCursorAdapter categoriesAdapter;
    private RecipeItemListFragment recipeListFragment;
    private SearchView searchView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        setContentView(R.layout.activity_recipeitem_list);
        recipeListFragment = (RecipeItemListFragment) getSupportFragmentManager().findFragmentById(
                R.id.recipeitem_list);
        setUpCategoriesSpinner();
        if (findViewById(R.id.recipeitem_detail_container) != null) {
            // The detail container view will be present only in the
            // large-screen layouts (res/values-large and
            // res/values-sw600dp). If this view is present, then the
            // activity should be in two-pane mode.
            mTwoPane = true;

            // In two-pane mode, list items should be given the
            // 'activated' state when touched.
            recipeListFragment.setActivateOnItemClick(true);
            ((RecipeItemListFragment) getSupportFragmentManager().findFragmentById(
                    R.id.recipeitem_list)).setActivateOnItemClick(true);

            if (getIntent().hasExtra(EXTRA_RECIPE_ID) && savedInstanceState == null) {
                ((RecipeItemListFragment) getSupportFragmentManager().findFragmentById(
                        R.id.recipeitem_list)).setSelectionById(getIntent().getLongExtra(
                        EXTRA_RECIPE_ID, 0));
            }

        }

        if (getIntent().hasExtra(EXTRA_RECIPE_ID) && savedInstanceState == null) {
            startService(NotificationService.getIntentForAlarm(this));
            onItemSelected(getIntent().getLongExtra(EXTRA_RECIPE_ID, 0));
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        Fragment fragment = getSupportFragmentManager().findFragmentById(
                R.id.recipeitem_detail_container);
        if (fragment != null && fragment.isAdded()) {
            findViewById(R.id.detail_empty_view).setVisibility(View.GONE);
        }
    }

    private void setUpCategoriesSpinner() {
        String[] columns = new String[] { Categories.CATEGORY };
        getSupportLoaderManager().restartLoader(1, null, this);
        categoriesAdapter = new SimpleCursorAdapter(this, R.layout.spinner_category, null, columns,
                new int[] { android.R.id.text1 }, 0);

        ArrayAdapter<String> allRecipesAdapter = new ArrayAdapter<String>(this,
                R.layout.spinner_category, android.R.id.text1, new String[] {
                        getString(R.string.all_recipes), getString(R.string.favorites) });
        allRecipesAdapter.setDropDownViewResource(R.layout.drop_down_item_category);
        categoriesAdapter.setDropDownViewResource(R.layout.drop_down_item_category);
        MergeSpinnerAdapter mergeAdapter = new MergeSpinnerAdapter();
        mergeAdapter.addAdapter(allRecipesAdapter);
        mergeAdapter.addAdapter(categoriesAdapter);

        getSupportActionBar().setListNavigationCallbacks(mergeAdapter, this);
    }

    /**
     * Callback method from {@link RecipeItemListFragment.Callbacks} indicating that the item with
     * the given ID was selected.
     */
    @Override
    public void onItemSelected(long id) {
        if (mTwoPane) {
            // In two-pane mode, show the detail view in this activity by
            // adding or replacing the detail fragment using a
            // fragment transaction.

            findViewById(R.id.detail_empty_view).setVisibility(View.GONE);

            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.recipeitem_detail_container, RecipeItemDetailFragment.create(id))
                    .commit();

        } else {
            // In single-pane mode, simply start the detail activity
            // for the selected item ID.
            Intent detailIntent = new Intent(this, RecipeItemDetailActivity.class);
            detailIntent.putExtra(RecipeItemDetailFragment.ARG_ITEM_ID, id);
            startActivity(detailIntent);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_search) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.search_menu, menu);
        setUpSearchView(menu);
        return super.onCreateOptionsMenu(menu);
    }

    private void setUpSearchView(Menu menu) {
        MenuItem menuItem = menu.findItem(R.id.action_search);
        searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
        searchView.setIconifiedByDefault(false);
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        MenuItemCompat.setOnActionExpandListener(menuItem, this);
        searchView.setOnSuggestionListener(this);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            if (!TextUtils.isEmpty(query)) {
                SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
                        SearchRecipeSuggestionsProvider.AUTHORITY,
                        SearchRecipeSuggestionsProvider.MODE);
                suggestions.saveRecentQuery(query, null);

            }
            recipeListFragment.setQuery(query);
        }

    }

    @Override
    public boolean onNavigationItemSelected(int which, long id) {
        RecipeItemListFragment listFragment = (RecipeItemListFragment) getSupportFragmentManager()
                .findFragmentById(R.id.recipeitem_list);
        if (which == ALL_RECIPES_ITEM_INDEX) {
            listFragment.showRecipesFromCategory(null);
            return true;
        } else if (which == FAVORITES_ITEM_INDEX) {
            listFragment.showFavorites();
            return true;
        }

        Cursor item = (Cursor) categoriesAdapter.getItem(which - 2);
        String category = CategoriesRecord.fromCursor(item).getCategory();
        listFragment.showRecipesFromCategory(category);
        return true;
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        return SQuery.newQuery().createSupportLoader(Categories.CONTENT_URI,
                CategoriesRecord.PROJECTION);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        categoriesAdapter.swapCursor(data);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        categoriesAdapter.swapCursor(null);
    }

    @Override
    public boolean onMenuItemActionExpand(MenuItem item) {
        return true;
    }

    @Override
    public boolean onMenuItemActionCollapse(MenuItem item) {
        recipeListFragment.setQuery(null);
        return true;
    }

    public static Intent newIntentForRecipe(Context context, long recipeId) {
        Intent intent = new Intent(context, RecipeItemListActivity.class);
        intent.putExtra(EXTRA_RECIPE_ID, recipeId);
        return intent;
    }

    @Override
    public boolean onSuggestionClick(int position) {
        CursorAdapter selectedView = searchView.getSuggestionsAdapter();
        Cursor cursor = (Cursor) selectedView.getItem(position);
        int index = cursor.getColumnIndexOrThrow(SearchManager.SUGGEST_COLUMN_TEXT_1);
        searchView.setQuery(cursor.getString(index), true);
        return true;
    }

    @Override
    public boolean onSuggestionSelect(int position) {
        return true;
    }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Hendri
  • 1

1 Answers1

0

If you are using a theme which does not include an action bar, you'll need to explicitly set one using a toolbar.

An example:

@Bind(R.id.toolbar)
Toolbar mToolbar;

Butterknife.bind(this)
setActionBar(mToolbar)
//or
setSupportActionBar(mToolbar)
//then you can use getActionBar()
getActionBar().setDisplayShowTitleEnabled(true)
Orbit
  • 2,985
  • 9
  • 49
  • 106