我的Android应用程序每次运行时都会崩溃

时间:2014-04-17 20:36:40

标签: java android

我制作了一个计算二次方程的android应用程序。我建立了一个apk并在我的手机上测试它并没有用。每次我运行应用程序时,它甚至会崩溃按钮,文本字段也不会显示。只有包含应用程序名称的窗口才会崩溃。

这是代码:

package com.islamatabl.quadraticequationsfoa;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.*;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.*;

import java.lang.Math;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button calc = (Button) findViewById(R.id.button1);
        calc.setOnClickListener(new View.OnClickListener(){

                public void onClick(View v)
                {
                    int duration = 5;
                    Context context = getApplicationContext();

                    EditText f1 = (EditText) findViewById(R.id.editText1);
                    EditText f2 = (EditText) findViewById(R.id.editText2);
                    EditText f3 = (EditText) findViewById(R.id.editText3);
                    TextView disp1 = (TextView) findViewById(R.id.textView4);
                    TextView disp2 = (TextView) findViewById(R.id.textView5);
                    double a,b,c,root,inroot,x1,x2;
                    a = Double.parseDouble(f1.getText().toString());
                    b = Double.parseDouble(f2.getText().toString());
                    c = Double.parseDouble(f3.getText().toString());
                    inroot = Math.pow(b,2.0) - 2*a*c;
                    if (inroot<0)
                    {
                        Toast.makeText(context, "Math Error", duration).show();
                    }
                    else if (a==0)
                    {
                        Toast.makeText(context, "a cannot be 0", duration).show();
                    }
                    else
                    {
                    root = Math.sqrt(inroot);
                    x1 = (-b+root)/2*a;
                    x2 = (-b-root)/2*a;
                    if (x1==x2)
                    {
                        disp1.setText("X = " + String.valueOf(x1));
                        disp2.setText("");
                    }
                    else
                    {
                        disp1.setText("X1 = " + String.valueOf(x1));
                        disp2.setText("X2 = " + String.valueOf(x2));
                    }
                    } 
                }



        });



        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }

}

这是XML:

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.islamatabl.quadraticequationsfoa.MainActivity$PlaceholderFragment" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="23dp"
        android:layout_marginTop="100dp"
        android:ems="10"
        android:inputType="number|numberSigned|numberDecimal" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:ems="10"
        android:inputType="number|numberSigned|numberDecimal" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText2"
        android:layout_below="@+id/editText2"
        android:ems="10"
        android:inputType="number|numberSigned|numberDecimal" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/editText2"
        android:layout_marginRight="17dp"
        android:layout_toLeftOf="@+id/editText1"
        android:text="a:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/editText2"
        android:layout_alignLeft="@+id/textView1"
        android:text="b:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/editText3"
        android:layout_alignLeft="@+id/textView2"
        android:text="c:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText3"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="34dp"
        android:text="X1 ="
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView4"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="28dp"
        android:text="X2 ="
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView5"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:text="Calculate" />

</RelativeLayout>

这是logcat:

04-19 17:18:54.312: D/AndroidRuntime(27681): Shutting down VM
04-19 17:18:54.312: W/dalvikvm(27681): threadid=1: thread exiting with uncaught exception (group=0x41f862a0)
04-19 17:18:54.322: E/AndroidRuntime(27681): FATAL EXCEPTION: main
04-19 17:18:54.322: E/AndroidRuntime(27681): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.islamatabl.quadraticequationsfoa/com.islamatabl.quadraticequationsfoa.MainActivity}: java.lang.NullPointerException
04-19 17:18:54.322: E/AndroidRuntime(27681):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081)
04-19 17:18:54.322: E/AndroidRuntime(27681):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106)
04-19 17:18:54.322: E/AndroidRuntime(27681):    at android.app.ActivityThread.access$700(ActivityThread.java:134)
04-19 17:18:54.322: E/AndroidRuntime(27681):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1217)
04-19 17:18:54.322: E/AndroidRuntime(27681):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-19 17:18:54.322: E/AndroidRuntime(27681):    at android.os.Looper.loop(Looper.java:137)
04-19 17:18:54.322: E/AndroidRuntime(27681):    at android.app.ActivityThread.main(ActivityThread.java:4856)
04-19 17:18:54.322: E/AndroidRuntime(27681):    at java.lang.reflect.Method.invokeNative(Native Method)
04-19 17:18:54.322: E/AndroidRuntime(27681):    at java.lang.reflect.Method.invoke(Method.java:511)
04-19 17:18:54.322: E/AndroidRuntime(27681):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
04-19 17:18:54.322: E/AndroidRuntime(27681):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
04-19 17:18:54.322: E/AndroidRuntime(27681):    at dalvik.system.NativeStart.main(Native Method)
04-19 17:18:54.322: E/AndroidRuntime(27681): Caused by: java.lang.NullPointerException
04-19 17:18:54.322: E/AndroidRuntime(27681):    at com.islamatabl.quadraticequationsfoa.MainActivity.onCreate(MainActivity.java:26)
04-19 17:18:54.322: E/AndroidRuntime(27681):    at android.app.Activity.performCreate(Activity.java:5047)
04-19 17:18:54.322: E/AndroidRuntime(27681):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
04-19 17:18:54.322: E/AndroidRuntime(27681):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2045)
04-19 17:18:54.322: E/AndroidRuntime(27681):    ... 11 more

1 个答案:

答案 0 :(得分:0)

在设置onclick侦听器之前初始化所有这些并尝试

 public class MainActivity extends ActionBarActivity {
 EditText f1,f2,f3;
 Button calc;
 TextView disp1,disp2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
                Context context = getApplicationContext();
                calc = (Button) findViewById(R.id.button1);
                f1 = (EditText) findViewById(R.id.editText1);
                f2 = (EditText) findViewById(R.id.editText2);
                f3 = (EditText) findViewById(R.id.editText3);
                disp1 = (TextView) findViewById(R.id.textView4);
                TextView disp2 = (TextView) findViewById(R.id.textView5);

现在设置onclick侦听器并尝试