0

Application has unfortunately has stopped error. I am new to android and i am using eclipse. I am getting this error when had a back button on the app. I have kept the button named back (All the buttons are distinguished with unique id)in many pages but all the back buttons must redirect to the same page and there i am getting the error. Please provide me a solution.

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:background="@drawable/memo">

 <Button
    android:id="@+id/btnm1"
    android:layout_centerInParent="true"
    android:layout_marginTop="70dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
      android:textSize="45dp"
    android:layout_alignParentTop="true"
    android:text="Create memo" />

<Button
    android:id="@+id/btnm2"
            android:layout_centerInParent="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btnm1"
    android:text="Edit memo" 
      android:textSize="45dp"/>

     <Button
    android:id="@+id/btnm3"
            android:layout_centerInParent="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btnm2"
    android:text="Delete memo" 
      android:textSize="45dp"/>


  <TextView
    android:id="@+id/tvm1"
    android:textColor="#C73F17"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="Memo"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textSize="50dp" />

    <Button
    android:id="@+id/backmemo"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/tvm1"
    android:layout_below="@+id/btnm3"
    android:layout_marginLeft="24dp"
    android:layout_marginTop="56dp"
    android:text="Back" />

 </RelativeLayout>

//JAVA CODE

package dlp.android.digichronicle;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity  implements View.OnClickListener{
     Button         btn1,btn2,btn21,btn22,btn23,btn24,btn25,btnm1,btnm2,btnm3,btntk1,btntk2,btntk3,btnrm1,btnrm 2,btnrm3;
Button btne1,btne2,btne3,backmemo,backtask,backremind,backevent;
TextView tv1,tv31;  

      @Override
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      FirstPage();
        }


     public void FirstPage(){
    setContentView(R.layout.activity_main);
    btn1=(Button)findViewById(R.id.btn1);
    btn1.setOnClickListener(this);
    btn2=(Button)findViewById(R.id.btn2);
    btn2.setOnClickListener(this);

      dia();//dialogue box  
     }


     public void ThirdPage(){


      }

         public void dia(){
    Context context = MainActivity.this;
      AlertDialog.Builder ad = new AlertDialog.Builder(context);
      ad.setTitle("EVENT");
      ad.setMessage("Event goes here");
      ad.setPositiveButton("Done",
      new OnClickListener() {
      public void onClick(DialogInterface dialog,
      int arg1) { }
      });
      ad.setCancelable(true);
      ad.show();

        }
          public void onClick(View v){

    if(v==btn1)
        SecondPg();         
        if(v==btn2)
        {

         }
        if(v==btn21)
        {
            memo();

         }
        if(v==btn22)
        {
            task();

         }
        if(v==btn23)
        {
            event();

         }
        if(v==btn24)
        {
            reminder();
         }
        if(v==btn25)
        {
            //ThirdPage();
         }
        if(v==backmemo)
        {
            //SecondPg();
         }
        if(v==backtask)
        {
            //SecondPg();
         }
        if(v==backremind)
        {
            //SecondPg();
         }
        if(v==backevent)
        {
            SecondPg();
         }
            }
         public void SecondPg(){
  setContentView(R.layout.second);

  tv1=(TextView)findViewById(R.id.tv1);
  btn21=(Button)findViewById(R.id.btn21);
btn21.setOnClickListener(this);
btn22=(Button)findViewById(R.id.btn22);
btn22.setOnClickListener(this);
btn23=(Button)findViewById(R.id.btn23);
btn23.setOnClickListener(this);
btn24=(Button)findViewById(R.id.btn24);
btn24.setOnClickListener(this);
btn25=(Button)findViewById(R.id.btn25);
btn25.setOnClickListener(this);
//    back_tas=(Button)findViewById(R.id.back_tas);
    //  back_tas.setOnClickListener(this);
        backevent=(Button)findViewById(R.id.backevent);
        backevent.setOnClickListener(this);
        //back_mem=(Button)findViewById(R.id.back_mem);
        //back_mem.setOnClickListener(this);
        //back_rem=(Button)findViewById(R.id.back_rem);
        //back_rem.setOnClickListener(this);

}    

        public void memo(){
setContentView(R.layout.memo);
btnm1=(Button)findViewById(R.id.btnm1);
btnm1.setOnClickListener(this);
btnm2=(Button)findViewById(R.id.btnm2);
btnm2.setOnClickListener(this);
btnm3=(Button)findViewById(R.id.btnm3);
btnm3.setOnClickListener(this);

     }
        public void task(){
setContentView(R.layout.tasks);
btntk1=(Button)findViewById(R.id.btntk1);
btntk1.setOnClickListener(this);
btntk2=(Button)findViewById(R.id.btntk2);
btntk2.setOnClickListener(this);
btntk3=(Button)findViewById(R.id.btntk3);
btntk3.setOnClickListener(this);

      }
        public void reminder(){
setContentView(R.layout.reminder);
btnrm1=(Button)findViewById(R.id.btnrm1);
btnrm1.setOnClickListener(this);
btnrm2=(Button)findViewById(R.id.btnrm2);
btnrm2.setOnClickListener(this);
btnrm3=(Button)findViewById(R.id.btnrm3);
btnrm3.setOnClickListener(this);

      }
        public void event(){
setContentView(R.layout.events);
btne1=(Button)findViewById(R.id.btne1);
btne1.setOnClickListener(this);
btne2=(Button)findViewById(R.id.btne2);
btne2.setOnClickListener(this);
btne3=(Button)findViewById(R.id.btne3);
btne3.setOnClickListener(this);

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

Here is the log cat

09-14 21:35:07.086: W/dalvikvm(708): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
   09-14 21:35:07.107: E/AndroidRuntime(708): FATAL EXCEPTION: main
     09-14 21:35:07.107: E/AndroidRuntime(708): java.lang.NullPointerException
    09-14 21:35:07.107: E/AndroidRuntime(708):  at  dlp.android.digichronicle.MainActivity.SecondPg(MainActivity.java:121)
     09-14 21:35:07.107: E/AndroidRuntime(708):     at dlp.android.digichronicle.MainActivity.onClick(MainActivity.java:59)
        09-14 21:35:07.107: E/AndroidRuntime(708):  at android.view.View.performClick(View.java:3511)
       09-14 21:35:07.107: E/AndroidRuntime(708):   at android.view.View$PerformClick.run(View.java:14105)
    09-14 21:35:07.107: E/AndroidRuntime(708):  at android.os.Handler.handleCallback(Handler.java:605)
     09-14 21:35:07.107: E/AndroidRuntime(708):     at              android.os.Handler.dispatchMessage(Handler.java:92)
      09-14 21:35:07.107: E/AndroidRuntime(708):    at android.os.Looper.loop(Looper.java:137)
     09-14 21:35:07.107: E/AndroidRuntime(708):     at android.app.ActivityThread.main(ActivityThread.java:4424)
        09-14 21:35:07.107: E/AndroidRuntime(708):  at java.lang.reflect.Method.invokeNative(Native Method)
          09-14 21:35:07.107: E/AndroidRuntime(708):    at java.lang.reflect.Method.invoke(Method.java:511)
      09-14 21:35:07.107: E/AndroidRuntime(708):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
      09-14 21:35:07.107: E/AndroidRuntime(708):    at              com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
       09-14 21:35:07.107: E/AndroidRuntime(708):   at dalvik.system.NativeStart.main(Native             Method)
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • We cannot give you a solution unless you SHOW your code and at least try looking at your MainActivity line 121! java.lang.NullPointerException 09-14 21:35:07.107: E/AndroidRuntime(708): at dlp.android.digichronicle.MainActivity.SecondPg(MainActivity.java:121) – krilovich Sep 14 '12 at 16:54
  • i have put all the code along with MainActivity after the xml with heading Java Code in the code please scroll down and see – Dileep Kumar Sep 14 '12 at 17:08
  • please help me. I am in an immense pressure of doing the app – Dileep Kumar Sep 14 '12 at 17:22

3 Answers3

1

I can tell you right now that your actual problem is that you do setContentView
twice in your main activity and this will not work as you cannot call setContentView twice like that in the same activity.

public void SecondPg(){
    setContentView(R.layout.second);

In android you cannot go between pages like that and you actually need another activity which will be called and will use that second layout! the null pointer is because it cannot find that R.id button because it does not exist on the FIRST contentview that you set. Read the tutorial: Android Intents - Tutorial

Also have a look at the this question on how to use intents: Best way to display multiple setContentView in Android

Community
  • 1
  • 1
krilovich
  • 3,475
  • 1
  • 23
  • 33
  • ohh...thanks a lot but which activity should i use to call the same layout again in same class – Dileep Kumar Sep 16 '12 at 18:41
  • Each activity is a class by itself and each activity should have it's own layout. Your first activity should setContentLayout on it's own layout and somewhere in there you should create an intent that start the second activity. Your second activity should have it's own layout – krilovich Sep 17 '12 at 12:54
0

Read your stacktrace! It clearly lists culprit to be Null Pointer Exception lurking in your MainActivity.java, line 121, as result of handling click in MainActivity.java line 59.

EDIT: It looks you try to findViewById() on R.id.tv1 but your layout does not feature such element. It uses R.id.tvm1 however. And it seems that you got the habit of sharing your cryptic Ids among the layouts (or this copy&paste code) so some other layout uses R.id.tv1, hence no compiler error.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
0

Click the two lines below Null Pointer Exception error in your logcat to reach to the error lines in the code. For more, have a look at this.. What is a NullPointerException, and how do I fix it?

Community
  • 1
  • 1
Feona
  • 295
  • 2
  • 6
  • 18