3

我对 Android 开发还很陌生,所以我确信这将是我错过的一些愚蠢的事情,但我有两个活动:StartScreen 和 Map。StartScreen 只是简单的文本和一个按钮,然后应该将您带到 Map 活动。

我已经尝试将 Map 活动减少到几乎没有,而且我确信那里没有任何东西会导致错误,但我就是不知道是什么。

这是错误的 logcat 输出,当我按下链接两个活动的按钮时发生:

04-28 20:40:24.097: E/AndroidRuntime(13458): FATAL EXCEPTION: main
04-28 20:40:24.097: E/AndroidRuntime(13458): java.lang.RuntimeException: Unable to       instantiate activity ComponentInfo{com.rr.freespace/com.rr.freespace.Map}:  java.lang.NullPointerException
04-28 20:40:24.097: E/AndroidRuntime(13458):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
04-28 20:40:24.097: E/AndroidRuntime(13458):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
04-28 20:40:24.097: E/AndroidRuntime(13458):    at android.app.ActivityThread.access$600(ActivityThread.java:139)
04-28 20:40:24.097: E/AndroidRuntime(13458):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
04-28 20:40:24.097: E/AndroidRuntime(13458):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-28 20:40:24.097: E/AndroidRuntime(13458):    at android.os.Looper.loop(Looper.java:154)
04-28 20:40:24.097: E/AndroidRuntime(13458):    at android.app.ActivityThread.main(ActivityThread.java:4945)
04-28 20:40:24.097: E/AndroidRuntime(13458):    at java.lang.reflect.Method.invokeNative(Native Method)
04-28 20:40:24.097: E/AndroidRuntime(13458):    at java.lang.reflect.Method.invoke(Method.java:511)
04-28 20:40:24.097: E/AndroidRuntime(13458):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-28 20:40:24.097: E/AndroidRuntime(13458):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-28 20:40:24.097: E/AndroidRuntime(13458):    at dalvik.system.NativeStart.main(Native Method)
04-28 20:40:24.097: E/AndroidRuntime(13458): Caused by: java.lang.NullPointerException
04-28 20:40:24.097: E/AndroidRuntime(13458):    at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
04-28 20:40:24.097: E/AndroidRuntime(13458):    at android.content.ComponentName.<init>(ComponentName.java:75)
04-28 20:40:24.097: E/AndroidRuntime(13458):    at android.content.Intent.<init>(Intent.java:3475)
04-28 20:40:24.097: E/AndroidRuntime(13458):    at com.rr.freespace.Map.<init>(Map.java:12)
04-28 20:40:24.097: E/AndroidRuntime(13458):    at java.lang.Class.newInstanceImpl(Native Method)
04-28 20:40:24.097: E/AndroidRuntime(13458):    at java.lang.Class.newInstance(Class.java:1319)
04-28 20:40:24.097: E/AndroidRuntime(13458):    at android.app.Instrumentation.newActivity(Instrumentation.java:1039)
04-28 20:40:24.097: E/AndroidRuntime(13458):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)

这是我的 StartScreen.java:

package com.rr.freespace;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;

public class StartScreen extends Activity {

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

    public void doIt(View view) {     // Do something in response to button }
        Intent intent = new Intent(this, Map.class);
        startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.start_screen, menu);
        return true;
    }

}

activity_start_screen.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=".StartScreen" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="180dp"
        android:text="@string/hometext2"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_centerVertical="true"
        android:layout_margin="20dp"
        android:text="@string/doit"
        android:onClick="doIt" />

</RelativeLayout>

最后,清单文件:

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

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />
    <permission
        android:name="com.example.mapdemo.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="com.rr.freespace.permission.MAPS_RECEIVE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <uses-library android:name="com.google.android.maps" />

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyBaoeVjdRY312vJ5KMLXxmzpKHhPiYqASo"/>
    <activity
        android:name="com.rr.freespace.StartScreen"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.rr.freespace.Map"
        android:label="@string/app_name" >
    </activity>
</application>

</manifest>

我希望有人能弄清楚这里发生了什么,因为我不能!干杯!

编辑:这是 map.java 代码,这里可能还有更多我还没有遇到的错误,因为我昨天在访问 android 设备之前一起破解了其中的大部分内容:

package com.rr.freespace;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.Menu;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMapOptions;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;


public class Map extends Activity {
    final Context context = this;
Intent goback = new Intent(this, StartScreen.class);
protected LocationManager locationManager;
protected LocationListener listener;
private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    final boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

    if (!gpsEnabled) {
        AlertDialog.Builder gpssettings = new AlertDialog.Builder(context);
        gpssettings.setMessage(R.string.gpstext)
            .setTitle(R.string.gpstitle);
        gpssettings.setPositiveButton(R.string.gpsok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // User clicked OK button
                enableLocationSettings();
            }
        });
        gpssettings.setNegativeButton(R.string.gpscancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // User cancelled the dialog
                startActivity(goback);
            }
        });
    }

    mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    GoogleMapOptions options = new GoogleMapOptions();
    options.mapType(GoogleMap.MAP_TYPE_NORMAL)
    .compassEnabled(false)
    .rotateGesturesEnabled(false)
    .tiltGesturesEnabled(false);

    //Get GPS data
    final LocationListener listener = new LocationListener() {

        @Override
        public void onLocationChanged(Location location) {
            // A new location update is received.  Do something useful with it.  Update the UI with
            // the location update.
            final LatLng position = new LatLng(location.getLatitude(),location.getLongitude());
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(position, 15));
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    };
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            1000,          // 10-second interval.
            5,             // 5 meters.
            listener);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.map, menu);
    return true;
    }

    private void enableLocationSettings() {
        Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(settingsIntent);
    }

    protected void onStop() {
        super.onStop();
        locationManager.removeUpdates(listener);
    }

}
4

1 回答 1

0
Intent goback = new Intent(this, StartScreen.class);

这会导致问题。

你不能在这里使用“this”,最好在使用它们之前创建意图。

但是,如果您想尽快拥有它,请在 onCreate 中进行。

于 2013-04-28T20:33:19.573 回答