80,351
社区成员
发帖
与我相关
我的任务
分享
////////////////////////////////////AndroidManifest.xml///////////////////////////////////////
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hello"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="14" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.hello.MainActivity"
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="SecondActivity"
android:label="SecondActivity">
</activity>
</application>
</manifest>
////////////////////////////////////MainActivity.java////////////////////////////////////////
package com.example.hello;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.util.DisplayMetrics;
import android.widget.TextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.content.Intent;
public class MainActivity extends Activity {
//添加一个文本控件 需要在activity_main中添加一个相应的<TextView>标签,
private TextView mTextView01, mTextView02;
private Button mButton01, mButton02;
private Button calculate;
private RadioButton checked;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//和TextView标签中比较常用的查找xml中id的函数,以及单独设置字符串的重载函数(setText)
mTextView01 = (TextView) this.findViewById(R.id.mTextView01);
mTextView01.setText(R.string.welcome);
//打开资源文件的一种方法DOM(应该是吧)
final Resources resources = this.getBaseContext().getResources();
Drawable darw = resources.getDrawable(R.drawable.white);
mTextView01.setBackgroundDrawable(darw);
//第二个textView标签
mTextView02 = (TextView) this.findViewById(R.id.mTextView02);
mTextView02.setText(R.string.test_link);
//使用自带的color类定义文本以及背景颜色
mTextView02.setBackgroundColor(Color.YELLOW);
mTextView02.setTextColor(Color.GREEN);
//必须引用 android.util.DisplayMetrics;*/
DisplayMetrics dw = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dw);
//获取 mTextView02中的字符串,动态更改为屏幕分辨率的值*/
CharSequence str01 = getString(R.string.test_link);
str01 = "这是我手机屏幕的分辨率:" + dw.widthPixels + " " + dw.heightPixels;
mTextView02.setText(str01);
//添加一个按钮*//////////////////出错的是这个按钮(主页面的 转换文字 按钮)
mButton01 = (Button) this.findViewById(R.id.transChar);
mButton01.setOnClickListener(
new OnClickListener() {
public void onClick(View v)
{
mTextView01.setText(R.string.test_link);
mTextView02.setText(R.string.welcome);
//layout 页面转换
//jumptoLayout2();
// 活动事件转换,new 一个Intent, 并指定一个意向的class(类)
Intent intnt = new Intent();
intnt.setClass(MainActivity.this, SecondActivity.class);
//执行
startActivity(intnt);
/*
* 此行表示程序在转换时,该Activity已经运行完毕,所以在点击返回键时不会回到之前的页面,而是直接退出。如果想
* 让程序在按返回键时退回到原先的页面可以注释掉这句话,或者就不要写出来。
*/
MainActivity.this.finish();
}
}
);
//将数据从一个Activity 传入另一个Activity
calculate = (Button) this.findViewById(R.id.calculate);
calculate.setOnClickListener(
new OnClickListener() {
public void onClick( View v) {
//取得输入的身高
EditText et = (EditText) findViewById(R.id.editText1);
double height = Double.parseDouble(et.getText().toString());
//检查单选框
String sex="";
checked = (RadioButton) findViewById(R.id.Man);
if( checked.isChecked())
sex="M";
else
sex="F";
//利用Bundle封装数据
Bundle bundle = new Bundle();
bundle.putDouble("height", height);
bundle.putString("Sex", sex);
//准备转向另一个Activity
Intent SecndActivity = new Intent();
SecndActivity.putExtras(bundle);
SecndActivity.setClass(MainActivity.this, SecondActivity.class);
startActivity(SecndActivity);
}
}
);
}
//页面跳转
public void jumptoLayout2() {
setContentView(R.layout.layout2);
mButton02 = (Button) this.findViewById(R.id.Trans2);
mButton02.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
mTextView01.setText(R.string.welcome);
mTextView02.setText(R.string.test_link);
jumptoLayout1();
}
}
);
}
public void jumptoLayout1() {
setContentView(R.layout.activity_main);
mButton01 = (Button) this.findViewById(R.id.transChar);
mButton01.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
jumptoLayout2();
mTextView01.setText(R.string.test_link);
mTextView02.setText(R.string.welcome);
}
}
);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
/////////////////////////////////////////////SecondActivity.java///////////////////////////////
package com.example.hello;
import java.text.NumberFormat;
import java.text.DecimalFormat;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.Intent;
import android.widget.TextView;
public class SecondActivity extends Activity{
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
Button btn2;
Button DisplayResult;
final TextView DisplayBMI;
btn2 = (Button) this.findViewById(R.id.Trans2);
btn2.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
// new 一个 Intent,并指定一个意向的class(类)
Intent intnt = new Intent();
intnt.setClass(SecondActivity.this, MainActivity.class);
//执行
startActivity(intnt);
/*
* 此行表示程序在转换时,该Activity已经运行完毕,所以在点击返回键时不会回到之前的页面,而是直接退出。如果想
* 让程序在按返回键时退回到原先的页面可以注释掉这句话,或者就不要写出来。
*/
SecondActivity.this.finish();
}
}
);
//取出数据
Bundle bundle = this.getIntent().getExtras();
final double height = bundle.getDouble("height");
final String sex = bundle.getString("Sex");
//获取控件句柄
DisplayBMI = (TextView) this.findViewById(R.id.DisplayBMI);
DisplayResult = (Button) this.findViewById(R.id.Display);
DisplayResult.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
//点击按钮显示结果
NumberFormat nf = new DecimalFormat("0.00");
String s = "你是一位"+getSex(sex)+",\n"+
"你的身高为:"+height+",\n"+
"你的体重为:"+nf.format(getWeight(sex, height));
DisplayBMI.setText(s);
}
}
);
}
//判断性别
public String getSex(String sex) {
String sexText="";
if( sex == "M")
sexText = this.getString(R.string.Man);
else sexText = this.getString(R.string.Woman);
return sexText;
}
//计算体重
public double getWeight( String sex, double high) {
if( getSex(sex) == this.getString(R.id.Man) )
return (high-80) *0.7;
else return (high-60) *0.6;
}
}
01-25 22:42:48.873: E/AndroidRuntime(873): FATAL EXCEPTION: main
01-25 22:42:48.873: E/AndroidRuntime(873): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hello/com.example.hello.SecondActivity}: java.lang.NullPointerException
01-25 22:42:48.873: E/AndroidRuntime(873): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-25 22:42:48.873: E/AndroidRuntime(873): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-25 22:42:48.873: E/AndroidRuntime(873): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-25 22:42:48.873: E/AndroidRuntime(873): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-25 22:42:48.873: E/AndroidRuntime(873): at android.os.Handler.dispatchMessage(Handler.java:99)
01-25 22:42:48.873: E/AndroidRuntime(873): at android.os.Looper.loop(Looper.java:137)
01-25 22:42:48.873: E/AndroidRuntime(873): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-25 22:42:48.873: E/AndroidRuntime(873): at java.lang.reflect.Method.invokeNative(Native Method)
01-25 22:42:48.873: E/AndroidRuntime(873): at java.lang.reflect.Method.invoke(Method.java:511)
01-25 22:42:48.873: E/AndroidRuntime(873): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-25 22:42:48.873: E/AndroidRuntime(873): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-25 22:42:48.873: E/AndroidRuntime(873): at dalvik.system.NativeStart.main(Native Method)
01-25 22:42:48.873: E/AndroidRuntime(873): Caused by: java.lang.NullPointerException
01-25 22:42:48.873: E/AndroidRuntime(873): at com.example.hello.SecondActivity.onCreate(SecondActivity.java:44)
01-25 22:42:48.873: E/AndroidRuntime(873): at android.app.Activity.performCreate(Activity.java:5104)
01-25 22:42:48.873: E/AndroidRuntime(873): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-25 22:42:48.873: E/AndroidRuntime(873): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-25 22:42:48.873: E/AndroidRuntime(873): ... 11 more
<activity
android:name=".SecondActivity"
android:label="SecondActivity">
</activity>
要在SecondActivity加一点