我的应用程序崩溃,但我不知道为什么?

时间:2014-09-30 11:38:08

标签: android nullpointerexception fatal-error uncaught-exception

当我运行我的应用程序时,它崩溃但我不知道为什么。 log cat正在给出致命的错误。

Logcat

09-30 07:09:38.380: D/AndroidRuntime(1361): Shutting down VM
09-30 07:09:38.380: W/dalvikvm(1361): threadid=1: thread exiting with uncaught exception (group=0xb4b06b90)
09-30 07:09:38.410: E/AndroidRuntime(1361): FATAL EXCEPTION: main
09-30 07:09:38.410: E/AndroidRuntime(1361): Process: shibli.sendDarood, PID: 1361
09-30 07:09:38.410: E/AndroidRuntime(1361): java.lang.RuntimeException: Unable to start activity ComponentInfo{shibli.sendDarood/shibli.sendDarood.MainActivity}: java.lang.NullPointerException
09-30 07:09:38.410: E/AndroidRuntime(1361):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
09-30 07:09:38.410: E/AndroidRuntime(1361):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
09-30 07:09:38.410: E/AndroidRuntime(1361):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
09-30 07:09:38.410: E/AndroidRuntime(1361):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
09-30 07:09:38.410: E/AndroidRuntime(1361):     at android.os.Handler.dispatchMessage(Handler.java:102)
09-30 07:09:38.410: E/AndroidRuntime(1361):     at android.os.Looper.loop(Looper.java:137)
09-30 07:09:38.410: E/AndroidRuntime(1361):     at android.app.ActivityThread.main(ActivityThread.java:4998)
09-30 07:09:38.410: E/AndroidRuntime(1361):     at java.lang.reflect.Method.invokeNative(Native Method)
09-30 07:09:38.410: E/AndroidRuntime(1361):     at java.lang.reflect.Method.invoke(Method.java:515)
09-30 07:09:38.410: E/AndroidRuntime(1361):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
09-30 07:09:38.410: E/AndroidRuntime(1361):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
09-30 07:09:38.410: E/AndroidRuntime(1361):     at dalvik.system.NativeStart.main(Native Method)
09-30 07:09:38.410: E/AndroidRuntime(1361): Caused by: java.lang.NullPointerException
09-30 07:09:38.410: E/AndroidRuntime(1361):     at shibli.sendDarood.MainActivity.onCreate(MainActivity.java:143)
09-30 07:09:38.410: E/AndroidRuntime(1361):     at android.app.Activity.performCreate(Activity.java:5243)
09-30 07:09:38.410: E/AndroidRuntime(1361):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-30 07:09:38.410: E/AndroidRuntime(1361):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
09-30 07:09:38.410: E/AndroidRuntime(1361):     ... 11 more
09-30 07:14:39.150: I/Process(1361): Sending signal. PID: 1361 SIG: 9

 package shibli.sendDarood;

 import android.os.Bundle; 
 import android.app.Activity;
 import android.content.Context;
 import android.content.SharedPreferences;
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.TextView;


public class MainActivity extends Activity {
EditText dAmnt;
TextView finmsg;
Button enter, msg, bt3;
int counter = 0;
long total = 0;

@Override
protected void onSaveInstanceState(Bundle outState) {
    // TODO Auto-generated method stub
    super.onSaveInstanceState(outState);

    outState.putLong("Value", total);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onRestoreInstanceState(savedInstanceState);
    total=savedInstanceState.getLong("Value");

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    SharedPreferences ob=getSharedPreferences("mydata", Context.MODE_PRIVATE);
    total=ob.getLong("Total", total);



    setContentView(R.layout.activity_main);

    finmsg = (TextView) findViewById(R.id.finalmsg);
    dAmnt = (EditText) findViewById(R.id.editText1);
    enter = (Button) findViewById(R.id.button1);
    msg = (Button) findViewById(R.id.button2);
    enter.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if (dAmnt.getText().toString().equalsIgnoreCase("")) {




            } else {


                if (dAmnt.getText() != null
                        && Integer.parseInt(dAmnt.getText().toString()) <= 1000) {
                    counter = Integer.parseInt(dAmnt.getText().toString());
                    total += counter;
                    dAmnt.getText().clear();


                } else if (Integer.parseInt(dAmnt.getText().toString()) > 1000) {
                    finmsg.setText("hey Dude Plz Enter value Less than 1000");
                    dAmnt.getText().clear();

                    total += 0;


                }

            }
        }

    });
    msg.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            if (total < 1) {
                finmsg.setText("No Darood you have Recited Yet :" + total);



            }

            else if (total >= 1 && total <= 1000) {
                finmsg.setText("Total Darood Shareef Recited :" + total);

            } 

            else if (total > 1000) {
                finmsg.setText("SubhanAllah !!SubhanAllah!!SubhanAllah!! ..Total Number of Darood                                   you have Recited is: "+ total);

            }

        }
    });
    bt3.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            total=0;



        }
    });



}




