0

这显然是一个被问过多次的问题,但只能根据具体情况给出解决方案。我正在设计一个名为 Trackr 的应用程序,它所做的只是在按下按钮时打开一个服务。我不断收到此错误:

11-13 16:44:06.546: D/ActivityThread(4656): setTargetHeapUtilization:0.25
11-13 16:44:06.546: D/ActivityThread(4656): setTargetHeapIdealFree:8388608
11-13 16:44:06.546: D/ActivityThread(4656): setTargetHeapConcurrentStart:2097152
11-13 16:44:06.926: W/dalvikvm(4656): threadid=1: thread exiting with uncaught exception (group=0x41225438)
11-13 16:44:06.936: E/AndroidRuntime(4656): FATAL EXCEPTION: main
11-13 16:44:06.936: E/AndroidRuntime(4656): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.example.trackr/com.example.trackr.StartScreen}: java.lang.NullPointerException
11-13 16:44:06.936: E/AndroidRuntime(4656): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
11-13 16:44:06.936: E/AndroidRuntime(4656): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
11-13 16:44:06.936: E/AndroidRuntime(4656): at android.app.ActivityThread.access$700(ActivityThread.java:143)
11-13 16:44:06.936: E/AndroidRuntime(4656): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
11-13 16:44:06.936: E/AndroidRuntime(4656): at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 16:44:06.936: E/AndroidRuntime(4656): at android.os.Looper.loop(Looper.java:137)
11-13 16:44:06.936: E/AndroidRuntime(4656): at android.app.ActivityThread.main(ActivityThread.java:4962)
11-13 16:44:06.936: E/AndroidRuntime(4656): at java.lang.reflect.Method.invokeNative(Native Metood)
11-13 16:44:06.936: E/AndroidRuntime(4656): at java.lang.reflect.Method.invoke(Method.java:511)
11-13 16:44:06.936: E/AndroidRuntime(4656): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
11-13 16:44:06.936: E/AndroidRuntime(4656): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
11-13 16:44:06.936: E/AndroidRuntime(4656): at dalvik.system.NativeStart.main(Native Method)
11-13 16:44:06.936: E/AndroidRuntime(4656): Caused by: java.lang.NullPointerException
11-13 16:44:06.936: E/AndroidRuntime(4656): at com.example.trackr.StartScreen.onCreate(StartScreen.java:17)
11-13 16:44:06.936: E/AndroidRuntime(4656): at android.app.Activity.performCreate(Activity.java:5160)
11-13 16:44:06.936: E/AndroidRuntime(4656): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
11-13 16:44:06.936: E/AndroidRuntime(4656): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
11-13 16:44:06.936: E/AndroidRuntime(4656): ... 11 more

我有 2 个课程,StartScreen(一项活动)和 StartService(一项服务)。我还有一个应用程序清单和每个布局文件。

package com.example.trackr;

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

public class StartScreen extends Activity {

    Intent startMonitor;
    Button newBut;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_screen);
        startMonitor.setClassName("com.example.trackr","StartService");

    }

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

    public void callService(View view){
        startService(startMonitor);
    }
}

这是布局文件:

<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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="90dp"
        android:onClick="callService"
        android:text="Enable" />

</RelativeLayout>

然后这是清单文件:

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

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

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

    <service android:name="com.example.trackr.StartService"></service>

</application>

</manifest>

我知道这似乎是一个简单的问题,但它让我难住了。我真的很感激一些帮助。谢谢!

4

1 回答 1

6

你到了NPE这里

startMonitor.setClassName("com.example.trackr","StartService");

因为startMonitor还没有初始化。将其更改为

startMonitor = new Intent();  // initialize your variable before trying to use it
startMonitor.setClassName("com.example.trackr","StartService");
于 2013-11-13T22:01:14.657 回答