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)
4

3 回答 3

1

我现在可以告诉你,你的实际问题是你setContentView
在你的主要活动中做了两次,这是行不通的,因为你不能setContentView在同一个活动中调用两次。

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

在 android 中,您不能在这样的页面之间切换,您实际上需要另一个将被调用并使用第二个布局的活动!空指针是因为它找不到该R.id按钮,因为它在您设置的第一个内容视图上不存在。阅读教程:Android Intents - 教程

也看看这个关于如何使用意图的问题:Best way to display multiple setContentView in Android

于 2012-09-14T20:21:22.693 回答
0

单击 logcat 中 Null Pointer Exception 错误下方的两行以访问代码中的错误行。有关更多信息,请查看此内容。 什么是 NullPointerException,我该如何解决?

于 2012-09-14T16:43:40.577 回答
0

阅读您的堆栈跟踪!它清楚地列出了潜伏在 MainActivity.java 第 121 行中的 Null Pointer Exception 的罪魁祸首,这是在 MainActivity.java 第 59 行中处理单击的结果。

编辑:看起来你尝试了findViewById()R.id.tv1但你的布局没有这样的元素。但是它使用R.id.tvm1。而且您似乎习惯于在布局(或此复制和粘贴代码)之间共享您的神秘 ID,因此其他一些布局使用R.id.tv1,因此没有编译器错误。

于 2012-09-14T16:29:30.360 回答