应用程序崩溃时出错无法实例化活动ComponentInfo

时间:2017-05-16 11:35:40

标签: android android-fragments

这真的很正常,很多时候都会问问题,但我仍然无法问它。我的应用程序崩溃没有任何错误。整个应用程序似乎很好,它的源代码没有任何错误,但仍然是应用程序崩溃。

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;

public class HomePage extends AppCompatActivity {
    private RecyclerView mblogList;
    private DatabaseReference mdatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mdatabase= FirebaseDatabase.getInstance().getReference().child("Blog");
        setContentView(R.layout.activity_home_page);
        mblogList=(RecyclerView)findViewById(R.id.blog_list);
        mblogList.setHasFixedSize(true);
        mblogList.setItemViewCacheSize(20);
        mblogList.setDrawingCacheEnabled(true);
        mblogList.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
        mblogList.setLayoutManager(new LinearLayoutManager(this));


    }

    @Override
    protected void onStart() {
        super.onStart();
        FirebaseRecyclerAdapter<Blog,BlogViewHolder>firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(Blog.class,R.layout.blog_row,BlogViewHolder.class,mdatabase) {

            @Override
            protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {
                viewHolder.setTitle(model.getTitle());
                viewHolder.setDesc(model.getDescription());
                viewHolder.setImage(getApplicationContext(),model.getImage());

            }
        };
        mblogList.setAdapter(firebaseRecyclerAdapter);
    }
    public static class BlogViewHolder extends RecyclerView.ViewHolder {
        View mView;
        public BlogViewHolder(View itemView) {


            super(itemView);
            mView=itemView;
        }
        public  void setTitle(String title){
            TextView post_title=(TextView)mView.findViewById(R.id.post_title);
            post_title.setText(title);

        }
        public  void setDesc(String desc){
            TextView post_title=(TextView)mView.findViewById(R.id.post_des);
            post_title.setText(desc);

        }
        public void setImage(Context ctx,String image){
            ImageView post_image=(ImageView)mView.findViewById(R.id.post_image);
            Picasso.with(ctx).load(image).into(post_image);
        }
    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
  if (item.getItemId()==R.id.action_add){
      startActivity(new Intent(HomePage.this,BlogPost.class));
  }
        return super.onOptionsItemSelected(item);
    }
}

这是homeActivity。这很明显。

 < activity android:name=".HomePage"
                android:exported="true">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name=".BlogPost">
                <intent-filter>
                    <action android:name="android.intent.action.GET_CONTENT">
                        <category android:name="android.intent.category.DEFAULT" />
                    </action>
                </intent-filter>
            </activity>
            <activity android:name=".LoginActivity"
                android:exported="true"/>

实际上我想首先打开loginActivity,我尝试用HomePage活动更改名称,但它仍然崩溃。我不知道为什么会发生这种情况。我已经尝试过各种可能的方法,尝试清理和重建项目 错误就在这里

05-16 17:41:37.992 7012-7012/com.example.hp.urblog E/AndroidRuntime: FATAL EXCEPTION: main
                                                                     Process: com.example.hp.urblog, PID: 7012
                                                                     java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.hp.urblog/com.example.hp.urblog.LoginActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Looper android.content.Context.getMainLooper()' on a null object reference
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
                                                                         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 'android.os.Looper android.content.Context.getMainLooper()' on a null object reference
                                                                         at android.content.ContextWrapper.getMainLooper(ContextWrapper.java:102)
                                                                         at com.google.android.gms.common.api.GoogleApiClient$Builder.<init>(Unknown Source)
                                                                         at com.example.hp.urblog.LoginActivity.<init>(LoginActivity.java:20)
                                                                         at java.lang.Class.newInstance(Native Method)
                                                                         at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
                                                                         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) 

这是我的loginActivity

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

import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.api.GoogleApiClient;
public class LoginActivity extends AppCompatActivity {
    private Button Sign_in_button;
    private static final int RC_SIGN_IN = 9001;
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();
    GoogleApiClient   mGoogleApiClient = new GoogleApiClient.Builder(this)
            //  .enableAutoManage(LoginActivity.this, MainActivity.class)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

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

        setContentView(R.layout.activity_login);
        Sign_in_button=(Button)findViewById(R.id.sign_in_button);
        Sign_in_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.sign_in_button:
                        signIn();
                        break;
                    // ...
                }
            }
        });
    }
    private void signIn() {
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient );
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            handleSignInResult(result);
        }
    }
    private void handleSignInResult(GoogleSignInResult result) {
        //Log.d(TAG, "handleSignInResult:" + result.isSuccess());
        if (result.isSuccess()) {
            // Signed in successfully, show authenticated UI.
            GoogleSignInAccount acct = result.getSignInAccount();
            startActivity(new Intent(this,MainActivity.class));
          //  mStatusTextView.setText(getString(R.string.signed_in_fmt, acct.getDisplayName()));
           // updateUI(true);
        } //else {
            // Signed out, show unauthenticated UI.
           // updateUI(false);
        //}
    }


}

1 个答案:

答案 0 :(得分:0)

Yup得到您的问题GoogleApiClient未正确初始化

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

            GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                 .requestEmail()
                 .build();
            GoogleApiClient   mGoogleApiClient = new GoogleApiClient.Builder(LoginActivity.this)
             //  .enableAutoManage(LoginActivity.this, MainActivity.class)
                 .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                 .build();


            Sign_in_button=(Button)findViewById(R.id.sign_in_button);
            Sign_in_button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (v.getId()) {
                        case R.id.sign_in_button:
                            signIn();
                            break;
                        // ...
                    }
                }
            });
        }

参考 - https://stackoverflow.com/a/41320391/4741746https://stackoverflow.com/a/28928746/4741746

每当你想要传递上下文时,还有一个建议总是使用ActivityName.this,比如LoginActivity.this不仅仅是#34;这个&#34;