我做了一個應用程序,應用程序的一部分是獲取經度和緯度添加到一個字符串,並使用該字符串從天氣服務器獲取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
後的堆棧跟蹤,請。並注意'getLastKnownLocation'可以返回'null'。 –
除非我是盲人,否則你根本沒有打過onClick。對不起剛剛看到它。 –
@RED_它是在xml文件中定義的。 –