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 8681443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:30:42+00:00 2026-06-12T21:30:42+00:00

I have a very simple code, as I am new to Java and Android.

  • 0

I have a very simple code, as I am new to Java and Android. So, I have this button, which when clicked, should switch the text between two Text Views.
I have my Android phone in debugging mode plugged in, so my app runs directly on it. However, it crashes as soon as it is launched, saying, “Unfortunately, has stopped”.

Here is my code:

    public class ConvertItAll extends Activity {

String temp;
TextView tv1 = (TextView) findViewById(R.id.tvUnit1);
TextView tv2 = (TextView) findViewById(R.id.tvUnit2);

EditText et1=(EditText) findViewById(R.id.etInput);
TextView tv3=(EditText) findViewById(R.id.tvOutput);
Button b1=(Button) findViewById(R.id.bEdit1);
Button b2=(Button) findViewById(R.id.bEdit2);
Button b3 = (Button) findViewById(R.id.bSwap);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    b3.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            temp = tv1.getText().toString();
            tv1.setText(tv2.getText().toString());
            tv2.setText(temp);
        }
    });
}
}

The error, when I see in Logcat, occurs in the line where I declare and initialize the View variables. So, I tried to comment these declarations (and did not use the variables too, of course), and the App loaded fine, and I could see the Layout.

Can someone please help? If I cannot initialize variables, how will I possibly use them?

Here is the LogCat result:

    •   09-29 23:42:10.788: I/Process(600): Sending signal. PID: 600 SIG: 9

    •   09-29 23:42:27.846: E/Trace(670): error opening trace file: No such file or directory (2)

    •   09-29 23:42:28.536: D/dalvikvm(670): GC_FOR_ALLOC freed 70K, 3% free 8051K/8259K, paused 51ms, total 53ms
    •   09-29 23:42:28.556: I/dalvikvm-heap(670): Grow heap (frag case) to 8.827MB for 960016-byte allocation

    •   09-29 23:42:28.776: D/dalvikvm(670): GC_CONCURRENT freed 1K, 3% free 8987K/9223K, paused 78ms+6ms, total 222ms

    •   09-29 23:42:28.776: D/dalvikvm(670): WAIT_FOR_CONCURRENT_GC blocked 8ms

    •   09-29 23:42:29.106: D/gralloc_goldfish(670): Emulator without GPU emulation detected.

    •   09-29 23:42:29.966: D/AndroidRuntime(670): Shutting down VM

    •   09-29 23:42:29.966: W/dalvikvm(670): threadid=1: thread exiting with uncaught exception (group=0x40a13300)

    •   09-29 23:42:29.996: E/AndroidRuntime(670): FATAL EXCEPTION: main

    •   09-29 23:42:29.996: E/AndroidRuntime(670): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.allinoneconverter/com.example.allinoneconverter.ConvertItAll}: java.lang.NullPointerException

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at android.app.ActivityThread.access$600(ActivityThread.java:130)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at android.os.Handler.dispatchMessage(Handler.java:99)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at android.os.Looper.loop(Looper.java:137)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at android.app.ActivityThread.main(ActivityThread.java:4745)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at java.lang.reflect.Method.invokeNative(Native Method)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at java.lang.reflect.Method.invoke(Method.java:511)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at dalvik.system.NativeStart.main(Native Method)

    •   09-29 23:42:29.996: E/AndroidRuntime(670): Caused by: java.lang.NullPointerException

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at android.app.Activity.findViewById(Activity.java:1825)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at com.example.allinoneconverter.ConvertItAll.<init>(ConvertItAll.java:12)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at java.lang.Class.newInstanceImpl(Native Method)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at java.lang.Class.newInstance(Class.java:1319)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at android.app.Instrumentation.newActivity(Instrumentation.java:1053)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)

    •   09-29 23:42:29.996: E/AndroidRuntime(670):  ... 11 more
  • 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-12T21:30:43+00:00Added an answer on June 12, 2026 at 9:30 pm

    You can only get a reference to the Views in your layout after it has been inflated. Try calling setContentView() after the super() call, and then initializing the variables. So your code would look something like:

    public class ConvertItAll extends Activity {
    
    String temp;
    TextView tv1;
    
    .....
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.<your_Llayout>);
        tv1 = (TextView) findViewById(R.id.tvUnit1)
       .....
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple code, which annoyingly was working and for the life
I have the following very simple code snippet which is loaded from a separate
I have a very simple JavaScript/jquery code which won't just work correctly. The problem
I have a very simple HTML/JS code, which expands the size of a div
I am very new to the android platform thus java so i have a
I have a very simple question: How can I make this code more simple
I have a very simple client server code written java(server listens on some port
I am very new to java, and this seems very simple, perhaps I am
I'm very new to Java. I have a single class file which is used
I have a very simple code: package mygame; public class RunGame { public static

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.