我是Android新手,我无法理解我在代码中应该对protectionLevel
做什么,因为这是导致我的应用程序在private GoogleApiClient client;
崩溃的原因。
这是我的代码:
RunTime
它给了我以下错误:
无法开始活动 ComponentInfo {com.example.yasmin.beproject / com.example.yasmin.beproject.MainActivity}: java.lang.NullPointerException:尝试调用虚方法'void com.google.android.gms.common.api.GoogleApiClient.connect()'上的null 对象参考
我知道我应该初始化我的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();
}
}
}
变量,但我该怎么做?