buddy123
buddy123

Reputation: 6327

trouble with first android application

I followed developers guide on google, and tried to create an app, at the moment with only 1 activity. it doesnt have much, just few items on it, and the class is nearly empty from functions, yet it crashes when i activate it. the manifest is this:

    <?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>

and my activity class file is this:

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

my next step was to add another activity, but i just wanted to see if its basically working.. the logcat is this:

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)

Can anyone please help me find out my error?

Upvotes: 0

Views: 80

Answers (7)

edwin
edwin

Reputation: 8091

Try This

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

}

You must first specify the layout that you are using for the activity. Then cast it's components

Upvotes: 0

Siddharth Lele
Siddharth Lele

Reputation: 27748

You are casting all your widgets before calling the 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);

    }

Change your code to:

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

    }

Without calling the setContentView(R.layout.activity_main);, the Activity will not be able to map your widget casts to the layout you have declared them in.

Upvotes: 0

Nostradamus
Nostradamus

Reputation: 658

Move the code for findinf views to 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);

}

Upvotes: 0

SKK
SKK

Reputation: 5271

Change

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

to

 android:name=".MainActivity"

and write these in onCreate after setContentView(...)

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

Upvotes: 0

takecare
takecare

Reputation: 1903

Try using the findViewById() only after setting the content view, i.e., after:

setContentView(R.layout.activity_main);

Upvotes: 0

nano_nano
nano_nano

Reputation: 12524

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

Upvotes: 1

duggu
duggu

Reputation: 38439

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

}

Upvotes: 1

Related Questions