java - 我的应用程序无法自行运行 MainActivity

标签 java android

我的 logcat 中出现一些错误,并且我的应用程序无法在我的智能手机上运行... 我更改了 list 和 MainActivity 中的一些代码(名为 HomaActivity )

这是我的 list :

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ir.homa">
<permission
    android:name="android.permission.INTERNET">
</permission>
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="24" />
    <application
        android:name=".AppController"
        android:allowBackup="true"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >



        <activity
            android:name=".HomaActivity"
            android:label="@string/app_name"
            android:launchMode="singleTop"   >
            <intent-filter >
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".LoginActivity"
            android:label="@string/app_name"
            android:launchMode="singleTop"
            android:windowSoftInputMode="adjustPan" >
            <category android:name="android.intent.category.DEFAULT" />
        </activity>
        <activity
            android:name=".RegisterActivity"
            android:label="@string/app_name"
            android:launchMode="singleTop"
            android:windowSoftInputMode="adjustPan" >

            <category android:name="android.intent.category.DEFAULT" />
        </activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
     App Indexing.  See https://g.co/AppIndexing/AndroidStudio for more information. -->
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />


    </application>

</manifest>

这是 HomaActivity.Java:

   package ir.homa;

import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
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.View;

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


public class HomaActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homa);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        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.setDrawerListener(toggle);
        toggle.syncState();

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




    }

    /**
     * Logging out the user. Will set isLoggedIn flag to false in shared
     * preferences Clears the user data from sqlite users table
     */





    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.homa, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

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

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

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

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

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

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

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
   public GoogleApiClient client;
    @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.
     GoogleApiClient client = null;
        client.connect();
        Action viewAction = Action.newAction(
                Action.TYPE_VIEW, // TODO: choose an action type.
                "Homa 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://ir.homa/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.
                "Homa 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://ir.homa/http/host/path")
        );
        AppIndex.AppIndexApi.end(client, viewAction);
        client.disconnect();
    }
}

这些是我的 LOGCAT 中的错误:

   07-02 20:26:44.441 26501-26501/ir.homa E/AndroidRuntime: FATAL EXCEPTION: main
                                                         Process: ir.homa, PID: 26501
                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{ir.homa/ir.homa.HomaActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.common.api.GoogleApiClient.connect()' on a null object reference
                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2658)
                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723)
                                                             at android.app.ActivityThread.access$900(ActivityThread.java:172)
                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422)
                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                             at android.os.Looper.loop(Looper.java:145)
                                                             at android.app.ActivityThread.main(ActivityThread.java:5832)
                                                             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:1399)
                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
                                                          Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.common.api.GoogleApiClient.connect()' on a null object reference
                                                             at ir.homa.HomaActivity.onStart(HomaActivity.java:127)
                                                             at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1234)
                                                             at android.app.Activity.performStart(Activity.java:6258)
                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2621)
                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2723) 
                                                             at android.app.ActivityThread.access$900(ActivityThread.java:172) 
                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1422) 
                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                             at android.os.Looper.loop(Looper.java:145) 
                                                             at android.app.ActivityThread.main(ActivityThread.java:5832) 
                                                             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:1399) 
                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 

最佳答案

这是你的问题,你正在调用一个空变量的方法:

GoogleApiClient client = null;
client.connect();

您需要先初始化客户端。像这样的东西

GoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)
    .enableAutoManage(this /* FragmentActivity */,
                  this /* OnConnectionFailedListener */)
    .addApi(Drive.API)
    .addScope(Drive.SCOPE_FILE)
    .build();

检查this link .

关于java - 我的应用程序无法自行运行 MainActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38161954/

相关文章:

java - 输入字段和将文本转换为数字

android - json 值的 img src 属性在 android TextView 中显示蓝色小框

java - 配置 log4j 以在运行时记录到自定义文件

java - 用户输入到多维数组(列)

java - 能够向电子邮件地址发送电子邮件,并将其显示在网页上

java - 使用标志 CLEAR_TOP 时后台堆栈中的 Activity

javascript - 如何在webview中调用css和js文件?

java - 如何将 JSF UIInput 组件重置为其托管 bean 值

java - 有没有一种方法或工具可以将 IntelliJ IDEA 项目转换为 Makefile 项目?

android - 检查数据是否存在于 SQLite Android