不幸的是,appName已停止错误

时间:2013-08-17 10:46:43

标签: android android-layout android-intent android-emulator

当我按照错误消息运行我的应用程序时,它会给出:

  

不幸的是,appName已停止错误

我有以下MainActivity.java文件:

package com.example.hello_world;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;  

public class MainActivity extends Activity implements  OnCheckedChangeListener {

    Button btnSubmit,b2;
    RadioButton rb1,rb2;
    EditText etFirstName,etLastName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        btnSubmit=(Button)findViewById(R.id.btnSubmit);


        rb1.setOnCheckedChangeListener(new OnCheckedChangeListener()  {





        btnSubmit.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


                    switch(v.getId())
                    {
                    case R.id.btnSubmit:

                        boolean didWork=true;
                        try
                        {
                        String fName=((EditText) findViewById(R.id.etFirstName)).getText().toString();
                        String LName=((EditText) findViewById(R.id.etLastNam)).getText().toString();


                        Toast msg1 = Toast.makeText(getBaseContext(),
                                fName, Toast.LENGTH_LONG);
                      msg1.show();

                        HNT entry=new HNT(MainActivity.this);

                        entry.open();


                        Toast msg2 = Toast.makeText(getBaseContext(),
                                fName, Toast.LENGTH_LONG);
                         msg2.show();

                        entry.createEntry(fName,LName);


                        entry.close();

                        break;
                        }
                        catch(Exception e)
                        {
                            didWork=false;


                            Toast msg1 = Toast.makeText(getBaseContext(),
                                    "Catch Failed"+e.getMessage(), Toast.LENGTH_LONG);
                          msg1.show();

                        }
                        finally
                        {
                            if(didWork)
                            {
                                Toast msg1 = Toast.makeText(getBaseContext(),
                                        "Sucess", Toast.LENGTH_LONG);
                              msg1.show();


                            }
                            else
                            {
                                Toast msg1 = Toast.makeText(getBaseContext(),
                                        "Failed", Toast.LENGTH_LONG);
                              msg1.show();
                            }
                        }

                }



            }
        });        
 }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
        // TODO Auto-generated method stub

    }

}

xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    style="@style/AppTheme"
    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=".MainActivity" >

    <TextView
        android:id="@+id/tvFName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="34dp"
        android:text="First Name"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/etFirstName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/tvFName"
        android:layout_alignBottom="@+id/tvFName"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/tvFName"
        android:inputType="textPersonName" />

    <EditText
        android:id="@+id/etLastNam"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/etFirstName"
        android:layout_below="@+id/etFirstName"
        android:layout_marginLeft="13dp"
        android:layout_marginTop="43dp"
        android:ems="10"
        android:inputType="textPersonName" />

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/etLastNam"
        android:layout_alignLeft="@+id/tvFName"
        android:text="Last Name"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/btnSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/etLastNam"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="74dp"
        android:text="Submit" />

</RelativeLayout>

清单文件:

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

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

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


    </application>

</manifest>

1 个答案:

答案 0 :(得分:3)

发布的xml中没有单选按钮

但你有这个

     rb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

您可能正在NullPointerException。如果您不想删除上述内容。

同时删除

   implements  OnCheckedChangeListener 

和这个

@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
    // TODO Auto-generated method stub

}