HelloWorldActivity.javapackage com.chk.helloworld;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;/**
 * 创建Activity的要点 1、一个Activity就是一个类,并且要继承Activity 
 * 2、需要复写onCreate这个方法
 * 3、每一个Activity都需要在AndroidManifest.xml中进行配置 
 * 4、为Activity添加必要的控件
 * 
 * @author Administrator
 * 
 */
public class HelloWorldActivity extends Activity {
/** Called when the activity is first created. */
private Button myButton = null; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myButton = (Button) findViewById(R.id.myButton);
myButton.setText("请按我!");
myButton.setOnClickListener(new MyButtonListenter());
} class MyButtonListenter implements OnClickListener { public void onClick(View v) {
// 生成一个Intent对象
Intent intent = new Intent();
intent.setClass(HelloWorldActivity.this, OtherActivity.class);
HelloWorldActivity.this.startActivity(intent);
}
}
}OtherActivity.java
package com.chk.helloworld;import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;public class OtherActivity extends Activity {
private TextView myTextView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
myTextView = (TextView)findViewById(R.id.myText);
myTextView.setText(R.string.other);
}
}AndroidManifest.xml<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.chk.helloworld"
    android:versionCode="1"
    android:versionName="1.0" >    <uses-sdk android:minSdkVersion="15" />    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".HelloWorldActivity"
            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=".OtherAvtivity" android:label="@string/other">
        </activity>
    </application></manifest>问题:
虚拟机运行起来之后,可以看到第一个Activity(也就是HelloWorldActivity),当我点击Button时,本来应该调用OtherActivity的,但是却出现了这样的提示:很抱歉,“HelloWorld”已停止运行,请问这是怎么回事儿请各位高手详细解答一下,我刚接触Android。

解决方案 »

  1.   

    会不会是 myTextView 是 null?
      

  2.   

    查看一下logcat里面的警告看下.....
      

  3.   

    哈哈!你们都没有看出来
    其实今天上午我就看出来了
    AndroidManifest.xml中注册的时候
    一个单词写错了
    <activity android:name=".OtherAvtivity" android:label="@string/other">
    应该是
    <activity android:name=".OtherActivity" android:label="@string/other">
      

  4.   

    很多时候都是因为不够细心才出Bug的
      

  5.   

    问题是这样你能编译通过??eclipse安装有问题把??