0

I'm very new to Android and I'm having trouble understanding what I'm supposed to do in regards of private GoogleApiClient client; in my code since it's what's causing my app to crash on RunTime. Here's my code:

package com.example.yasmin.beproject;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

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

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends ListActivity {

  private PackageManager packageManager = null;
  private List < ApplicationInfo > applist = null;
  private AppAdapter listadapter = null;
  /**
   * 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);

    packageManager = getPackageManager();

    new LoadApplications().execute();


  }

  @
  Override
  protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);

    ApplicationInfo app = applist.get(position);

    try {
      Intent intent = packageManager.getLaunchIntentForPackage(app.packageName);
      if (intent != null) {
        startActivity(intent);
      }
    } catch (ActivityNotFoundException e) {
      Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
    } catch (Exception e) {
      Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
    }
  }

  private List < ApplicationInfo > checkForLaunchIntent(List < ApplicationInfo > list) {

    ArrayList < ApplicationInfo > appList = new ArrayList < ApplicationInfo > ();

    for (ApplicationInfo info: list) {
      try {
        if (packageManager.getLaunchIntentForPackage(info.packageName) != null) {
          appList.add(info);
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }

    return appList;
  }

  @
  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://com.example.yasmin.beproject/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://com.example.yasmin.beproject/http/host/path")
    );
    AppIndex.AppIndexApi.end(client, viewAction);
    client.disconnect();
  }

  private class LoadApplications extends AsyncTask < Void, Void, Void > {

    private ProgressDialog progress = null;

    @
    Override
    protected Void doInBackground(Void...params) {
      applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));

      listadapter = new AppAdapter(MainActivity.this, R.layout.list_item, applist);

      return null;
    }

    @
    Override
    protected void onPostExecute(Void result) {
      setListAdapter(listadapter);
      progress.dismiss();
      super.onPostExecute(result);
    }

    @
    Override
    protected void onPreExecute() {

      progress = ProgressDialog.show(MainActivity.this, null, "Loading apps info...");
      super.onPreExecute();

    }
  }
}

And it gives me the follwoing error:

Unable to start activity ComponentInfo{com.example.yasmin.beproject/com.example.yasmin.beproject.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.common.api.GoogleApiClient.connect()' on a null object reference

I understand that I am supposed to initialize my client variable, but how do I do that?

ytba92
  • 55
  • 13
  • 1
    take a look at https://developers.google.com/android/guides/api-client – Bertrand Martel Jul 31 '16 at 13:24
  • @Tunaki - the question was **how to initialize** – OneCricketeer Jul 31 '16 at 13:34
  • I understand that I'm supposed to make it point to a value, but I don't know how to do that, what value am I supposed to assign it? – ytba92 Jul 31 '16 at 13:45
  • https://www.youtube.com/watch?v=n6R-sc2QMVM I followed this step by step, in his code he doesn't have this private GoogleApiClient client. mine was autogenerated. How can I prevent that from happening? – ytba92 Jul 31 '16 at 14:27

0 Answers0