2014-02-26 3 views
0

앱을 만들었는데 앱의 일부가 경도와 위도를 하나의 문자열에 추가하고 해당 문자열을 날씨 서버에서 XML 파일로 가져 와서 볼 수 있습니다. 어떤 날씨, 온도, 습도 ... 그것은 당신의 장소에 있습니다.버튼을 누를 때 안드로이드 앱이 작동을 멈 춥니 다.

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> 

로그 캣 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

stacktrace를 게시하십시오. 그리고'getLastKnownLocation'은'null'을 반환 할 수 있다는 것을 알아 두십시오. –

+0

나는 눈이 멀지 않은 한 onClick을 전혀 호출하지 않았습니다. 죄송합니다. 그냥 보았습니다. –

+0

@RED_ xml 파일에 정의되어 있습니다. –

답변

0

에뮬레이터 또는 장치에서 테스트 중입니까? 귀하의 Location location이 null이라는 확실성이 있습니다. 매니페스트에 해당 권한이 있는지 확인하십시오. 이러한 상황을 처리하려면 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

그것은 작동하지만 버튼은 아무것도 표시하지 않습니다, 그게 probi 내가 보안 API를 테스트하기 때문에, 내가 coorect API를 키와 함께 그것을 시도하고 여전히 작동하지 않는 경우 아마도 다른 statemtn 대단히 감사합니다;) 나는이 주석 위로 화살표를 원하지만 명성이 필요합니다. 많이 ** – user3357521

+0

맙소사, 그것은 returining null입니다, 나는 진짜 전화로 그것을 시도하지만 지금 당장 고맙습니다 ou를 갈 필요가;) – user3357521

관련 문제