简体   繁体   English

开发Android应用程式时,我不断收到错误讯息

[英]I keep getting an error while developing an Android application

The error I keep getting is "The application Test (process com.example.test) has stopped unexpectedly. Please Try again". 我一直收到的错误是“应用程序测试(进程com.example.test)意外停止。请重试”。 This error occurs when I launch the application on the emulator. 当我在模拟器上启动应用程序时,会发生此错误。 Below are the codes: 以下是代码:

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;
}}

Manifest XML code 清单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>

Activity Main XML 活动主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>

change main activity with this 以此改变主要活动

    @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;
}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM