以下程序有错误,闪退 先mark下,回头改
————————Activity03————————
package com.example.activity03;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
//1、 在Activity03中什声明四个控件:2个EditText(数字输入框),1个TextView(乘以),1个Button(计算)。-->修改activity03_layout。xml文件
//2、要为其中的两个控件(1个TextView(乘以)、1个Button(计算))设置显示的值
//3、创建一个监听器类,用来监听按钮按下的动作
//4、将监听器类的对象绑定在按钮对象上
public class Activity03 extends Activity {
//1)在cativity03_layout.xml中声明好了四个控件过后,
//2)在Activity中将这四个控件取出来:-->声明代表这四个控件的对象-->用findViewById()方法取出控件
private EditText factorone;
private EditText factortwo;
private TextView symbol;
private Button calculate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity03_layout);
//3)根据控件的ID来取得代表控件的对象-->findViewById(R.id.**)
factorone=(EditText)findViewById(R.id.factorone);
factortwo=(EditText)findViewById(R.id.factortwo);
symbol=(TextView)findViewById(R.id.symbol);
calculate=(Button)findViewById(R.id.calculate);
//4)为symbol和calculate设置显示的值
//方法一:直接写入字符串“乘以”/“计算”。有点:简单方便;缺点:考虑到范围可能会有多种语言,直接赋值的方式,修改起来非常麻烦
//symbol.setText("乘以");
//calculate.setText("计算");
// 方法二:1)在string中配置字符串,然后由Activity03调用----推荐!
//()内不再是字符串,而是对应的Id.优点:应用程序中并无相应的值,只有其Id,真正的值在strings.xml中,如需修改,只需要在strings.xml中修改
symbol.setText(R.string.symbol);
calculate.setText(R.string.calculate);
//11)将监听器对象绑定到按钮(calculate)对象上面去
calculate.setOnClickListener(new CalculateListener());
}
//20)添加Menu菜单,回调函数,当在手机上电极Menu按钮时就会执行该函数---Menu于前面EditText,Button等相比,不需要在布局文件中进行配置,直接复写onCreatextMenuClosed(方法)即可
//该方法的作用是,当客户点击Menu按钮时候,就会调用该方法
@Override
public void onContextMenuClosed(Menu menu) {
menu.add(0, 1, 1, R.string.exit);
menu.add(0,2,2,R.string.about);
// TODO Auto-generated method stub
//return super.onContextMenuClosed(menu);
super.onContextMenuClosed(menu);
}
//21)给退出/关于添加动作--->当点击Item时就会有动作,并把点击的值传进(item)去
//当客户点击菜单中的某一个选项时,会调用该方法,并把点击的选项最为参数传进去
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==1);
finish();
// TODO Auto-generated method stub
return super.onOptionsItemSelected(item);
}
//5)编写应用程序的监听器(内部类,比较常见的,eg:在多线程程序中使用内部类来隐藏run函数;在编写模板回调模式时使用匿名内部类作为回调接口的实现)
//1、在内部类中,可以直接使用外部类的成员变量,但不能拥有;2、可以再内部类中使用外部类的成员函数/对象
//如果对Listener不太了解,可以Google“Observer设计模式”-->有助于了解Listener
class CalculateListener implements OnClickListener{
@Override
public void onClick(View v) {
//6)取得两个EditText控件的值
String factoroneStr = factorone.getText().toString();
String factortwoStr = factortwo.getText().toString();
//7)将这两个值存放到Intent对象当中
Intent intent = new Intent();
//8)调用intent。put()方法将键值对传进intent中去
intent.putExtra("one",factoroneStr);
intent.putExtra("two",factortwoStr);
//9)设置intent启动哪一个Activity
intent.setClass(Activity03.this, ResultActivity.class);
//10)使用这个Intent对象来启动ResultActivity-->调用现在的Activity的startActivity()方法,以启动跳转
Activity03.this.startActivity(intent);
}
}
}
——————activity03_layout.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.example.activity03.Activity03" >
<EditText
android:id="@+id/factorone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/symbol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
/>
<Editext
android:id="@+id/factortwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/calculate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
—————————ResultActivity————————
package com.example.activity03;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
//接收从Activity03中所传入的值
//计算两个值得乘积
//将计算结果显示在Activity上-->要想显示,最少应该有一个TextView控件用来显示-->要在ResultActivity的布局文件当中配置一个TestView
//------以下步骤基本和Activity03类似------
//12)在result。xml中配置一个TextView
public class ResultActivity extends Activity{
//13)声明TextView控件的对象
private TextView resultView;
//14)导入OnCreate()方法的复写文件
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//15)使该Activity使用result。xml的布局
setContentView(R.layout.result);
//16)根据控件的Id来得到控件对象
resultView= (TextView)findViewById(R.id.result);
//得到intent对象中的值
//1、先得到intent对象
Intent intent = getIntent();
//2、把Activity03中传入的键再传入intent。getStringExtra()方法中,以通过键得到值
String factoroneStr = intent.getStringExtra("one");
String factortwoStr = intent.getStringExtra("two");
//17)通过键得出的值都是String类型,无法相乘,所以还得转化成整型-->用Integer。parseInt()方法抽取一下
int factoroneInt = Integer.parseInt(factoroneStr);
int factortwoInt = Integer.parseInt(factortwoStr);
//18)计算两个值得乘积
int result = factoroneInt * factortwoInt;
//19)在result的TextView中(resultView中)将结果显示出来(注意:setText()的输出结果为String类型,所以要把结果转化为字符串类型,其方法为:根据Java语法:连接符两端有一端是字符串,另外一段也将会是字符串,然后显示,所以相乘结果也是String类型)
resultView.setText(result + "");
}
}
—————————result.xml—————————
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
—————————string.xml——————————
<resources>
<string name="app_name">Activity03</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="symbol">乘以</string>
<string name="calculate">计算</string>
<string name="exit">退出</string>
<string name="about">关于</string>
</resources>
——————Activity03Manifest.xml———————
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activity03"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Activity03"
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>
</manifest>
——————————R.java———————————
/* AUTO-GENERATED FILE. DO NOT MODIFY.
- This class was automatically generated by the
- aapt tool from the resource data it found. It
- should not be modified by hand.
*/
package com.example.activity03;
public final class R {
public static final class attr {
}
public static final class dimen {
/** Default screen margins, per the Android Design guidelines.
Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively).
*/
public static final int activity_horizontal_margin=0x7f040000;
public static final int activity_vertical_margin=0x7f040001;
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int action_settings=0x7f080005;
public static final int calculate=0x7f080003;
public static final int factorone=0x7f080000;
public static final int factortwo=0x7f080002;
public static final int result=0x7f080004;
public static final int symbol=0x7f080001;
}
public static final class layout {
public static final int activity03_layout=0x7f030000;
public static final int result=0x7f030001;
}
public static final class menu {
public static final int activity03=0x7f070000;
}
public static final class string {
public static final int about=0x7f050006;
public static final int action_settings=0x7f050002;
public static final int app_name=0x7f050000;
public static final int calculate=0x7f050004;
public static final int exit=0x7f050005;
public static final int hello_world=0x7f050001;
public static final int symbol=0x7f050003;
}
public static final class style {
/**
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
API 11 theme customizations can go here.
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
API 14 theme customizations can go here.
/
public static final int AppBaseTheme=0x7f060000;
/* Application theme.
All customizations that are NOT specific to a particular API-level can go here.
*/
public static final int AppTheme=0x7f060001;
}
}
以下是新写代码,但bug仍未解决,估计是Activity03中,caiculate.setOnClickListener((android.view.view.OnClickListener)new calculateListener());方法错误,
原文中是:
caiculate.setOnClickListener(new calculateListener());
但我在Eclipse中这样写时调试报错。
————————Activity03————————
package com.example.actyvity03;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Activity03 extends Activity {
private EditText factorOne;
private EditText factorTwo;
private TextView symbol;
private Button calculate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
factorOne=(EditText)findViewById(R.id.factorOne);
factorTwo=(EditText)findViewById(R.id.factorTwo);
symbol=(TextView)findViewById(R.id.symbol);
calculate=(Button)findViewById(R.id.calculate);
symbol.setText(R.string.symbol);
calculate.setText(R.string.calculate);
//下行标记-->学习observe设计模式
calculate.setOnClickListener( (android.view.View.OnClickListener) new calculateListener());
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.add(0, 1, 1, R.string.exit);
menu.add(0,2,2,R.string.about);
// TODO Auto-generated method stub
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
return super.onOptionsItemSelected(item);
}
class calculateListener implements OnClickListener{
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
String factorOneStr = factorOne.getText().toString();
String factorTwoStr = factorTwo.getText().toString();
Intent intent = new Intent();
intent.putExtra("one", factorOneStr);
intent.putExtra("two", factorTwoStr);
intent.setClass(Activity03.this,ResultActivity.class);
Activity03.this.startActivity(intent);
}
}
}
——————activity03_layout(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.example.actyvity03.Activity03" >
<EditText
android:id="@+id/factorOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/symbol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<EditText
android:id="@+id/factorTwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/calculate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
—————————ResultActivity————————
package com.example.actyvity03;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class ResultActivity extends Activity {
private TextView resultView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
resultView=(TextView)findViewById(R.id.result);
Intent intent = getIntent();
String factorOneStr = intent.getStringExtra("one");
String factorTwoStr = intent.getStringExtra("two");
int factorOneint = Integer.parseInt(factorOneStr);
int factorTwoint = Integer.parseInt(factorTwoStr);
int result = factorOneint * factorTwoint ;
resultView.setText(result + "");
}
}
—————————result.xml—————————
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
——————Activity03Manifest.xml———————
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.actyvity03"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Activity03"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<ResultActivity
android:name=".ResultActivity"
android:label="@string/app_name"
/>
</application>
</manifest>
—————————string.xml——————————
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Actyvity03</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="symbol">乘以</string>
<string name="calculate">计算</string>
<string name="exit">退出</string>
<string name="about">关于</string>
</resources>
——————————R.java———————————
/* AUTO-GENERATED FILE. DO NOT MODIFY.
- This class was automatically generated by the
- aapt tool from the resource data it found. It
- should not be modified by hand.
*/
package com.example.actyvity03;
public final class R {
public static final class attr {
}
public static final class dimen {
/** Default screen margins, per the Android Design guidelines.
Example customization of dimensions originally defined in res/values/dimens.xml
(such as screen margins) for screens with more than 820dp of available width. This
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively).
*/
public static final int activity_horizontal_margin=0x7f040000;
public static final int activity_vertical_margin=0x7f040001;
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int action_settings=0x7f080005;
public static final int calculate=0x7f080003;
public static final int factorOne=0x7f080000;
public static final int factorTwo=0x7f080002;
public static final int result=0x7f080004;
public static final int symbol=0x7f080001;
}
public static final class layout {
public static final int main=0x7f030000;
public static final int result=0x7f030001;
}
public static final class menu {
public static final int activity03=0x7f070000;
}
public static final class string {
public static final int about=0x7f050006;
public static final int action_settings=0x7f050002;
public static final int app_name=0x7f050000;
public static final int calculate=0x7f050004;
public static final int exit=0x7f050005;
public static final int hello_world=0x7f050001;
public static final int symbol=0x7f050003;
}
public static final class style {
/**
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
API 11 theme customizations can go here.
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
API 14 theme customizations can go here.
/
public static final int AppBaseTheme=0x7f060000;
/* Application theme.
All customizations that are NOT specific to a particular API-level can go here.
*/
public static final int AppTheme=0x7f060001;
}
}
网友评论