第一个Android应用程序的麻烦

时间:2013-01-30 11:16:47

标签: android

我关注谷歌的开发者指南,并试图创建一个应用程序,目前只有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)

任何人都可以帮我找出我的错误吗?

7 个答案:

答案 0 :(得分: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);

}

答案 1 :(得分:1)

放上那行代码

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);

在你的onCreate方法和行

之后
setContentView(R.layout.activity_main);

答案 2 :(得分:0)

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

之后
setContentView(R.layout.activity_main);

答案 3 :(得分: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将无法映射您的小部件强制转换为您声明的布局。

答案 4 :(得分: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);

答案 5 :(得分:0)

将用于查找视图的代码移动到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);

}

答案 6 :(得分: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);

}

您必须先指定用于活动的布局。 然后施展它的组件