@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    SharedPreferences ob=getSharedPreferences("mydata", Context.MODE_PRIVATE);
    SharedPreferences.Editor ed= ob.edit();
    ed.putLong("Total",total);
    ed.commit();
}

}

xml代码

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

   <TextView
     android:id="@+id/tvArabicText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_margin="7dp"
    android:gravity="center"
    android:text="@string/arabicText"
    android:textColor="#FF0707"
    android:textStyle="bold"
    />

<TextView
    android:id="@+id/tvEngText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/tvArabicText"
    android:layout_margin="7dp"
    android:background="#00E00E"
    android:gravity="center"
    android:text="@string/engText"
    />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/editText1"
    android:layout_margin="7dp"
    android:text="@string/submit" />

<TextView
    android:id="@+id/finalmsg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/button2"
    android:layout_margin="7dp"
    android:text="@string/FinalMsg"
    android:textColor="#FF940A"
    android:textSize="15sp" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_margin="7dp"
    android:ems="10"
    android:hint="@string/hin"
    android:inputType="number"
    android:maxLength="7" >
</EditText>

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/button1"
    android:layout_margin="7dp"
    android:text="@string/Button" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/tvEngText"
    android:layout_below="@+id/tvEngText"
    android:layout_margin="7dp"
    android:background="#00E00E"
    android:gravity="center"
    android:text="@string/msg" />
   </RelativeLayout>

我的清单文件是

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

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

<application

    android:allowBackup="true"
    android:icon="@drawable/gumbad_ekhizra"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name="shibli.sendDarood.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>
  </application>
 </manifest>

4 个答案:

答案 0 :(得分:3)

您需要在调用方法之前初始化bt3

答案 1 :(得分:3)

由于以下原因,您的代码将崩溃:

  

java.lang.NullPointerException

此异常代码位于143行代码的MainActivity.java中。

快速修复:

Button bt3 = (Button) findViewById(R.id.button3);

您忘记在xml文件中设置此按钮。

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/button2"
    android:layout_margin="7dp"
    android:text="@string/Button" />

答案 2 :(得分:2)

我只能在布局文件中看到两个按钮。
但你已经宣布了三个按钮

public class MainActivity extends Activity {
EditText dAmnt;
TextView finmsg;
Button enter, msg, bt3; // three buttons
int counter = 0;
long total = 0;


setContentView(R.layout.activity_main);

finmsg = (TextView) findViewById(R.id.finalmsg);
dAmnt = (EditText) findViewById(R.id.editText1);
enter = (Button) findViewById(R.id.button1);
msg = (Button) findViewById(R.id.button2);

bt3 = (Button) findViewById(R.id.id_of_your_button)                  //Add this line
enter.setOnClickListener(new View.OnClickListener() {

答案 3 :(得分:0)

布局中只有2个按钮,首先在布局中创建第三个按钮然后 像其他人一样初始化该按钮。如果你没有初始化NPE来

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences ob=getSharedPreferences("mydata", Context.MODE_PRIVATE);
        total=ob.getLong("Total", total);



        setContentView(R.layout.activity_main);

        bt3=(Button)findViewById(R.id.xyz_button);  //Here you find the new button

        finmsg = (TextView) findViewById(R.id.finalmsg);
        dAmnt = (EditText) findViewById(R.id.editText1);
        enter = (Button) findViewById(R.id.button1);
        msg = (Button) findViewById(R.id.button2);
        enter.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (dAmnt.getText().toString().equalsIgnoreCase("")) {




                } else {


                    if (dAmnt.getText() != null
                            && Integer.parseInt(dAmnt.getText().toString()) <= 1000) {
                        counter = Integer.parseInt(dAmnt.getText().toString());
                        total += counter;
                        dAmnt.getText().clear();


                    } else if (Integer.parseInt(dAmnt.getText().toString()) > 1000) {
                        finmsg.setText("hey Dude Plz Enter value Less than 1000");
                        dAmnt.getText().clear();

                        total += 0;


                    }

                }
            }

        });
        msg.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                if (total < 1) {
                    finmsg.setText("No Darood you have Recited Yet :" + total);



                }

                else if (total >= 1 && total <= 1000) {
                    finmsg.setText("Total Darood Shareef Recited :" + total);

                } 

                else if (total > 1000) {
                    finmsg.setText("SubhanAllah !!SubhanAllah!!SubhanAllah!! ..Total Number of Darood                                   you have Recited is: "+ total);

                }

            }
        });
        bt3.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                total=0;



            }
        });



    }