Android应用程序每次启动时都会崩溃

时间:2014-07-24 14:39:41

标签: java android crash startup

我开始在Android Studio中编写我的第一个应用程序,它在这个阶段应该做的是你点击一个带有数字的按钮,然后将它输出到文本字段中。当我运行此代码时,应用程序在启动时崩溃,我不知道出了什么问题。

MyActivity.java

package com.example.david.calculator;

import android.app.Activity;
import android.os.Bundle; 
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import org.w3c.dom.Text;


public class MyActivity extends Activity {

EditText results;
private int number;
final EditText result = (EditText) findViewById(R.id.number);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    Button one = (Button) findViewById(R.id.button);
    one.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            BtnPressed(1);
        }
    });

}

private void BtnPressed(int i) {
    result.setText(Integer.toString(i));
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.my, 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);
}
}

activity_my.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity">

<EditText
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:id="@+id/number"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:hint="0"
    android:background="#ffe7e7e7"
    android:textSize="30sp"
    android:gravity="center_vertical|right"
    android:paddingRight="20dp"
    android:editable="true"
    android:enabled="true"
    android:numeric="integer|signed|decimal" />

<Button
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:text="1"
    android:id="@+id/button"
    android:layout_below="@+id/number"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_marginTop="10dp" />

<Button
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:text="2"
    android:id="@+id/button2"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_alignTop="@+id/button"
    android:layout_toRightOf="@+id/button"
    android:layout_marginLeft="10dp" />

<Button
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:text="3"
    android:id="@+id/button3"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_alignTop="@+id/button2"
    android:layout_toRightOf="@+id/button2"
    android:layout_marginLeft="10dp" />

<Button
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:text="4"
    android:id="@+id/button4"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_below="@+id/button"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="10dp" />

<Button
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:text="5"
    android:id="@+id/button5"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_alignTop="@+id/button4"
    android:layout_toRightOf="@+id/button4"
    android:layout_marginLeft="10dp" />

<Button
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:text="6"
    android:id="@+id/button6"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_alignTop="@+id/button5"
    android:layout_toRightOf="@+id/button5"
    android:layout_marginLeft="10dp" />

<Button
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:text="7"
    android:id="@+id/button7"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_below="@+id/button4"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="10dp" />

<Button
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:text="8"
    android:id="@+id/button8"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_alignTop="@+id/button7"
    android:layout_toRightOf="@+id/button7"
    android:layout_marginLeft="10dp" />

<Button
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:text="9"
    android:id="@+id/button9"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_alignTop="@+id/button8"
    android:layout_toRightOf="@+id/button8"
    android:layout_marginLeft="10dp" />

<Button
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:text="0"
    android:id="@+id/button11"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_below="@+id/button7"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="10dp" />

<Button
    android:layout_width="160dp"
    android:layout_height="75dp"
    android:text="="
    android:id="@+id/button12"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_alignTop="@+id/button11"
    android:layout_toRightOf="@+id/button11"
    android:layout_marginLeft="10dp" />

<Button
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:text="+"
    android:id="@+id/button13"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_alignTop="@+id/button3"
    android:layout_toRightOf="@+id/button3"
    android:layout_marginLeft="10dp" />

<Button
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:text="-"
    android:id="@+id/button14"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_alignTop="@+id/button6"
    android:layout_toRightOf="@+id/button6"
    android:layout_marginLeft="10dp" />

<Button
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:text="*"
    android:id="@+id/button15"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_alignTop="@+id/button9"
    android:layout_toRightOf="@+id/button9"
    android:layout_marginLeft="10dp" />

<Button
    android:layout_width="75dp"
    android:layout_height="75dp"
    android:text="/"
    android:id="@+id/button16"
    android:textSize="50sp"
    android:background="#ff0fdf22"
    android:layout_alignTop="@+id/button12"
    android:layout_toRightOf="@+id/button12"
    android:layout_marginLeft="10dp" />

logcat的:

07-24 16:30:07.894  26564-26564/com.example.david.calculator D/AndroidRuntime﹕ Shutting down VM
07-24 16:30:07.894  26564-26564/com.example.david.calculator W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4190aba8)
07-24 16:30:07.899  26564-26564/com.example.david.calculator E/AndroidRuntime﹕ FATAL      EXCEPTION: main
Process: com.example.david.calculator, PID: 26564
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.david.calculator/com.example.david.calculator.MyActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at android.app.Activity.findViewById(Activity.java:1884)
        at com.example.david.calculator.MyActivity.<init>(MyActivity.java:18)
        at java.lang.Class.newInstanceImpl(Native Method)
        at java.lang.Class.newInstance(Class.java:1208)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)  
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

非常感谢你们所有的回复。

4 个答案:

答案 0 :(得分:1)

你应该移动

final EditText result = (EditText) findViewById(R.id.number);
onCreate(...)

之后的setContentView(..)

更正代码

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

EditText result = (EditText) findViewById(R.id.number);

Button one = (Button) findViewById(R.id.button);
one.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        BtnPressed(1);
    }
  });

}

答案 1 :(得分:1)

您正在主类中设置EditText,而不是在类onCreate方法中设置。如果尚未创建活动,则无法找到视图。

public class MyActivity extends Activity {

EditText results;
private int number;
final EditText result = (EditText) findViewById(R.id.number);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    Button one = (Button) findViewById(R.id.button);
    one.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            BtnPressed(1);
        }
    });

}

应该成为

public class MyActivity extends Activity {

EditText results;
private int number;
private EditText result;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    result = (EditText) findViewById(R.id.number);
    Button one = (Button) findViewById(R.id.button);
    one.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            BtnPressed(1);
        }
    });

}

您在日志中收到NullPointerException,这应该可以修复它。

答案 2 :(得分:0)

检查您的代码:

  

final EditText result =(EditText)findViewById(R.id.number);

这应该在 setContentView()方法之后,就像按钮一样。

答案 3 :(得分:0)

你应该这样做:

final EditText result = (EditText) findViewById(R.id.number);
在setContentView之后的onCreate中