繁体   English   中英

按下按钮时android应用崩溃

[英]android app crashes when a button is pressed

我制作了一个应用程序,该应用程序的一部分是获取经度和纬度,将它们添加到一个字符串中,并使用该字符串从天气服务器中获取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>

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

您是否正在模拟器或设备上对其进行测试? 您的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
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM