0

我遵循谷歌上的开发者指南,并尝试创建一个应用程序,目前只有 1 个活动。它没有太多,只有很少的项目,而且这个类的功能几乎是空的,但是当我激活它时它会崩溃。清单是这样的:

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

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />

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

我的活动类文件是这样的:

package com.example.remoteswitch;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.SeekBar;
import android.widget.TextView;


public class MainActivity extends Activity {
    public final static String EXTRA_MESSAGE = "com.example.remoteswitch.MESSAGE";
    int maxTime = 90;
    CheckBox checkbox = (CheckBox) findViewById(R.id.timerCB);
    SeekBar timebar = (SeekBar)findViewById(R.id.timerTime);
    TextView statText = (TextView)findViewById(R.id.statusText);
    TextView timerText = (TextView)findViewById(R.id.timerText);

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        timebar.setEnabled(false);
        timebar.setMax(maxTime);

    }

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

    public void activateTimer(View view) {
        // Do something in response to button
        //Intent intent = new Intent(this, DisplayMessageActivity.class);

        if (checkbox.isChecked())
            timebar.setEnabled(true);

    }

    public void startBoiler(View view) {
        // Do something in response to button
        //Intent intent = new Intent(this, SecondActivity.class);
        //intent.putExtra(EXTRA_MESSAGE,"Hi");
        //startActivity(intent);
    }
}

我的下一步是添加另一个活动,但我只是想看看它是否基本正常工作.. logcat 是这样的:

01-30 11:03:28.477: E/AndroidRuntime(1539): FATAL EXCEPTION: main
01-30 11:03:28.477: E/AndroidRuntime(1539): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.remoteswitch/com.example.remoteswitch.MainActivity}: java.lang.NullPointerException
01-30 11:03:28.477: E/AndroidRuntime(1539):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
01-30 11:03:28.477: E/AndroidRuntime(1539):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-30 11:03:28.477: E/AndroidRuntime(1539):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-30 11:03:28.477: E/AndroidRuntime(1539):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-30 11:03:28.477: E/AndroidRuntime(1539):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-30 11:03:28.477: E/AndroidRuntime(1539):     at android.os.Looper.loop(Looper.java:137)
01-30 11:03:28.477: E/AndroidRuntime(1539):     at android.app.ActivityThread.main(ActivityThread.java:5039)
01-30 11:03:28.477: E/AndroidRuntime(1539):     at java.lang.reflect.Method.invokeNative(Native Method)
01-30 11:03:28.477: E/AndroidRuntime(1539):     at java.lang.reflect.Method.invoke(Method.java:511)
01-30 11:03:28.477: E/AndroidRuntime(1539):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-30 11:03:28.477: E/AndroidRuntime(1539):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-30 11:03:28.477: E/AndroidRuntime(1539):     at dalvik.system.NativeStart.main(Native Method)
01-30 11:03:28.477: E/AndroidRuntime(1539): Caused by: java.lang.NullPointerException
01-30 11:03:28.477: E/AndroidRuntime(1539):     at android.app.Activity.findViewById(Activity.java:1839)
01-30 11:03:28.477: E/AndroidRuntime(1539):     at com.example.remoteswitch.MainActivity.<init>(MainActivity.java:15)
01-30 11:03:28.477: E/AndroidRuntime(1539):     at java.lang.Class.newInstanceImpl(Native Method)
01-30 11:03:28.477: E/AndroidRuntime(1539):     at java.lang.Class.newInstance(Class.java:1319)
01-30 11:03:28.477: E/AndroidRuntime(1539):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
01-30 11:03:28.477: E/AndroidRuntime(1539):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)

谁能帮我找出我的错误?

4

7 回答 7

1
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.remoteswitch.MESSAGE";
int maxTime = 90;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    CheckBox checkbox = (CheckBox) findViewById(R.id.timerCB);
    SeekBar timebar = (SeekBar)findViewById(R.id.timerTime);
    TextView statText = (TextView)findViewById(R.id.statusText);
    TextView timerText = (TextView)findViewById(R.id.timerText);
    timebar.setEnabled(false);
    timebar.setMax(maxTime);

}
于 2013-01-30T11:19:27.973 回答
1

