Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8808425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:37:12+00:00 2026-06-14T02:37:12+00:00

So I’ve been following the wrox book on begining android I just can’t seem

  • 0

So I’ve been following the wrox book on begining android I just can’t seem to firgue out why my program is crashing when it switches between actives. The crash occurs when I click on the center d_pad outside of The EditBox and Button.

The error logcat describe is as follows.
11-11 11:03:50.700: W/dalvikvm(334): threadid=1: thread exiting with uncaught exception (group=0x40015560)
11-11 11:03:50.949: E/AndroidRuntime(334): FATAL EXCEPTION: main
11-11 11:03:50.949: E/AndroidRuntime(334): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.activites/com.example.activites.Activity2}: java.lang.NullPointerException
11-11 11:03:50.949: E/AndroidRuntime(334):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-11 11:03:50.949: E/AndroidRuntime(334):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-11 11:03:50.949: E/AndroidRuntime(334):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-11 11:03:50.949: E/AndroidRuntime(334):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-11 11:03:50.949: E/AndroidRuntime(334):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-11 11:03:50.949: E/AndroidRuntime(334):  at android.os.Looper.loop(Looper.java:123)
11-11 11:03:50.949: E/AndroidRuntime(334):  at android.app.ActivityThread.main(ActivityThread.java:3683)
11-11 11:03:50.949: E/AndroidRuntime(334):  at java.lang.reflect.Method.invokeNative(Native Method)
11-11 11:03:50.949: E/AndroidRuntime(334):  at java.lang.reflect.Method.invoke(Method.java:507)
11-11 11:03:50.949: E/AndroidRuntime(334):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-11 11:03:50.949: E/AndroidRuntime(334):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-11 11:03:50.949: E/AndroidRuntime(334):  at dalvik.system.NativeStart.main(Native Method)
11-11 11:03:50.949: E/AndroidRuntime(334): Caused by: java.lang.NullPointerException
11-11 11:03:50.949: E/AndroidRuntime(334):  at com.example.activites.Activity2.onCreate(Activity2.java:17)
11-11 11:03:50.949: E/AndroidRuntime(334):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-11 11:03:50.949: E/AndroidRuntime(334):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-11 11:03:50.949: E/AndroidRuntime(334):  ... 11 more

Here is the xml file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Please enter you name" />
<EditText
    android:id="@+id/txt_username"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
<Button
    android:id="@+id/btn_OK"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="OK" />

</LinearLayout>

Here are the two classes that are interacting with one another. First the main

package com.example.activites;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

public class MainActivity extends Activity {
    String tag = "Events";
    int request_Code = 1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        Log.d(tag, "In the onCreate() event");
    }

//  @Override
//  public boolean onCreateOptionsMenu(Menu menu) {
//      getMenuInflater().inflate(R.menu.activity_main, menu);
//      return true;
//  }

    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode== KeyEvent.KEYCODE_DPAD_CENTER)
        {
            startActivityForResult(new Intent("com.example.ACTIVITY2"),request_Code);
        }
        return false;

    }

    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if(requestCode == request_Code){
            if(resultCode==RESULT_OK){
                Toast.makeText(this, data.getData().toString(), Toast.LENGTH_SHORT).show();
            }
        }
    }
}

Second class

package com.example.activites;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Activity2 extends Activity {

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2);
        Button btn = (Button) findViewById(R.id.btn_OK);


    btn.setOnClickListener (new View.OnClickListener(){
        public void onClick (View view){
            Intent data = new Intent();
            EditText txt_username = (EditText) findViewById(R.id.txt_username);

            data.setData(Uri.parse(txt_username.getText().toString()));
            setResult(RESULT_OK, data);
            finish();
        }
    });
}
}

Here is the manifest file

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.activites"
    android:versionCode="1"
    android:debuggable="true"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Activity2"
            android:label="Activity 2" >
            <intent-filter>
                <action android:name="com.example.ACTIVITY2" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

    </application>
</manifest>

Again thanks in advance for your help/ I’m just surprised how different debugging these android applications are from .net development.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:37:14+00:00Added an answer on June 14, 2026 at 2:37 am

    I would guess that your activity2 layout doesn’t have a Button with the id btn_OK, so btn is null:

    Button btn = (Button) findViewById(R.id.btn_OK);
    btn.setOnClickListener( ... ); // Crash here with an NPE
    

    findViewById() can only locate Views that are in the current layout passed to setContentView(), otherwise it returns null.


    This is the important LogCat information:

    Caused by: java.lang.NullPointerException
        at com.example.activites.Activity2.onCreate(Activity2.java:17)
    

    Notice the last bit Activity2.java:17, that is the file and line number where the error occured. Which I guess is: btn.setOnClickListener( ... ); for the reasons above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.