-6

I am a beginner in eclipse and i am trying to display my buttons but my app has stopped working and i dont know why. hope my doubts will be cleared. thanks in advance.


my Log cat:

  10-14 14:56:15.527: I/dalvikvm(1283): threadid=3: reacting to signal 3
10-14 14:56:15.857: I/dalvikvm(1283): Wrote stack traces to '/data/anr/traces.txt'
10-14 14:56:16.037: I/dalvikvm(1283): threadid=3: reacting to signal 3
10-14 14:56:16.107: I/dalvikvm(1283): Wrote stack traces to '/data/anr/traces.txt'
10-14 14:56:16.296: D/AndroidRuntime(1283): Shutting down VM
10-14 14:56:16.296: W/dalvikvm(1283): threadid=1: thread exiting with uncaught exception (group=0x409bf1f8)
10-14 14:56:16.316: E/AndroidRuntime(1283): FATAL EXCEPTION: main
10-14 14:56:16.316: E/AndroidRuntime(1283): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lpu/com.example.lpu.MainActivity}: java.lang.NullPointerException
10-14 14:56:16.316: E/AndroidRuntime(1283):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
10-14 14:56:16.316: E/AndroidRuntime(1283):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10-14 14:56:16.316: E/AndroidRuntime(1283):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
10-14 14:56:16.316: E/AndroidRuntime(1283):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
10-14 14:56:16.316: E/AndroidRuntime(1283):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-14 14:56:16.316: E/AndroidRuntime(1283):     at android.os.Looper.loop(Looper.java:137)
10-14 14:56:16.316: E/AndroidRuntime(1283):     at android.app.ActivityThread.main(ActivityThread.java:4424)
10-14 14:56:16.316: E/AndroidRuntime(1283):     at java.lang.reflect.Method.invokeNative(Native Method)
10-14 14:56:16.316: E/AndroidRuntime(1283):     at java.lang.reflect.Method.invoke(Method.java:511)
10-14 14:56:16.316: E/AndroidRuntime(1283):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-14 14:56:16.316: E/AndroidRuntime(1283):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-14 14:56:16.316: E/AndroidRuntime(1283):     at dalvik.system.NativeStart.main(Native Method)
10-14 14:56:16.316: E/AndroidRuntime(1283): Caused by: java.lang.NullPointerException
10-14 14:56:16.316: E/AndroidRuntime(1283):     at com.example.lpu.MainActivity.onCreate(MainActivity.java:29)
10-14 14:56:16.316: E/AndroidRuntime(1283):     at android.app.Activity.performCreate(Activity.java:4466)
10-14 14:56:16.316: E/AndroidRuntime(1283):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-14 14:56:16.316: E/AndroidRuntime(1283):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
10-14 14:56:16.316: E/AndroidRuntime(1283):     ... 11 more
10-14 14:56:16.547: I/dalvikvm(1283): threadid=3: reacting to signal 3
10-14 14:56:16.577: I/dalvikvm(1283): Wrote stack traces to '/data/anr/traces.txt'
10-14 14:56:16.866: I/dalvikvm(1283): threadid=3: reacting to signal 3
10-14 14:56:16.886: I/dalvikvm(1283): Wrote stack traces to '/data/anr/traces.txt'
10-14 14:57:06.806: I/dalvikvm(1330): threadid=3: reacting to signal 3
10-14 14:57:07.136: I/dalvikvm(1330): Wrote stack traces to '/data/anr/traces.txt'
10-14 14:57:07.277: I/dalvikvm(1330): threadid=3: reacting to signal 3
10-14 14:57:07.407: I/dalvikvm(1330): Wrote stack traces to '/data/anr/traces.txt'
10-14 14:57:07.736: D/dalvikvm(1330): GC_FOR_ALLOC freed 65K, 3% free 9119K/9347K, paused 78ms
10-14 14:57:07.806: I/dalvikvm(1330): threadid=3: reacting to signal 3
10-14 14:57:08.026: I/dalvikvm-heap(1330): Grow heap (frag case) to 16.883MB for 8294416-byte allocation
10-14 14:57:08.087: I/dalvikvm(1330): Wrote stack traces to '/data/anr/traces.txt'
10-14 14:57:08.207: D/dalvikvm(1330): GC_CONCURRENT freed 1K, 2% free 17217K/17479K, paused 6ms+19ms
10-14 14:57:08.307: I/dalvikvm(1330): threadid=3: reacting to signal 3
10-14 14:57:08.337: I/dalvikvm(1330): Wrote stack traces to '/data/anr/traces.txt'
10-14 14:57:08.826: I/dalvikvm(1330): threadid=3: reacting to signal 3
10-14 14:57:08.866: I/dalvikvm(1330): Wrote stack traces to '/data/anr/traces.txt'
10-14 14:57:09.301: D/gralloc_goldfish(1330): Emulator without GPU emulation detected.
10-14 14:57:09.316: I/dalvikvm(1330): threadid=3: reacting to signal 3
10-14 14:57:09.357: I/dalvikvm(1330): Wrote stack traces to '/data/anr/traces.txt'
10-14 14:57:12.797: I/dalvikvm(1330): threadid=3: reacting to signal 3
10-14 14:57:12.849: I/dalvikvm(1330): Wrote stack traces to '/data/anr/traces.txt'
10-14 14:57:13.306: I/dalvikvm(1330): threadid=3: reacting to signal 3
10-14 14:57:13.326: I/dalvikvm(1330): Wrote stack traces to '/data/anr/traces.txt'
10-14 14:57:18.666: I/dalvikvm(1330): threadid=3: reacting to signal 3
10-14 14:57:18.697: I/dalvikvm(1330): Wrote stack traces to '/data/anr/traces.txt'
10-14 15:01:11.557: I/dalvikvm(1371): threadid=3: reacting to signal 3
10-14 15:01:11.807: I/dalvikvm(1371): Wrote stack traces to '/data/anr/traces.txt'
10-14 15:01:12.046: I/dalvikvm(1371): threadid=3: reacting to signal 3
10-14 15:01:12.097: I/dalvikvm(1371): Wrote stack traces to '/data/anr/traces.txt'
10-14 15:01:12.137: D/AndroidRuntime(1371): Shutting down VM
10-14 15:01:12.147: W/dalvikvm(1371): threadid=1: thread exiting with uncaught exception (group=0x409bf1f8)
10-14 15:01:12.157: E/AndroidRuntime(1371): FATAL EXCEPTION: main
10-14 15:01:12.157: E/AndroidRuntime(1371): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lpu/com.example.lpu.MainActivity}: java.lang.NullPointerException
10-14 15:01:12.157: E/AndroidRuntime(1371):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
10-14 15:01:12.157: E/AndroidRuntime(1371):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10-14 15:01:12.157: E/AndroidRuntime(1371):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
10-14 15:01:12.157: E/AndroidRuntime(1371):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
10-14 15:01:12.157: E/AndroidRuntime(1371):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-14 15:01:12.157: E/AndroidRuntime(1371):     at android.os.Looper.loop(Looper.java:137)
10-14 15:01:12.157: E/AndroidRuntime(1371):     at android.app.ActivityThread.main(ActivityThread.java:4424)
10-14 15:01:12.157: E/AndroidRuntime(1371):     at java.lang.reflect.Method.invokeNative(Native Method)
10-14 15:01:12.157: E/AndroidRuntime(1371):     at java.lang.reflect.Method.invoke(Method.java:511)
10-14 15:01:12.157: E/AndroidRuntime(1371):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-14 15:01:12.157: E/AndroidRuntime(1371):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-14 15:01:12.157: E/AndroidRuntime(1371):     at dalvik.system.NativeStart.main(Native Method)
10-14 15:01:12.157: E/AndroidRuntime(1371): Caused by: java.lang.NullPointerException
10-14 15:01:12.157: E/AndroidRuntime(1371):     at com.example.lpu.MainActivity.onCreate(MainActivity.java:29)
10-14 15:01:12.157: E/AndroidRuntime(1371):     at android.app.Activity.performCreate(Activity.java:4466)
10-14 15:01:12.157: E/AndroidRuntime(1371):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-14 15:01:12.157: E/AndroidRuntime(1371):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
10-14 15:01:12.157: E/AndroidRuntime(1371):     ... 11 more
10-14 15:01:12.547: I/dalvikvm(1371): threadid=3: reacting to signal 3
10-14 15:01:12.586: I/dalvikvm(1371): Wrote stack traces to '/data/anr/traces.txt'
10-14 15:01:12.816: I/dalvikvm(1371): threadid=3: reacting to signal 3
10-14 15:01:12.826: I/dalvikvm(1371): Wrote stack traces to '/data/anr/traces.txt'
10-14 15:04:31.236: I/Process(1371): Sending signal. PID: 1371 SIG: 9

