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
R.id.startred ,R.id.start? - pavlofff