2014-03-25 77 views
1

我只是編程了幾天,所以我很努力地掌握我在這裏做錯了什麼,希望社區能指引我朝着正確的方向前進。 。 這裏我MainActivity:致命的例外:main java.lang.RuntimeException:無法啓動活動ComponentInfo

package com.whatsonwhere.app; 

import android.app.Activity; 
import android.app.Dialog; 
import android.content.Intent; 
import android.content.IntentSender; 
import android.location.Location; 
import android.os.Bundle; 
import android.support.v4.app.DialogFragment; 
import android.support.v7.app.ActionBarActivity; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
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.maps.CameraUpdate; 
import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 


public class MainActivity extends ActionBarActivity implements 
     GooglePlayServicesClient.ConnectionCallbacks, 
     GooglePlayServicesClient.OnConnectionFailedListener{ 
    private SupportMapFragment mapFragment; 
    private GoogleMap map; 
    private LocationClient mLocationClient; 
    /* 
    * Define a request code to send to Google Play services 
    * This code is returned in Activity.onActivityResult 
    */ 
    private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; 

    // Define a DialogFragment that displays the error dialog 
    public static class ErrorDialogFragment extends DialogFragment { 

     // Global field to contain the error dialog 
     private Dialog mDialog; 

     // Default constructor. Sets the dialog field to null 
     public ErrorDialogFragment() { 
      super(); 
      mDialog = null; 
     } 

     // Set the dialog to display 
     public void setDialog(Dialog dialog) { 
      mDialog = dialog; 
     } 

     // Return a Dialog to the DialogFragment. 
     @Override 
     public Dialog onCreateDialog(Bundle savedInstanceState) { 
      return mDialog; 
     } 
    } 

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

     mLocationClient = new LocationClient(this, this, this); 

     mapFragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapView)); 
     map = mapFragment.getMap(); 

     map.setMyLocationEnabled(true); 

    } 

    /* 
    * Called when the Activity becomes visible. 
    */ 
    @Override 
    protected void onStart() { 
     super.onStart(); 
     // Connect the client. 
     if(isGooglePlayServicesAvailable()){ 
      mLocationClient.connect(); 
     } 

    } 

    /* 
    * Called when the Activity is no longer visible. 
    */ 
    @Override 
    protected void onStop() { 
     // Disconnecting the client invalidates it. 
     mLocationClient.disconnect(); 
     super.onStop(); 
    } 

    /* 
    * Handle results returned to the FragmentActivity 
    * by Google Play services 
    */ 
    @Override 
    protected void onActivityResult(
      int requestCode, int resultCode, Intent data) { 
     // Decide what to do based on the original request code 
     switch (requestCode) { 

      case CONNECTION_FAILURE_RESOLUTION_REQUEST: 
      /* 
      * If the result code is Activity.RESULT_OK, try 
      * to connect again 
      */ 
       switch (resultCode) { 
        case Activity.RESULT_OK: 
         mLocationClient.connect(); 
         break; 
       } 

     } 
    } 

    private boolean isGooglePlayServicesAvailable() { 
     // Check that Google Play services is available 
     int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
     // If Google Play services is available 
     if (ConnectionResult.SUCCESS == resultCode) { 
      // In debug mode, log the status 
      Log.d("Location Updates", "Google Play services is available."); 
      return true; 
     } else { 
      // Get the error dialog from Google Play services 
      Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(resultCode, 
        this, 
        CONNECTION_FAILURE_RESOLUTION_REQUEST); 

      // If Google Play services can provide an error dialog 
      if (errorDialog != null) { 
       // Create a new DialogFragment for the error dialog 
       ErrorDialogFragment errorFragment = new ErrorDialogFragment(); 
       errorFragment.setDialog(errorDialog); 
       errorFragment.show(getSupportFragmentManager(), "Location Updates"); 
      } 

      return false; 
     } 
    } 

    /* 
    * Called by Location Services when the request to connect the 
    * client finishes successfully. At this point, you can 
    * request the current location or start periodic updates 
    */ 
    @Override 
    public void onConnected(Bundle dataBundle) { 
     // Display the connection status 
     Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show(); 
     Location location = mLocationClient.getLastLocation(); 
     LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); 
     CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 17); 
     map.animateCamera(cameraUpdate); 
    } 

    /* 
    * Called by Location Services if the connection to the 
    * location client drops because of an error. 
    */ 
    @Override 
    public void onDisconnected() { 
     // Display the connection status 
     Toast.makeText(this, "Disconnected. Please re-connect.", 
       Toast.LENGTH_SHORT).show(); 
    } 

    /* 
    * Called by Location Services if the attempt to 
    * Location Services fails. 
    */ 
    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
    /* 
    * Google Play services can resolve some errors it detects. 
    * If the error has a resolution, try sending an Intent to 
    * start a Google Play services activity that can resolve 
    * error. 
    */ 
     if (connectionResult.hasResolution()) { 
      try { 
       // Start an Activity that tries to resolve the error 
       connectionResult.startResolutionForResult(
         this, 
         CONNECTION_FAILURE_RESOLUTION_REQUEST); 
      /* 
      * Thrown if Google Play services canceled the original 
      * PendingIntent 
      */ 
      } catch (IntentSender.SendIntentException e) { 
       // Log the error 
       e.printStackTrace(); 
      } 
     } else { 
      Toast.makeText(getApplicationContext(), "Sorry. Location services not available to you", Toast.LENGTH_LONG).show(); 
     } 
    } 




    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, 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(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 



} 