xml file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/game" >

     <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView4"
        android:layout_below="@+id/textView4"
        android:text="• Game Development"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="13dp" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView5"
        android:layout_below="@+id/textView5"
        android:layout_marginTop="21dp"
        android:text="BS Information Technology major in"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FDFEFE"
        android:textSize="15dp"
        android:textStyle="bold|italic" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView6"
        android:layout_below="@+id/textView6"
        android:text="PACUCOA Level II Reaccredited Status)"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="13dp"
        android:textStyle="italic" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="18dp"
        android:text="COMPUTER STUDIES"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#FDFEFE"
        android:textSize="20sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="16dp"
        android:text="BS Computer Science major in"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FDFEFE"
        android:textSize="15dp"
        android:textStyle="bold|italic" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView3"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="14dp"
        android:text="• Mobile Application Development"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="13dp" />

    <TextView
        android:id="@+id/textView10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView9"
        android:layout_below="@+id/textView9"
        android:layout_marginTop="16dp"
        android:text="Associate in Computer Technology"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#FDFEFE"
        android:textStyle="bold|italic" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:text="(Bs Computer Science - PACUCOA level III Reacrideted Status)"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="13sp"
        android:textStyle="italic" />

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView8"
        android:layout_below="@+id/textView8"
        android:text="• Digital Animation"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="13dp" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView7"
        android:layout_centerVertical="true"
        android:text="• Multimedia Technologies"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textSize="13dp" />

