2014-02-26 200 views
0

我做了一個應用程序,應用程序的一部分是獲取經度和緯度添加到一個字符串,並使用該字符串從天氣服務器獲取xml文件,所以你可以看到什麼天氣,溫度,溼度......它在你的地方。當一個按鈕被按下時,android應用程序崩潰

activity_main.xml中:

<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:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:text="@string/instruction_nexttobutton_english" /> 

<Button 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textView1" 
    android:layout_alignRight="@+id/textView1" 
    android:layout_below="@+id/textView1" 
    android:onClick="getPositionAndGetWheater" 
    android:text="@string/getweather_button_english" /> 

<WebView 
    android:id="@+id/webview" 
    android:layout_width="wrap_content" 
    android:layout_height="115dp" 
    android:layout_alignLeft="@+id/button2" 
    android:layout_alignRight="@+id/button2" 
    android:layout_below="@+id/button2" /> 

</RelativeLayout> 

MainActivity.java:

package com.example.thelexapp; 

import android.location.Location; 
import android.location.LocationManager; 
import android.net.Uri; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.view.Menu; 
import android.webkit.WebView; 

public class MainActivity extends Activity { 


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

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

public void getPositionAndGetWheater(View view) { 
    LocationManager lm =     (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    double longitude = location.getLongitude(); 
    double latitude = location.getLatitude(); 
    String longitudestring = String.valueOf(longitude); 
    String latitudestring = String.valueOf(latitude); 
    String URLforweather =  "http://api.worldweatheronline.com/free/v1/weather.ashx?q=" + longitudestring + "," + latitudestring + "&format=xml&num_of_days=1&key=IHAVEAAPIKEYBUTI'MNOTPOSTINGITONTHEFORUMFORSECURITYREASONS"; 
    WebView webview=(WebView)findViewById(R.id.webview); 
    webview.loadUrl(URLforweather); 

} 


} 

「APPNAME」 的Manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.thelexapp" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="18" /> 

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.INTERNET" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.example.thelexapp.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> 
</application> 

</manifest> 

logcat的WINDOWS:

02-26 15:00:26.710: D/dalvikvm(977): Not late-enabling CheckJNI (already on) 
02-26 15:00:29.730: V/WebViewChromium(977): Binding Chromium to the background looperLooper{b1dceb80} 
02-26 15:00:29.750: I/chromium(977): [INFO:library_loader_hooks.cc(112)] Chromium  logging enabled: level = 0, default verbosity = 0 
02-26 15:00:29.770: I/BrowserProcessMain(977): Initializing chromium process, renderers=0 
02-26 15:00:29.960: E/chromium(977): [ERROR:gl_surface_egl.cc(153)] No suitable EGL configs found. 
02-26 15:00:29.960: E/chromium(977): [ERROR:gl_surface_egl.cc(620)] GLSurfaceEGL::InitializeOneOff failed. 
02-26 15:00:29.960: E/chromium(977): [ERROR:gl_surface_egl.cc(153)] No suitable EGL configs found. 
02-26 15:00:29.960: E/chromium(977): [ERROR:gl_surface_egl.cc(620)] GLSurfaceEGL::InitializeOneOff failed. 
02-26 15:00:29.970: E/chromium(977): [ERROR:gpu_info_collector.cc(86)] gfx::GLSurface::InitializeOneOff() failed 
02-26 15:00:30.000: W/chromium(977): [WARNING:proxy_service.cc(888)] PAC support disabled because there is no system implementation 
02-26 15:00:30.150: D/dalvikvm(977): GC_FOR_ALLOC freed 88K, 5% free 3235K/3392K, paused 48ms, total 50ms 
02-26 15:00:30.160: I/dalvikvm-heap(977): Grow heap (frag case) to 4.297MB for 1127536-byte allocation 
02-26 15:00:30.230: D/dalvikvm(977): GC_FOR_ALLOC freed 3K, 4% free 4333K/4496K, paused 67ms, total 67ms 
02-26 15:00:30.800: D/gralloc_goldfish(977): Emulator without GPU emulation detected. 
02-26 15:00:30.880: W/AwContents(977): nativeOnDraw failed; clearing to background color. 
02-26 15:00:31.870: W/AwContents(977): nativeOnDraw failed; clearing to background color. 
02-26 15:00:31.930: W/AwContents(977): nativeOnDraw failed; clearing to background color. 
02-26 15:00:32.000: W/AwContents(977): nativeOnDraw failed; clearing to background color. 
02-26 15:00:32.020: W/AwContents(977): nativeOnDraw failed; clearing to background color. 
02-26 15:00:32.090: W/AwContents(977): nativeOnDraw failed; clearing to background color. 
02-26 15:00:32.130: W/AwContents(977): nativeOnDraw failed; clearing to background color. 
02-26 15:00:31.817: D/AndroidRuntime(977): Shutting down VM 
02-26 15:00:31.817: W/dalvikvm(977): threadid=1: thread exiting with uncaught exception (group=0xb1af5b90) 
02-26 15:00:31.827: E/AndroidRuntime(977): FATAL EXCEPTION: main 
02-26 15:00:31.827: E/AndroidRuntime(977): Process: com.example.thelexapp, PID: 977 
02-26 15:00:31.827: E/AndroidRuntime(977): java.lang.IllegalStateException: Could not execute method of the activity 
02-26 15:00:31.827: E/AndroidRuntime(977): at android.view.View$1.onClick(View.java:3814) 
02-26 15:00:31.827: E/AndroidRuntime(977): at android.view.View.performClick(View.java:4424) 
02-26 15:00:31.827: E/AndroidRuntime(977): at android.view.View$PerformClick.run(View.java:18383) 
02-26 15:00:31.827: E/AndroidRuntime(977): at android.os.Handler.handleCallback(Handler.java:733) 
02-26 15:00:31.827: E/AndroidRuntime(977): at android.os.Handler.dispatchMessage(Handler.java:95) 
02-26 15:00:31.827: E/AndroidRuntime(977): at android.os.Looper.loop(Looper.java:137) 
02-26 15:00:31.827: E/AndroidRuntime(977): at android.app.ActivityThread.main(ActivityThread.java:4998) 
02-26 15:00:31.827: E/AndroidRuntime(977): at java.lang.reflect.Method.invokeNative(Native Method) 
02-26 15:00:31.827: E/AndroidRuntime(977): at java.lang.reflect.Method.invoke(Method.java:515) 
02-26 15:00:31.827: E/AndroidRuntime(977): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) 
02-26 15:00:31.827: E/AndroidRuntime(977): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593) 
02-26 15:00:31.827: E/AndroidRuntime(977): at dalvik.system.NativeStart.main(Native Method) 
02-26 15:00:31.827: E/AndroidRuntime(977): Caused by: java.lang.reflect.InvocationTargetException 
02-26 15:00:31.827: E/AndroidRuntime(977): at java.lang.reflect.Method.invokeNative(Native Method) 
02-26 15:00:31.827: E/AndroidRuntime(977): at java.lang.reflect.Method.invoke(Method.java:515) 
02-26 15:00:31.827: E/AndroidRuntime(977): at android.view.View$1.onClick(View.java:3809) 
02-26 15:00:31.827: E/AndroidRuntime(977): ... 11 more 
02-26 15:00:31.827: E/AndroidRuntime(977): Caused by: java.lang.NullPointerException 
02-26 15:00:31.827: E/AndroidRuntime(977): at com.example.thelexapp.MainActivity.getPositionAndGetWheater(MainActivity.java:33) 
02-26 15:00:31.827: E/AndroidRuntime(977): ... 14 more 
02-26 15:00:37.167: I/Process(977): Sending signal. PID: 977 SIG: 9 
+1

