我在开发Android应用程序时遇到错误

时间:2013-10-04 12:12:25

标签: java android xml

我一直得到的错误是“应用程序测试(进程com.example.test)意外停止。请再试一次”。我在模拟器上启动应用程序时发生此错误。以下是代码:

MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
 //   setContentView(R.layout.activity_main);
    TextView myText = new TextView (this);
    myText.setText("Hello World!");
    setContentView(myText);
    myDemoButton();

}
private void myDemoButton() {
    Button displayButton = (Button) findViewById(R.id.btnTesting);
    displayButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.i("MyApp","Welcome To Android!");
            Toast.makeText(MainActivity.this,"Welcome To Android!",Toast.LENGTH_LONG).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;
}}

清单XML代码

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.test.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>

活动主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=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<Button
    android:id="@+id/btnTesting"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="22dp"
    android:text="Tap" />

  </RelativeLayout>

1 个答案:

答案 0 :(得分:0)

使用此

更改主要活动
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myDemoButton();

    }
    private void myDemoButton() {
        Button displayButton = (Button) findViewById(R.id.btnTesting);
        displayButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.i("MyApp","Welcome To Android!");
                Toast.makeText(MainActivity.this,"Welcome To Android!",Toast.LENGTH_LONG).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;
}}