</RelativeLayout>

Main Activity:

package com.example.lpu;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;

public class MainActivity extends Activity implements OnClickListener {

    Button button1;
    Button button2;
    ImageButton imageButton1;
    Intent intent;

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

        button1 = (Button)findViewById(R.id.button1);
        button2 = (Button)findViewById(R.id.button2);
        imageButton1 = (ImageButton)findViewById(R.id.imageButton1);

        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        imageButton1.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.button1:
        Intent intent = new Intent(MainActivity.this,BUTTON.class);
        this.startActivity(intent);
        break;

    case R.id.button2:
        Intent intent2 = new Intent(MainActivity.this,BUTOON.class);
        this.startActivity(intent2);
        break;

    case R.id.imageButton1:
        Intent intent3 = new Intent(MainActivity.this,CCS.class);
        this.startActivity(intent3);
        break;
    }

    }
}

here is the Android Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lpu"
    android:versionCode="1"
    android:versionName="1.0" >

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

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".BUTTON"></activity>
        <activity android:name=".BUTOON"></activity>
        <activity android:name=".CCS"></activity>

    </application>

</manifest>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
yen bico
  • 33
  • 6
  • where are your button1, button2, imageButton1 defined in you're XML file? The only thing I see are TextView's. You have to create a Button exactly the same way you created all your TextView's and then a d android:id="@+id/button1" for the first button, android:id="@+id/button2" for the second button and so on – Manuel_Rodrigues Oct 18 '16 at 11:20

2 Answers2

0

Your log file points to the problem:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lpu/com.example.lpu.MainActivity}: java.lang.NullPointerException
...
10-14 14:56:16.316: E/AndroidRuntime(1283): Caused by: java.lang.NullPointerException
10-14 14:56:16.316: E/AndroidRuntime(1283):     at com.example.lpu.MainActivity.onCreate(MainActivity.java:29)

As you can see from the last line you have a null pointer exception at line 29 of your MainActivity class. It looks like you're trying to set an onClickListener incorrectly. Instead of

imageButton1.setOnClickListener(this);

you could do something like this:

button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // Perform action on click
    }
});

Per the Android docs: https://developer.android.com/reference/android/widget/Button.html

mrogers
  • 1,187
  • 2
  • 15
  • 22
0

Your xml is missing the elements you want to add the OnClickListener to.

You need to add a button with the id R.id.button1, another button with the id R.id.button2 and an imageButton with the id R.id.imageButton1 to your activity_main xml file.

Add something like this inside the relativeLayout:

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

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

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="imageButton1"
        android:id="@+id/imageButton1"
        />
Bmuig
  • 1,059
  • 7
  • 12