不幸的是app已经停止工作 - android模拟器

时间:2014-07-16 23:15:44

标签: android nullpointerexception android-emulator

我是Android应用开发的新手。在学习应用程序开发时,我遇到了错误:

  

不合时宜的APP停止工作

我的应用程序正常打开但是当我输入正确的文本并单击按钮时它停止工作并显示上述错误。}

TextPlay.java

import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;

public class TextPlay extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text);
        Button ChkCmd = (Button) findViewById(R.id.bResults);
        final ToggleButton passTog = (ToggleButton) findViewById(R.id.tbPassword);
        final EditText input = (EditText) findViewById(R.id.etCommands);
        final TextView display = (TextView) findViewById(R.id.tvDisplay);
        passTog.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (passTog.isChecked()) {
                    input.setInputType(InputType.TYPE_CLASS_TEXT
                            | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                } else {
                    input.setInputType(InputType.TYPE_CLASS_TEXT);
                }

            }

        });
        ChkCmd.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String check = input.getText().toString();
                if (check.contentEquals("left")) {
                    display.setGravity(Gravity.LEFT);
                } else if (check.contentEquals("right")) {
                    display.setGravity(Gravity.CENTER);
                } else if (check.contentEquals("center")) {
                    display.setGravity(Gravity.RIGHT);
                } else if (check.contentEquals("blue")) {

                }

            }
        });

    }

}

这是我的 MainFest:

        <?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
            package="com.example.dikshant"
            android:versionCode="1"
            android:versionName="1.0" >

            <uses-sdk
                android:minSdkVersion="8"
                android:targetSdkVersion="21" />

            <application
                android:allowBackup="true"
                android:icon="@drawable/ic_launcher"
                android:label="@string/app_name"
                android:theme="@style/AppTheme" >
                <activity
                    android:name=".Splash"
                    android:label="@string/app_name" >
                    <intent-filter>
                        <action android:name="android.intent.action.MAIN" />
                        <category android:name="android.intent.category.LAUNCHER" />
                    </intent-filter>
                </activity>
                <activity
                    android:name=".MainActivity"
                    android:label="@string/app_name" >
                    <intent-filter>
                        <action android:name="com.example.dikshant.MAINACTIVITY" />

                        <category android:name="android.intent.category.DEFAULT" />
                    </intent-filter>
                </activity>
                 <activity
                    android:name=".Menu"
                    android:label="@string/app_name" >
                    <intent-filter>
                        <action android:name="com.example.dikshant.MENU" />

                        <category android:name="android.intent.category.DEFAULT" />
                    </intent-filter>
                </activity>
                    <activity
                    android:name=".TextPlay"
                    android:label="@string/app_name" >
                    <intent-filter>
                        <action android:name="com.example.dikshant.TEXTPLAY" />
                        <category android:name="android.intent.category.DEFAULT" />
                    </intent-filter>
                </activity>
            </application>

        </manifest>

    ---------------------------------------------------------------------------------

这是我的 text.xml 文件

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:padding="20dp"
            android:background="@drawable/background2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <EditText 
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
                android:id="@+id/etCommands" 
                android:hint="Type a command" 
                android:password="true" />
        <LinearLayout
            android:weightSum="100"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:layout_weight="20"
                android:id="@+id/bResults"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Try Command" />
        enter code here
            <ToggleButton
                android:padding="10dp"
                android:checked="true"
                android:layout_weight="80"
                android:id="@+id/tbPassword"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="ToggleButton" />
        </LinearLayout>
            <TextView
                android:id="@+id/tvResults"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Invalid"
                android:layout_gravity="center"/>

        </LinearLayout>

logcat的

07-16 21:08:06.996: D/dalvikvm(1906): GC_FOR_ALLOC freed 49K, 4% free 3035K/3160K, paused 58ms, total 60ms
07-16 21:08:07.036: I/dalvikvm-heap(1906): Grow heap (frag case) to 5.808MB for 2915856-byte allocation
07-16 21:08:07.096: D/dalvikvm(1906): GC_FOR_ALLOC freed 2K, 3% free 5880K/6008K, paused 57ms, total 57ms
07-16 21:08:07.786: D/(1906): HostConnection::get() New Host Connection established 0xb8bcac50, tid 1906
07-16 21:08:07.846: W/EGL_emulation(1906): eglSurfaceAttrib not implemented
07-16 21:08:07.856: D/OpenGLRenderer(1906): Enabling debug mode 0
07-16 21:09:00.396: D/AndroidRuntime(1906): Shutting down VM
07-16 21:09:00.396: W/dalvikvm(1906): threadid=1: thread exiting with uncaught exception (group=0xb1ac2ba8)

---&gt;&gt;从现在起红色突出显示错误:

07-16 21:09:00.576: E/AndroidRuntime(1906): FATAL EXCEPTION: main
07-16 21:09:00.576: E/AndroidRuntime(1906): Process: com.example.dikshant, PID: 1906
07-16 21:09:00.576: E/AndroidRuntime(1906): java.lang.NullPointerException
07-16 21:09:00.576: E/AndroidRuntime(1906):     at com.example.dikshant.TextPlay$2.onClick(TextPlay.java:51)
07-16 21:09:00.576: E/AndroidRuntime(1906):     at android.view.View.performClick(View.java:4438)
07-16 21:09:00.576: E/AndroidRuntime(1906):     at android.view.View$PerformClick.run(View.java:18422)
07-16 21:09:00.576: E/AndroidRuntime(1906):     at android.os.Handler.handleCallback(Handler.java:733)
07-16 21:09:00.576: E/AndroidRuntime(1906):     at android.os.Handler.dispatchMessage(Handler.java:95)
07-16 21:09:00.576: E/AndroidRuntime(1906):     at android.os.Looper.loop(Looper.java:136)
07-16 21:09:00.576: E/AndroidRuntime(1906):     at android.app.ActivityThread.main(ActivityThread.java:5017)
07-16 21:09:00.576: E/AndroidRuntime(1906):     at java.lang.reflect.Method.invokeNative(Native Method)
07-16 21:09:00.576: E/AndroidRuntime(1906):     at java.lang.reflect.Method.invoke(Method.java:515)
07-16 21:09:00.576: E/AndroidRuntime(1906):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-16 21:09:00.576: E/AndroidRuntime(1906):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-16 21:09:00.576: E/AndroidRuntime(1906):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

在您的代码中,您将错误的ID与TextView相关联。

 final TextView display=(TextView)findViewById(R.id.tvDisplay);

应该是R.id.tvResults,就像您在布局中定义的一样。

只需更改它,您的代码即可正常运行:D