gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:36:13 29 4
gpt4 key购买 nike

我开始在 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" />

日志猫:

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)

非常感谢大家的回复。

            

最佳答案

您正在主类中设置 EditText,而不是在类 onCreate 方法中设置。如果 Activity 尚未创建,您将无法找到 View 。

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,这应该可以修复它。

关于java - Android 应用程序每次启动时都会崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24936763/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com