2013-08-23 40 views
1

我試圖使用位於LocationActivity.java中的checkGoogleplay()onCreate MainActivity.java,但應用程序在運行時崩潰。 如果我合併兩個java文件,它的工作原理,所以它可能涉及到進口問題?任何人的幫助,感謝從OnCreate調用另一個java方法時出現NullPointerException

這是我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(); 
     }    

    } 

這裏是我的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(); 
} 

這裏是目錄下載

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 
+0

只需在super.oncreate()方法 –

+0

之前調用此方法,您是否在ur文件清單文件中添加了此方法? – Piyush

+0

什麼是'getBaseContext()'?它可以爲空嗎?你爲什麼不使用'this'而不是它呢? –

回答

2

你被startActivity

這樣

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

的活動啓動

如果需要值在LoacationActivity使用意圖傳遞給MainActivity(考慮LocationActivity是活動類)。

你也有這樣的public class LocationActivity extends Activity

但我沒有看到LocationActivity

而且任何onCreate方法檢查此

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

下檢查檢查主題爲谷歌遊戲服務

+0

謝謝,我使用intent來啓動解決了大問題的活動,但是我應該在這裏使用什麼類擴展,如果我在LocationActivity中添加onCreate方法,MainActivity的所有函數都不會工作,因爲我想兩個super.oncreate。 – atom2ueki

+0

@ user2372824每個活動類都會擴展活動併爲其每個活動分配「onCreate」和自己的佈局。 – Raghunandan

+0

其實LocationActivity不需要擴展活動,但如果刪除它,它下面的許多方法將變得不明確 – atom2ueki

0

添加在AndroidManifest.xml文件下面的代碼:

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

在MainActivity下面...

+0

無論如何,我已經嘗試過,但無法正常工作,謝謝您的回覆。 – atom2ueki

1

您不能只使用以下項創建活動的對象:

MyActivity activity = new MyActivity();

,你會與正常的Java類。 Android中的所有活動必須經歷活動生命週期,以便它們具有附加的有效上下文。

通過治療活動作爲一個普通的Java類,你結束了一個空的上下文。由於Activity中的大多數方法都是在Context上調用的,所以會得到一個空指針異常,這就是爲什麼你的應用程序崩潰的原因。

取而代之的是,將所有的此類方法需要從其他類叫成接受在其構造一個有效的上下文一個實用工具類,然後利用這方面的方法做的工作。

0

Context對象只是傳遞給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(); 
    }    

的方法是在練習和活動,從簡單的Java文件不同,因此,你需要啓動它,而不是創建一個對象吧:

Intent i= new Intent(this, LocationActivity.class); 
startActivity(i); 
1

第一件事..我可以知道,LocationActivity.java活動中的onCreate()方法在哪裏。

and'mlocal = new LocationActivity();'您需要啓動該活動,否則不要在LocationActivity文件中擴展Activity。

您是否在manifest中聲明瞭LocationActivity?

LocationRequest mLocationRequest; LocationClient mLocationClient;

你可以探索以上兩個類別,可能會在這些類別中變爲空。

1

添加onCreate方法中的位置活動

加入清單中

0

更新你的方法

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

    } 
0

讓LocationActivity一個普通的Java類,並通過在MainActivity上下文在該類添加位置活動構造函數。

僅在您有視圖可顯示的情況下延長活動。我沒有看到在LocationActivity中擴展活動的任何需要

相關問題