put that lines of code

CheckBox checkbox = (CheckBox) findViewById(R.id.timerCB);
SeekBar timebar = (SeekBar)findViewById(R.id.timerTime);
TextView statText = (TextView)findViewById(R.id.statusText);
TextView timerText = (TextView)findViewById(R.id.timerText);

in you onCreate method and after the line

setContentView(R.layout.activity_main);
于 2013-01-30T11:19:29.670 回答
0

仅在设置内容视图后尝试使用 findViewById(),即:

setContentView(R.layout.activity_main);
于 2013-01-30T11:20:26.080 回答
0

在调用 setContentView(); 之前,您正在投射所有小部件;

public class MainActivity extends Activity {
    public final static String EXTRA_MESSAGE = "com.example.remoteswitch.MESSAGE";
    int maxTime = 90;
    CheckBox checkbox = (CheckBox) findViewById(R.id.timerCB);
    SeekBar timebar = (SeekBar)findViewById(R.id.timerTime);
    TextView statText = (TextView)findViewById(R.id.statusText);
    TextView timerText = (TextView)findViewById(R.id.timerText);

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        timebar.setEnabled(false);
        timebar.setMax(maxTime);

    }

将您的代码更改为:

public class MainActivity extends Activity {
    public final static String EXTRA_MESSAGE = "com.example.remoteswitch.MESSAGE";
    int maxTime = 90;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        CheckBox checkbox = (CheckBox) findViewById(R.id.timerCB);
        SeekBar timebar = (SeekBar)findViewById(R.id.timerTime);
        TextView statText = (TextView)findViewById(R.id.statusText);
        TextView timerText = (TextView)findViewById(R.id.timerText);

        timebar.setEnabled(false);
        timebar.setMax(maxTime);

    }

如果不调用setContentView(R.layout.activity_main);Activity将无法将您的小部件转换映射到您已在其中声明它们的布局。

于 2013-01-30T11:20:27.890 回答
0

改变

 android:name="com.example.remoteswitch.MainActivity"

 android:name=".MainActivity"

并在 setContentView(...) 之后将这些写入 onCreate

 CheckBox checkbox = (CheckBox) findViewById(R.id.timerCB);
 SeekBar timebar = (SeekBar)findViewById(R.id.timerTime);
 TextView statText = (TextView)findViewById(R.id.statusText);
 TextView timerText = (TextView)findViewById(R.id.timerText);
于 2013-01-30T11:21:04.720 回答
0

将 findinf 视图的代码移至 OnCreate:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    timebar = (SeekBar)findViewById(R.id.timerTime);
    timebar.setEnabled(false);
    timebar.setMax(maxTime);

}
于 2013-01-30T11:22:43.393 回答
0

尝试这个

CheckBox checkbox ;
SeekBar timebar ;
TextView statText ;
TextView timerText;

protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

   setContentView(R.layout.activity_main);

   checkbox = (CheckBox) findViewById(R.id.timerCB);
   timebar = (SeekBar)findViewById(R.id.timerTime);
   statText = (TextView)findViewById(R.id.statusText);
   timerText = (TextView)findViewById(R.id.timerText);

   //You can also use this approach , in this case avoid the global declaration of   widgets
   /*
    CheckBox checkbox = (CheckBox) findViewById(R.id.timerCB);
    SeekBar timebar = (SeekBar)findViewById(R.id.timerTime);
    TextView statText = (TextView)findViewById(R.id.statusText);
    TextView timerText = (TextView)findViewById(R.id.timerText);
   */

    timebar.setEnabled(false);
    timebar.setMax(maxTime);

}

您必须首先指定用于活动的布局。然后铸造它的组件

于 2013-01-30T11:23:40.757 回答