首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

Android中怎么实现页面跳转

2012-06-06 
Android中如何实现页面跳转Android中怎样实现页面跳转啊?试了好多次,有时可以,但大多数时候总是会报错!下

Android中如何实现页面跳转
Android中怎样实现页面跳转啊?试了好多次,有时可以,但大多数时候总是会报错!下面是我写的一个小例子,在模拟器中运行的时候就会显示:应用程序意外停止!

下面是java代码:

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/titleView"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:text="@string/titleLable"
  android:textAppearance="?android:attr/textAppearanceLarge"
  android:textColor="@color/titleCocle"
  android:textSize="30sp" />

  <Button
  android:id="@+id/timeButton"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="@string/timeLable"
  android:textColor="@color/textColor"
  android:textSize="25sp" />

</LinearLayout>

time.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/timeView"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:text="@string/TimeLable"
  android:textAppearance="?android:attr/textAppearanceLarge"
  android:textColor="@color/titleCocle"
  android:textSize="30sp" />
   
  <TextView
  android:id="@+id/startView"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/timeStartLable"
  android:textAppearance="?android:attr/textAppearanceLarge" />
   
  <Button
  android:id="@+id/backButton"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/backLable" />

</LinearLayout>


Liuxiuxiu02Activity.java

package lygtc.ruanjian.liuxiuxiu02;

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;

public class Liuxiuxiu02Activity extends Activity implements OnClickListener{
  /** Called when the activity is first created. */
private Button timeButton;
  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
   
  timeButton = (Button)findViewById(R.id.timeButton);
  timeButton.setOnClickListener(this);
  }
@Override


public void onClick(View v) {
// TODO Auto-generated method stub
int id = v.getId();

switch(id){
case R.id.timeButton:{
Intent timeIntent = new Intent(this,timeActivity.class);
startActivity(timeIntent);
}
}

}
}

timeActivity.class
package lygtc.ruanjian.liuxiuxiu02;

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;

public class timeActivity extends Activity implements OnClickListener{

private Button backButton;
  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.time);
   
  backButton = (Button)findViewById(R.id.timeButton);
  backButton.setOnClickListener(this);
  }
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id = v.getId();

switch(id){
case R.id.backButton:{
Intent backIntent = new Intent(this,Liuxiuxiu02Activity.class);
startActivity(backIntent);
}
}
}
}

AndroidManifest.xml中也加入了 
<activity android:name=".timeActivity" android:label="@string/TimeLable"></activity>


错误提示:
06-02 09:28:48.529: E/AndroidRuntime(527): java.lang.RuntimeException: Unable to start activity ComponentInfo{lygtc.ruanjian.liuxiuxiu02/lygtc.ruanjian.liuxiuxiu02.timeActivity}: java.lang.NullPointerException
06-02 09:28:48.529: E/AndroidRuntime(527): Caused by: java.lang.NullPointerException


 

[解决办法]
Caused by: java.lang.NullPointerException 空指针异常啊。

 timeButton = (Button)findViewById(R.id.timeButton);
timeButton.setOnClickListener(this);

timeButton 是null ,原因是 R.id.timeButton的id在time.xml中没有。

弄弄错了 。

还有就是你类名大写下,都不符合java规范的。
timeButton = (Button)findViewById(R.id.timeButton);
改成
timeButton = (Button)findViewById(R.id.backButton);


完了 ,给分 。

个人技术博客 : http://www.happyalaric.com

热点排行