和我的清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.whatsonwhere.app" > 
    <permission 
     android:name="com.whatsonwhere.app.MAPS_RECEIVE" 
     android:protectionLevel="signature"/> 
    <uses-permission android:name="com.whatsonwhere.app.MAPS_RECEIVE"/> 

    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> 

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
    <uses-feature 
     android:glEsVersion="0x00020000" 
     android:required="true"/> 
    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.whatsonwhere.app.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 


     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> 
     <meta-data 
      android:name="com.google.android.maps.v2.API_KEY" 
      android:value="mykey_here(removed)" /> 
    </application> 

</manifest> 

Activity_main:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context="com.whatsonwhere.app.MainActivity"> 

    <fragment xmlns:android="http://schemas.android.com/apk/res/android" 

     android:id="@+id/mapView" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:name="com.google.android.gms.maps.MapFragment" 
     android:layout_below="@+id/textView" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_above="@+id/button" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="@string/TopText" 
     android:id="@+id/textView2" 
     /> 

    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/nearme" 
     android:id="@+id/button" 
     android:layout_centerVertical="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/search" 
     android:id="@+id/button2" 
     android:layout_below="@+id/button" 
     android:layout_centerHorizontal="true" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="@string/loc" 
     android:id="@+id/textView" 
     android:layout_below="@+id/textView2" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

</RelativeLayout> 

最後的logcat的:

03-25 11:28:19.417 6650-6650/com.whatsonwhere.app D/AndroidRuntime﹕ Shutting down VM 
03-25 11:28:19.417 6650-6650/com.whatsonwhere.app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x415e88b0) 
03-25 11:28:19.417 6650-6650/com.whatsonwhere.app E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.whatsonwhere.app/com.whatsonwhere.app.MainActivity}: java.lang.NullPointerException 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316) 
      at android.app.ActivityThread.access$600(ActivityThread.java:150) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298) 
      at android.os.Handler.dispatchMessage(Handler.java:99) 
      at android.os.Looper.loop(Looper.java:213) 
      at android.app.ActivityThread.main(ActivityThread.java:5225) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:525) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.NullPointerException 
      at com.whatsonwhere.app.MainActivity.onCreate(MainActivity.java:71) 
      at android.app.Activity.performCreate(Activity.java:5133) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2230) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316) 
            at android.app.ActivityThread.access$600(ActivityThread.java:150) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298) 
            at android.os.Handler.dispatchMessage(Handler.java:99) 
            at android.os.Looper.loop(Looper.java:213) 
            at android.app.ActivityThread.main(ActivityThread.java:5225) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:525) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 
            at dalvik.system.NativeStart.main(Native Method) 
03-25 11:28:19.537 6650-6682/com.whatsonwhere.app W/ActivityThread﹕ ClassLoader.loadClass: The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader()); 
03-25 11:28:21.477 6650-6650/com.whatsonwhere.app I/Process﹕ Sending signal. PID: 6650 SIG: 9 
+0

第71行MainActivity.java是什麼? – Raghunandan

回答

1

如果指定:

setContentView(R.layout.activity_main); 

而且

mapFragment =((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapView)); 
map = mapFragment.getMap(); 

編譯器查找R.id.mapViewR.layout.activity_main .The名稱是區分大小寫。
線:

map=mapFragment.getMap(): 

拋出一個NPE作爲mapView並不將其各自的佈局中。檢查名稱是否爲id(s)以及是否在提及的layout中聲明。

此外,alternatively..you可以使用:

map = ((SupportMapFragment) this.getSupportFragmentManager() 
        .findFragmentById(R.id.mapView)).getMap(); 

節省了變量聲明和L-O-C,如果不另外需要。因爲你的XML文件中

<fragment 
      android:id="@+id/mapView" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/textView" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      android:layout_above="@+id/button" 
      class="com.google.android.gms.maps.SupportMapFragment" /> 
+0

不知道我在這裏支持 - 我將我的活動主要添加到頂端帖子 – Jimmyeao

+0

@Jimmyeao檢查編輯 – user2450263

+0

@Jimmyeao你有'android:name =「com.google.android.gms.maps.MapFragment」'但是當你初始化你有'mapFragment =((SupportMapFragment)getSupportFragmentManager()。findFragmentById(R.id.mapView));'。它應該是'SupportMapFragment'。地圖對象可以是null – Raghunandan

0

mapFragmentnullfindFragmentById()調用之後。請確保您在activity_main.xml內有id = mapView(區分大小寫)。如果你有它,你也可以嘗試將MapFragment的工作從onCreate移動到onStartonResume

+0

評論這條線沒有任何影響 - 該應用程序仍然崩潰與相同的錯誤 – Jimmyeao

+0

@Jimmyeao但你不應該評論這一行。如果你評論它,mapFragment肯定是'空' – nikis

+0

啊..有道理 - 生病了吧。 – Jimmyeao

0

有時,當活動被創建時,映射不會被初始化。這導致空指針異常

您必須將代碼映射片段等從onCreate移動到onResume

這將確保活動創建並在前臺。

0

mapFragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapView)); 

這裏更改爲

mapFragment = ((MapFragment) getSupportFragmentManager().findFragmentById(R.id.mapView)); 

您使用

android:name="com.google.android.gms.maps.MapFragment" 

添加片段喜歡這個10

+0

這並不解決NPE。 – Raghunandan

+0

但是糾正一下他犯了一個錯誤。這並沒有錯。 – Piyush

相關問題