-2

I am following an online tutorial on Udemy. I created the default hello world app, but it would not load when I tried to run it! I decided to go to the next video and created a simple project that should display the text "Hello, World!" when a button is pressed, but I get this message on the emulator when I run the project:

Unfortunately Hello World has stopped.

I have been trying for hours searching multiple threads, but none seem to give me a solution. I am using the latest adt bundle (API 20) version 23. Here are the two files:

MainActivity.java

package com.ebook.helloworld;
import android.app.Activity;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onBtnTouch(View v){
        TextView tv = (TextView) findViewById(R.id.textView1);
        tv.setText("Hello, World!");        
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    } 
}

activity_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.ebook.helloworld.MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:onClick="onBtnTouch"
        android:text="Say Hi" />

    <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="93dp" 
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>
4

1 回答 1

0

你在哪里设置的按钮?你应该把 onCreate:

Button button1 = (Button)this.findViewById (R.id.button1);

然后你可以:

button1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            //do what you want to do...

        }
});
于 2014-07-25T11:14:18.470 回答