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

  • SEARCH
  • Home
  • 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 8750083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:48:14+00:00 2026-06-13T12:48:14+00:00

Please have a look at the following code activity_main.xml <LinearLayout 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

  • 0

Please have a look at the following code

activity_main.xml

<LinearLayout 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" >

    <EditText
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LinearLayout>

MainActivity.java

package com.example.textbox;

import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    private EditText text = (EditText)findViewById(R.id.text);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text.setOnKeyListener(new TextMaker());
    }

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

    private class TextMaker implements OnKeyListener
    {

        String str = text.getText().toString();


        public boolean onKey(View arg0, int keyCode, KeyEvent event)
        {
            if((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER))
            {
                Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

When I run this application, I am getting the following error

10-27 19:05:46.487: D/AndroidRuntime(339): Shutting down VM
10-27 19:05:46.487: W/dalvikvm(339): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-27 19:05:46.527: E/AndroidRuntime(339): FATAL EXCEPTION: main
10-27 19:05:46.527: E/AndroidRuntime(339): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.textbox/com.example.textbox.MainActivity}: java.lang.NullPointerException
10-27 19:05:46.527: E/AndroidRuntime(339):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
10-27 19:05:46.527: E/AndroidRuntime(339):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-27 19:05:46.527: E/AndroidRuntime(339):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-27 19:05:46.527: E/AndroidRuntime(339):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-27 19:05:46.527: E/AndroidRuntime(339):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-27 19:05:46.527: E/AndroidRuntime(339):  at android.os.Looper.loop(Looper.java:123)
10-27 19:05:46.527: E/AndroidRuntime(339):  at android.app.ActivityThread.main(ActivityThread.java:3683)
10-27 19:05:46.527: E/AndroidRuntime(339):  at java.lang.reflect.Method.invokeNative(Native Method)
10-27 19:05:46.527: E/AndroidRuntime(339):  at java.lang.reflect.Method.invoke(Method.java:507)
10-27 19:05:46.527: E/AndroidRuntime(339):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-27 19:05:46.527: E/AndroidRuntime(339):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-27 19:05:46.527: E/AndroidRuntime(339):  at dalvik.system.NativeStart.main(Native Method)
10-27 19:05:46.527: E/AndroidRuntime(339): Caused by: java.lang.NullPointerException
10-27 19:05:46.527: E/AndroidRuntime(339):  at android.app.Activity.findViewById(Activity.java:1647)
10-27 19:05:46.527: E/AndroidRuntime(339):  at com.example.textbox.MainActivity.<init>(MainActivity.java:15)
10-27 19:05:46.527: E/AndroidRuntime(339):  at java.lang.Class.newInstanceImpl(Native Method)
10-27 19:05:46.527: E/AndroidRuntime(339):  at java.lang.Class.newInstance(Class.java:1409)
10-27 19:05:46.527: E/AndroidRuntime(339):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
10-27 19:05:46.527: E/AndroidRuntime(339):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
10-27 19:05:46.527: E/AndroidRuntime(339):  ... 11 more

Why is this? Please help!

  • 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-13T12:48:15+00:00Added an answer on June 13, 2026 at 12:48 pm

    We need to refer the EditText after specifying which layout we are considering.
    I think setContentView() helps you to know which layout we are considering. So try initialising the EditText with findViewById() after the statement setContentView().

    replace

    private EditText text = (EditText)findViewById(R.id.text);
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            text.setOnKeyListener(new TextMaker());
        }
    

    with

    private EditText text ;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            text = (EditText)findViewById(R.id.text);
            text.setOnKeyListener(new TextMaker());
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please have a look at the following code <LinearLayout 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:orientation=vertical
Please have a look at the following code: <HTML> <HEAD> <script src=//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js type=text/javascript></script> <SCRIPT
Please have a look at the following code import com.sun.jna.Native; import uk.co.caprica.vlcj.binding.LibVlc; import uk.co.caprica.vlcj.component.EmbeddedMediaPlayerComponent;
please have a look at the following code import java.util.ArrayList; import java.util.List; public class
Please have a look at the following code namespace Funny { class QuesionsAndAnswers {
Please have a look at the following code: $(#saveButton).click(function(){ $this = $(#tableData).find(input:checked).parent().parent(); tea =
Please have a look at the following code package Euler; import java.util.ArrayList; import java.util.List;
Please have a look at the following machine code ‎0111001101110100011100100110010101110011011100110110010101100100 This means something. I
am new here. i have a slight problem; PLease look at the following code
I have HttpClient 4.1. Please have a look at following program: import org.apache.http.client.methods.*; import

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.