gpt4 book ai didi

java - 当我运行应用程序时强制关闭问题

转载 作者:太空宇宙 更新时间:2023-11-04 14:56:21 24 4
gpt4 key购买 nike

我正在尝试编写一个程序,输入最多 6 个数字并将其反转。例如:123456 = 654321。我不知道我的程序是否有效,因为每当我运行该程序时,它都会强制关闭。请帮我!拜托了

这是主要 Activity :

public class MainActivity extends ActionBarActivity {

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

final EditText editNumber = (EditText) findViewById(R.id.editText1);
final EditText reverse = (EditText) findViewById(R.id.editText2);

Button btnConvert = (Button) findViewById(R.id.button1);
btnConvert.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
String x = editNumber.getText().toString();
String y = "";

for (int i = x.length() - 1; i >= 0; i--) {
y = y + x.charAt(i);
reverse.setText(String.valueOf(y));
}
}
});
}

}

这是我的 Fragment_Main.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.example.torres.MainActivity$PlaceholderFragment" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="39dp"
android:scrollHorizontally="false"
android:text="@string/hello_world" />

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:ems="10"
android:inputType="numberDecimal"
android:maxLength="6" />
</EditText>

<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"
android:text="@string/button" />

<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_alignRight="@+id/editText1"
android:layout_below="@+id/button1"
android:layout_marginTop="22dp"
android:ems="10"
android:inputType="number"
android:maxLength="6" />

</RelativeLayout>

这是我的日志猫:

04-18 20:57:55.986: E/AndroidRuntime(1393): FATAL EXCEPTION: main
04-18 20:57:55.986: E/AndroidRuntime(1393): Process: com.example.torres, PID: 1393
04-18 20:57:55.986: E/AndroidRuntime(1393): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.torres/com.example.torres.MainActivity}: java.lang.NullPointerException
04-18 20:57:55.986: E/AndroidRuntime(1393): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-18 20:57:55.986: E/AndroidRuntime(1393): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-18 20:57:55.986: E/AndroidRuntime(1393): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-18 20:57:55.986: E/AndroidRuntime(1393): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-18 20:57:55.986: E/AndroidRuntime(1393): at android.os.Handler.dispatchMessage(Handler.java:102)
04-18 20:57:55.986: E/AndroidRuntime(1393): at android.os.Looper.loop(Looper.java:136)
04-18 20:57:55.986: E/AndroidRuntime(1393): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-18 20:57:55.986: E/AndroidRuntime(1393): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 20:57:55.986: E/AndroidRuntime(1393): at java.lang.reflect.Method.invoke(Method.java:515)
04-18 20:57:55.986: E/AndroidRuntime(1393): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-18 20:57:55.986: E/AndroidRuntime(1393): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-18 20:57:55.986: E/AndroidRuntime(1393): at dalvik.system.NativeStart.main(Native Method)
04-18 20:57:55.986: E/AndroidRuntime(1393): Caused by: java.lang.NullPointerException
04-18 20:57:55.986: E/AndroidRuntime(1393): at com.example.torres.MainActivity.onCreate(MainActivity.java:21)
04-18 20:57:55.986: E/AndroidRuntime(1393): at android.app.Activity.performCreate(Activity.java:5231)
04-18 20:57:55.986: E/AndroidRuntime(1393): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-18 20:57:55.986: E/AndroidRuntime(1393): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-18 20:57:55.986: E/AndroidRuntime(1393): ... 11 more
04-18 20:59:25.276: D/AndroidRuntime(1440): Shutting down VM
04-18 20:59:25.276: W/dalvikvm(1440): threadid=1: thread exiting with uncaught exception (group=0xb2a12ba8)
04-18 20:59:25.286: E/AndroidRuntime(1440): FATAL EXCEPTION: main
04-18 20:59:25.286: E/AndroidRuntime(1440): Process: com.example.torres, PID: 1440
04-18 20:59:25.286: E/AndroidRuntime(1440): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.torres/com.example.torres.MainActivity}: java.lang.NullPointerException
04-18 20:59:25.286: E/AndroidRuntime(1440): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-18 20:59:25.286: E/AndroidRuntime(1440): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-18 20:59:25.286: E/AndroidRuntime(1440): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-18 20:59:25.286: E/AndroidRuntime(1440): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-18 20:59:25.286: E/AndroidRuntime(1440): at android.os.Handler.dispatchMessage(Handler.java:102)
04-18 20:59:25.286: E/AndroidRuntime(1440): at android.os.Looper.loop(Looper.java:136)
04-18 20:59:25.286: E/AndroidRuntime(1440): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-18 20:59:25.286: E/AndroidRuntime(1440): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 20:59:25.286: E/AndroidRuntime(1440): at java.lang.reflect.Method.invoke(Method.java:515)
04-18 20:59:25.286: E/AndroidRuntime(1440): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-18 20:59:25.286: E/AndroidRuntime(1440): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-18 20:59:25.286: E/AndroidRuntime(1440): at dalvik.system.NativeStart.main(Native Method)
04-18 20:59:25.286: E/AndroidRuntime(1440): Caused by: java.lang.NullPointerException
04-18 20:59:25.286: E/AndroidRuntime(1440): at com.example.torres.MainActivity.onCreate(MainActivity.java:21)
04-18 20:59:25.286: E/AndroidRuntime(1440): at android.app.Activity.performCreate(Activity.java:5231)
04-18 20:59:25.286: E/AndroidRuntime(1440): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-18 20:59:25.286: E/AndroidRuntime(1440): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-18 20:59:25.286: E/AndroidRuntime(1440): ... 11 more
04-18 20:59:27.726: I/Process(1440): Sending signal. PID: 1440 SIG: 9

最佳答案

Fragment_Main.xml 中的 UI 元素。您会收到 NullPointerException,因为您将 Activity_main.xml 设置为 MainActivity 的布局:setContentView(R.layout.activity_main);。将布局从 Fragment_Main.xml 移动到 Activity_main.xml。

关于java - 当我运行应用程序时强制关闭问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23164612/

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