SayHelloWorld.java:package com.helloworld;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;public class SayHelloWorld extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView view = (TextView) findViewById(R.layout.test);
view.setText("布局设置文本");
Button btn = (Button)findViewById(R.layout.button);
btn.setOnClickListener(new MyEvent());
}
class MyEvent implements View.OnClickListener{ public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(SayHelloWorld.this, Activity01.class);
SayHelloWorld.this.startActivity(intent);
}
}
}
Activity01.java:package com.helloworld;import android.app.Activity;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;public class Activity01 extends Activity{
Button btn = null;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity01);
TextView view=(TextView)findViewById(R.layout.activity01);
view.setText("TEST");
}
}layout下activity01.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">
<TextView android:layout_width="fill_parent" android:id="@+layout/activity01" />
<Button android:text="@string/app_name" android:id="@+layout/button" />
</LinearLayout>main.xml:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:id="@+layout/test"
/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+layout/button" />
</LinearLayout>AndroidManifest.xml注册Activity01:<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.helloworld"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/testimg" android:label="@string/app_name">
<activity android:name=".SayHelloWorld"
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=".Activity01" android:label="@string/app_name"/>
</application>
<uses-sdk android:minSdkVersion="4" />
</manifest> 问题:
为什么从SayHelloWrold点击按钮跳转到Activity01.java提示意外中止?
另外我的程序含有另外一个类Activity02.java,但没用到,没有注册,应该没有影响
请教各位,谢谢
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;public class SayHelloWorld extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView view = (TextView) findViewById(R.layout.test);
view.setText("布局设置文本");
Button btn = (Button)findViewById(R.layout.button);
btn.setOnClickListener(new MyEvent());
}
class MyEvent implements View.OnClickListener{ public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(SayHelloWorld.this, Activity01.class);
SayHelloWorld.this.startActivity(intent);
}
}
}
Activity01.java:package com.helloworld;import android.app.Activity;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;public class Activity01 extends Activity{
Button btn = null;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity01);
TextView view=(TextView)findViewById(R.layout.activity01);
view.setText("TEST");
}
}layout下activity01.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">
<TextView android:layout_width="fill_parent" android:id="@+layout/activity01" />
<Button android:text="@string/app_name" android:id="@+layout/button" />
</LinearLayout>main.xml:<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:id="@+layout/test"
/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+layout/button" />
</LinearLayout>AndroidManifest.xml注册Activity01:<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.helloworld"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/testimg" android:label="@string/app_name">
<activity android:name=".SayHelloWorld"
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=".Activity01" android:label="@string/app_name"/>
</application>
<uses-sdk android:minSdkVersion="4" />
</manifest> 问题:
为什么从SayHelloWrold点击按钮跳转到Activity01.java提示意外中止?
另外我的程序含有另外一个类Activity02.java,但没用到,没有注册,应该没有影响
请教各位,谢谢
解决方案 »
- android 平台如何实现对PDF文件的编辑操作
- 如何设置Android系统 的 亮度
- 搭建Android环境出现的问题,困扰我4天,求各位高人帮忙
- android jni 中文乱码
- android sdk 更新
- 安卓4.0通讯录源代码
- android手机无线网络定位的问题
- android如何将图标停在状态栏 类似于微信收到消息有个小图标 且不覆盖原有内容
- 初学者问题,写入读取数据异常,运行时报错
- android 重启应用(自己) 失败
- HTC Desire Z AudioRecord issue: AudioHardwareMSM7X30 cannot open /dev/msm_pcm_in
- android客户端向服务端传输对象。报了一个异常,请大家解决啊
应该是
TextView view=(TextView)findViewById(R.id.activity01);
吧是id,不是layout
android:id="@+id/activity01"啊还有android:id="@+layout/button改成
android:id="@+id/button"
配置文件我是@+layout 和id一样的
@后面加的要和前面一样吧,要么把id改为layout,要么把layout改为id
TextView view=(TextView)findViewById(R.layout.activity01);
即使是这样,这两个id重了也不妥吧,TextView的id换个别的
“配置文件我是@+layout”,晒晒你的配置文件
<TextView android:layout_width="fill_parent" android:id="@+layout/activity01" />与你的layout文件:
activity01.xml重名我想,当你在程序中 R.layout.activity01 找到的可能是TextView 而不是 layout
配置文件也重命名了,还是有错误
还有你这里面只有一个按钮、我感觉没必要再写一个MyEvent()、直接写Click事件就好、
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* 以findViewById()取得Button 对象,并添加onClickListener */
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
/* new 一个Intent 对象,并指定要启动的class */
Intent intent = new Intent();
intent.setClass(AndroidUI011.this, Android02Activity.class);
/* 调用一个新的Activity */
Android01.this.startActivity(intent);//(或者写成startActivity(intent);)
/* 关闭原本的Activity */
Android01.this.finish();
}
});
}
}
好像是、不知道我是不是记错了、
这句错了,不好意思
改成intent.setClass(Android01.this, Android02Activity.class);