1

I'm trying to use checkGoogleplay() from LocationActivity.java in onCreate MainActivity.java, but app crashed when running. if i merge two java files, it works, so it might related to import issue? anyone help, thanks

this is my LocationActivity

package com.example.location_calculator;

import android.app.Activity;

import android.location.Location;

import android.os.Bundle;

import android.util.Log;

import android.widget.TextView;

import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;

public class LocationActivity extends Activity implements LocationListener,GooglePlayServicesClient.ConnectionCallbacks,GooglePlayServicesClient.OnConnectionFailedListener
{

    private String TAG = this.getClass().getSimpleName();
    LocationRequest mLocationRequest;
    LocationClient mLocationClient;
    Location mCurrentLocation;
    TextView txtinstant;
    int resultCode;

    public void checkGoogleplay()
    {
        resultCode =GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

        if(ConnectionResult.SUCCESS == resultCode){
            Log.d("Location Updates",
                    "Google Play services is available.");
            mLocationClient = new LocationClient(this, this, this);
            mLocationClient.connect();
        }
        else{
            Toast.makeText(this, "Google Play Service Error " + resultCode,Toast.LENGTH_LONG).show();
        }               

    }

Here is my MainActivity

 package com.example.location_calculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity 
{

    TextView txtlocation;
    TextView txtstatus;
    TextView txtinstant;
    Button btn_loc;
    Button btn_ser;
    Button bnt_aploc;
    LocationActivity mlocal;


    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


            mlocal =new LocationActivity();

            mlocal.checkGoogleplay();
}

Here is Catlog

 08-23 15:19:12.576: W/dalvikvm(18236): threadid=1: thread exiting with uncaught exception (group=0x4190a700)
08-23 15:19:12.576: E/AndroidRuntime(18236): FATAL EXCEPTION: main
08-23 15:19:12.576: E/AndroidRuntime(18236): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.location_calculator/com.example.location_calculator.MainActivity}: java.lang.NullPointerException
08-23 15:19:12.576: E/AndroidRuntime(18236):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-23 15:19:12.576: E/AndroidRuntime(18236):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-23 15:19:12.576: E/AndroidRuntime(18236):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-23 15:19:12.576: E/AndroidRuntime(18236):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-23 15:19:12.576: E/AndroidRuntime(18236):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 15:19:12.576: E/AndroidRuntime(18236):    at android.os.Looper.loop(Looper.java:137)
08-23 15:19:12.576: E/AndroidRuntime(18236):    at android.app.ActivityThread.main(ActivityThread.java:5103)
08-23 15:19:12.576: E/AndroidRuntime(18236):    at java.lang.reflect.Method.invokeNative(Native Method)
08-23 15:19:12.576: E/AndroidRuntime(18236):    at java.lang.reflect.Method.invoke(Method.java:525)
08-23 15:19:12.576: E/AndroidRuntime(18236):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-23 15:19:12.576: E/AndroidRuntime(18236):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-23 15:19:12.576: E/AndroidRuntime(18236):    at dalvik.system.NativeStart.main(Native Method)
08-23 15:19:12.576: E/AndroidRuntime(18236): Caused by: java.lang.NullPointerException
08-23 15:19:12.576: E/AndroidRuntime(18236):    at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
08-23 15:19:12.576: E/AndroidRuntime(18236):    at com.example.location_calculator.LocationActivity.checkGoogleplay(LocationActivity.java:30)
08-23 15:19:12.576: E/AndroidRuntime(18236):    at com.example.location_calculator.MainActivity.onCreate(MainActivity.java:35)
08-23 15:19:12.576: E/AndroidRuntime(18236):    at android.app.Activity.performCreate(Activity.java:5133)
08-23 15:19:12.576: E/AndroidRuntime(18236):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-23 15:19:12.576: E/AndroidRuntime(18236):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-23 15:19:12.576: E/AndroidRuntime(18236):    ... 11 more
atom2ueki
  • 837
  • 4
  • 15
  • 32

7 Answers7

2

You are doing this

   mlocal =new LocationActivity(); // which is wrong

An activity is started by startActivity

If you need to pass values to MainActivity from LoacationActivity use intents (considering LocationActivity is a activity class).

Also you have this public class LocationActivity extends Activity

But i do not see any onCreate method in LocationActivity

Also check this

http://developer.android.com/training/location/retrieve-current.html

Check the topic under Check for Google Play Services

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Thanks,I use intent to start activity which solved the big problem, but what class extends i should use here, if i add onCreate method in LocationActivity, all the functions from MainActivity will not working, because of two super.oncreate i think. – atom2ueki Aug 23 '13 at 06:10
  • @user2372824 each activity class extends activity and has its `onCreate` and own layout for each activity. – Raghunandan Aug 23 '13 at 06:17
  • Actually LocationActivity no need extends activity, but if remove that, many method under it will become undefined – atom2ueki Aug 23 '13 at 06:23
  • @user2372824 what you need is a utility class i guess. so make a constructor that takes context as the param. Remove extends Activity and then use the context rather than `this`. – Raghunandan Aug 23 '13 at 06:25
1

first thing.. May I know, where is your onCreate() method in LocationActivity.java activity.

and 'mlocal =new LocationActivity();' is wrong you need to start that activity, otherwise don't extend Activity in LocationActivity file.

have you declared LocationActivity in manifest?

LocationRequest mLocationRequest; LocationClient mLocationClient;

can u explore above two classes, may be ther shud be something going null in those classes.

Niket K
  • 107
  • 1
  • 7
1

Add Oncreate method in location Activity

Add add Location Activity in manifest

0

Add the following code in your AndroidManifest.xml file:

<activity
        android:name=".LocationActivity" />

below the MainActivity...

Exceptional
  • 2,994
  • 1
  • 18
  • 25
0

Just pass the Context object to the GooglePlayServicesUtil

public void checkGoogleplay()
{
    resultCode =GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

    if(ConnectionResult.SUCCESS == resultCode){
        Log.d("Location Updates",
                "Google Play services is available.");
        mLocationClient = new LocationClient(this, this, this);
        mLocationClient.connect();
    }
    else{
        Toast.makeText(this, "Google Play Service Error " + resultCode,Toast.LENGTH_LONG).show();
    }               

The Method is in Activity and Activity differs from Simple Java files, therefore, you need to start it and not create an object for it:

Intent i= new Intent(this, LocationActivity.class);
startActivity(i);
Salman Khakwani
  • 6,684
  • 7
  • 33
  • 58
0

Update your method

public void checkGoogleplay(Context mContext)
    {
        resultCode =GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);

        if(ConnectionResult.SUCCESS == resultCode){
            Log.d("Location Updates",
                    "Google Play services is available.");
            mLocationClient = new LocationClient(mContext, this, this);
            mLocationClient.connect();
        }
        else{
            Toast.makeText(this, "Google Play Service Error " + resultCode,Toast.LENGTH_LONG).show();
        }               

    }
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
0

Make LocationActivity a normal Java class and pass the MainActivity context to that class in the constructor.

Extend Activity only where you have a view to display. I don't see any need for extending Activity in LocationActivity

Vedang Jadhav
  • 510
  • 2
  • 11