首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

初学者请问,关于eclipse中从一个activity跳转到另一个activity运行unfortunately activity has stopped时提示

2012-09-04 
菜鸟请教,关于eclipse中从一个activity跳转到另一个activity运行unfortunately activity has stopped时提

菜鸟请教,关于eclipse中从一个activity跳转到另一个activity运行unfortunately activity has stopped时提示
其中一个activity代码如下:
package mares.activity02;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
public class Activity02Activity extends Activity {
  /** Called when the activity is first created. */

  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  TextView myTextView = (TextView)findViewById(R.id.myTextView);
  Button myButton = (Button)findViewById(R.id.myButton);
  myTextView.setText("我的第一个TextView");
  myButton.setText("我的第一个Button");
  myButton.setOnClickListener((android.view.View.OnClickListener) new MyButtonListener());
}
  class MyButtonListener implements OnClickListener{
  public void onClick(View v){
  Intent intent = new Intent();
  intent.setClass(Activity02Activity.this, otherActivity.class);
  Activity02Activity.this.startActivity(intent);
  }
 
  }
}





另一个otheractivity代码如下:

package mares.activity02;
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) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
TextView myTextView= (TextView)findViewById(R.id.myTextView);
myTextView.setText(R.string.other);



}
public TextView getMyTextView() {
return myTextView;
}
public void setMyTextView(TextView myTextView) {
this.myTextView = myTextView;
}

}

main.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >

  <TextView
  android:id="@+id/myTextView"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello" />
  <Button
  android:id="@+id/myButton"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  />
</LinearLayout>


other.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_wi
dth="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >

  <TextView
  android:id="@+id/myTextView"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello" />
  <Button
  android:id="@+id/myButton"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  />
</LinearLayout>

strings.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>


<resources>

  <string name="hello">Hello World, Activity02Activity!</string>
  <string name="app_name">Activity02</string>
<string name="other">otherActivity</string>
</resources>

androidmanifest.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="mares.activity02"
  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=".Activity02Activity"
  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=".otherActivity" android:label="@string/other"/>
  </application>

</manifest>

运行后截图如下:
希望高手帮忙解决

[解决办法]
那是因为你没有正确import导入包,解决办法:

在顶部import android.os.Bundle;后面加上
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;

然后
myButton.setOnClickListener((android.view.View.OnClickListener) new MyButtonListener()); 
这一行,改为
myButton.setOnClickListener(new MyButtonListener());

热点排行