2012-07-12 45 views
0

任何人都可以告訴我什麼是錯的? 該應用程序總是在設備SE xperia X10上崩潰。也許這是我嵌入ADMOB的方式。 它甚至不顯示一秒鐘。請提供任何線索嗎? 在XML和清單中,一切似乎都沒事。 謝謝Android應用程序總是崩潰

package com.taiic.template.admob; 


import com.google.ads.AdRequest; 
import com.google.ads.AdView; 
import java.io.IOException; 
import java.util.*; 

import android.widget.*; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.os.Bundle; 
import android.location.*; 
import android.content.*; 

import android.app.Activity; 
import android.os.Bundle; 

public class adMain extends Activity { 

    Button addressButton; 
    TextView locationText; 
    TextView addressText; 
    Location currentLocation; 
    double currentLatitude; 
    double currentLongitude; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 




    addressText = (TextView)findViewById(R.id.addressText); 
    locationText = (TextView)findViewById(R.id.locationText); 
    addressButton = (Button)findViewById(R.id.addressButton); 

    this.addressText.setText("ready"); 

    LocationManager locationManager = 
     (LocationManager)this.getSystemService(Context.LOCATION_SERVICE); 

    LocationListener locationListener = new LocationListener() { 
     public void onLocationChanged(Location location) { 
      updateLocation(location); 
     } 
     public void onStatusChanged(
       String provider, int status, Bundle extras) {} 
     public void onProviderEnabled(String provider) {} 
     public void onProviderDisabled(String provider) {} 
    }; 

    locationManager.requestLocationUpdates(
      LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 

    this.addressButton.setOnClickListener(new OnClickListener() { 
     public void onClick(View v){ 
      getAddress(); 
     } 
    }); 
} 

void getAddress(){ 
    try{ 
     Geocoder gcd = new Geocoder(this, Locale.getDefault()); 
     List<Address> addresses = 
      gcd.getFromLocation(currentLatitude, currentLongitude,100); 
     if (addresses.size() > 0) { 
      StringBuilder result = new StringBuilder(); 
      for(int i = 0; i < addresses.size(); i++){ 
       Address address = addresses.get(i); 
       int maxIndex = address.getMaxAddressLineIndex(); 
       for (int x = 0; x <= maxIndex; x++){ 
        result.append(address.getAddressLine(x)); 
        result.append(","); 
       }    
       result.append(address.getLocality()); 
       result.append(","); 
       result.append(address.getPostalCode()); 
       result.append("\n\n"); 
      } 
      addressText.setText(result.toString()); 
     } 
    } 
    catch(IOException ex){ 
     addressText.setText(ex.getMessage().toString()); 
    } 
} 

void updateLocation(Location location){ 
    currentLocation = location; 
    currentLatitude = currentLocation.getLatitude(); 
    currentLongitude = currentLocation.getLongitude(); 
    locationText.setText(currentLatitude + ", " + currentLongitude); 
} 

AdView layout = (AdView)this.findViewById(R.id.adView); 
AdRequest adRequest = new AdRequest(); 
adRequest.setTesting(true); 
layout.loadAd(adRequest); 
} 

這裏是目錄下載

07-13 00:10:59.451: D/AndroidRuntime(3735): Shutting down VM 
07-13 00:10:59.451: W/dalvikvm(3735): threadid=1: thread exiting with uncaught exception (group=0x4001d560) 
07-13 00:10:59.461: E/AndroidRuntime(3735): FATAL EXCEPTION: main 
07-13 00:10:59.461: E/AndroidRuntime(3735): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.taiic.template.admob/com.taiic.template.admob.adMain}: java.lang.NullPointerException 
07-13 00:10:59.461: E/AndroidRuntime(3735):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1581) 
07-13 00:10:59.461: E/AndroidRuntime(3735):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675) 
07-13 00:10:59.461: E/AndroidRuntime(3735):  at android.app.ActivityThread.access$1500(ActivityThread.java:121) 
07-13 00:10:59.461: E/AndroidRuntime(3735):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943) 
07-13 00:10:59.461: E/AndroidRuntime(3735):  at android.os.Handler.dispatchMessage(Handler.java:99) 
07-13 00:10:59.461: E/AndroidRuntime(3735):  at android.os.Looper.loop(Looper.java:123) 
07-13 00:10:59.461: E/AndroidRuntime(3735):  at android.app.ActivityThread.main(ActivityThread.java:3701) 
07-13 00:10:59.461: E/AndroidRuntime(3735):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-13 00:10:59.461: E/AndroidRuntime(3735):  at java.lang.reflect.Method.invoke(Method.java:507) 
07-13 00:10:59.461: E/AndroidRuntime(3735):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862) 
07-13 00:10:59.461: E/AndroidRuntime(3735):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 
07-13 00:10:59.461: E/AndroidRuntime(3735):  at dalvik.system.NativeStart.main(Native Method) 
07-13 00:10:59.461: E/AndroidRuntime(3735): Caused by: java.lang.NullPointerException 
07-13 00:10:59.461: E/AndroidRuntime(3735):  at android.app.Activity.findViewById(Activity.java:1647) 
07-13 00:10:59.461: E/AndroidRuntime(3735):  at com.taiic.template.admob.adMain.<init>(adMain.java:102) 
07-13 00:10:59.461: E/AndroidRuntime(3735):  at java.lang.Class.newInstanceImpl(Native Method) 
07-13 00:10:59.461: E/AndroidRuntime(3735):  at java.lang.Class.newInstance(Class.java:1409) 
07-13 00:10:59.461: E/AndroidRuntime(3735):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 
07-13 00:10:59.461: E/AndroidRuntime(3735):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573) 
07-13 00:10:59.461: E/AndroidRuntime(3735):  ... 11 more 

回答

2

日誌是關於什麼地方出了錯非常清楚:

Caused by: java.lang.NullPointerException 
at android.app.Activity.findViewById(Activity.java:1647) 
at com.taiic.template.admob.adMain.<init>(adMain.java:102) 

在類adMain,上線102,你是試圖誇大活動佈局中不存在的視圖:

AdView layout = (AdView)this.findViewById(R.id.adView); 

所以問題是:你是否實際添加了一個AdView視圖(ID爲android:id="@+id/adView")到您設置爲活動內容視圖的main.xml文件中定義的佈局?錯誤表明你沒有。

+0

如果他確實在XML中定義了一個AdView,AdView是否有'android:id =「adView」'? – 2012-07-12 22:48:41

+0

@EricLeichtenschlag:好的;理論上他可能已經在其他一些佈局中定義了'android:id =「@ + id/adView」'。另一方面,代碼片段表明他仍在測試啓用廣告,所以我猜測引用並不是全部。我會將其添加到答案中,以防萬一。 – 2012-07-12 22:58:40

+0

我只是解決它,我加了 – user1427211 2012-07-12 23:03:42