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

时间:2014-04-19 01:05:37

标签: java android

我正在尝试编写一个程序,它将输入一个最多6个数字的数字并将其反转。 例如:123456 = 654321.我不知道每当我运行程序时我的程序是否正常工作,它会强行关闭。请帮我! pleaase即时通讯pleasee

这是mainActivity:

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>

这是我的logcat:

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

1 个答案:

答案 0 :(得分:0)

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