I studied similar questions asked here earlier, about the same error, but since I just started programming answers for my task I did not find. Program Code:

package com.evreal.myapplication; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { Button start; TextView message; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); start = (Button) findViewById(R.id.start); message = (TextView) findViewById(R.id.message); start.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { switch (v.getId()) { case R.id.start: message.setText("Hello world"); break; default: break; } } } ); } } 

Activity Markup:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:fitsSystemWindows="true" tools:context="com.evreal.myapplication.MainActivity" android:orientation="horizontal"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerHorizontal="true" android:layout_alignParentTop="true" android:weightSum="1"> <TextView android:layout_width="match_parent" android:layout_height="40dp" android:text="Evreal" android:id="@+id/evreal" android:layout_gravity="center_horizontal" android:gravity="center|center_horizontal" android:textStyle="bold" android:textSize="25dp" android:background="#01c017" android:textColor="#ffffff" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="Do this" android:id="@+id/text" android:layout_gravity="center_horizontal" android:textColor="@android:color/black" /> <Button style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start" android:id="@+id/button" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:id="@+id/message" android:textStyle="normal" android:textSize="25dp" android:textColor="@android:color/black" /> </LinearLayout> </RelativeLayout> 

at startup it produces the following errors:

03-23 ​​02: 03: 52.674 20433-20433 /? E / AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo {com.evreal.myapplication / com.evreal.myapplication.MainActivity}: java.lang.NullPointerException at android.app.ActivityThread.performLaunchActivity (ThisThread .java: 2180) at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2230) at android.app.ActivityThread.access $ 600 (ActivityThread.java:141) at android.app.ActivityThread $ H.handleMessage (ActivityThread.java : 1234) at android.os.Handler.dispatchMessage (Handler.java:99) at android.os.Looper.loop (Looper.java:137) at android.app.ActivityThread.main (ActivityThread.java:5041) at java .lang.reflect.Method.invokeNative (Native Method) at java.lang.reflect.Method.invoke (Method.javainter11) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java 12.60) at dalvik.system.NativeStart.main (Native Method) Caused by: java.lang.NullPointerException at com.evreal.myapplication.MainActivity.onCreate (MainActivity.java:22) at android.app.Activity.performCreate (Activity.javaClt104) at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1080) at android. app.ActivityThread.performLaunchActivity (ActivityThread.java:2144) at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2230) at android.app.ActivityThread.access $ 600 (ActivityThread.java:141) at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1234) at android.os.Handler.dispatchMessage (Handler.java:99) at android.os.Looper.loop (Looper.java:137) at android.app.ActivityThread.main (ActivityThread .java: 5041) at java.lang.reflect.Method.invokeNative (Native Method) at java.lang.reflect.Method.invoke (Method.java Lim11) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main (ZygoteInit.javailiate60) at dalvik.system.NativeStart.main (Native Method)

apparently an error in line 22 of the code

start.setOnClickListener (new View.OnClickListener () {

but I don’t know how to solve it.

Why does Null return and what is wrong?
Also why do so many errors betray?
And did I correctly suggest the cause of the fatal error?

The code of this method seems to meet the requirements of reference documentation from http://developer.android.com

Closed due to the fact that off-topic participants pavlofff , YuriySPb , aleksandr barakin , D-side , Abyx 29 Mar '16 at 14:31 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - pavlofff, Yuriy SPb, aleksandr barakin, Abyx
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Do you seriously think that this error would not have appeared by running your code in Eclipse with a standard emulator? Less unnecessary water in questions, less bold and triple question marks, please. The [android-studio] tag for questions about solving problems in the work of the IDE itself, and not for demonstrating what you are writing code - this is not essential in this case. - pavlofff
  • In addition to the @metalurgus answer, there are so many errors because it is a glass-string and here the whole chain of application “crash” caused by your inattention is presented. That is, from the discovery that there is no widget with the given ID to the fall for this reason, it is somewhere "in the depths of the system." The IDE should have highlighted your R.id.start red , R.id.start ? - pavlofff

1 answer 1

In the markup of your button id such:

 android:id="@+id/button" 

And you are trying to find it on this ID:

 start = (Button) findViewById(R.id.start); 

Only attentiveness and ability to use the debugger will save from such errors.

  • Thank you. I accepted all the criticism. - AndUp
  • IDE unfortunately did not highlight in red R.id.start - AndUp
  • one
    @AndUp, did not select because in another markup file you have another View with such id - Vladyslav Matviienko