後的堆棧跟蹤,請。並注意'getLastKnownLocation'可以返回'null'。 –

+0

除非我是盲人,否則你根本沒有打過onClick。對不起剛剛看到它。 –

+0

@RED_它是在xml文件中定義的。 –

回答

0

您是在仿真器還是設備上測試它?有一個可能性,你的Location location爲空。請確保您的清單中有相應的權限。爲了處理這種情況,你應該檢查你的位置null

public void getPositionAndGetWheater(View view) { 
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    if (location != null) { 

     double longitude = location.getLongitude(); 
     double latitude = location.getLatitude(); 
     String longitudestring = String.valueOf(longitude); 
     String latitudestring = String.valueOf(latitude); 
     String URLforweather = "http://api.worldweatheronline.com/free/v1/weather.ashx?q=" + longitudestring + "," + latitudestring + "&format=xml&num_of_days=1&key=IHAVEAAPIKEYBUTI'MNOTPOSTINGITONTHEFORUMFORSECURITYREASONS"; 
     WebView webview = (WebView) findViewById(R.id.webview); 
     webview.loadUrl(URLforweather); 
    } else { 
     //show error 
    } 

} 
+0

我試着它謝謝你;) – user3357521

+0

它的工作,但按鈕不顯示任何東西,這很可能是因爲我測試它與安全API,我試着用coorect api鍵,如果它仍然不起作用,它可能正在執行else statemtn非常感謝你;)我想要提出這個評論,但我需要聲望15:s **非常感謝你很多** – user3357521

+0

是的,它正在歸零,我會在真正的手機上試試它,但我現在需要立即去感謝你;) – user3357521

相關問題