-1

it's really a normal and many times asked question but still i'm helpless to ask it. My app is crashing without any error. The overall app seem fine and there is no any error in it's source code but still app crashes.

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);
    }
}

This is homeActivity. and this is manifest.

 < 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"/>

Actually i want to open loginActivity first and i have try changing the name with HomePage activity but still it's crashing.I haven't any idea why this is happening. I have already tried clean and rebuilding of projects every possible methods known to me have tried Error is here

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) 

It's my 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 Answers1

0

Yup Got your problem GoogleApiClient is not initialize properly

    @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;
                        // ...
                    }
                }
            });
        }

Refer -https://stackoverflow.com/a/41320391/4741746 and https://stackoverflow.com/a/28928746/4741746

Just one more suggestion whenever you want to pass context always use ActivityName.this like LoginActivity.this not only "this"

Community
  • 1
  • 1
Sushant Gosavi
  • 3,647
  • 3
  • 35
  • 55