当前位置: 代码迷 >> Android >> startActivity时出错解决办法
  详细解决方案

startActivity时出错解决办法

热度:848   发布时间:2016-05-01 22:30:52.0
startActivity时出错
今天做android开发一个小项目怎么都调不通,然后就随便建了个测试用例,发现startActivity函数竟然总是出错,我已经在AndroidManifest.xml 对新建的页面进行的注册了,但是依然不行不知道是什么原因,现在贴出来代码请大家指教!
Activity01代码

package An.Android;
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 TestA extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
   
  Button to_anoter = (Button) findViewById(R.id.button1); //测试打开新的activity
  to_anoter.setOnClickListener(new OnClickListener()
  {
  public void onClick(View v) 
  {
  // TODO Auto-generated method stub

  Intent intent0 = new Intent(TestA.this,anotherActivity.class);  
  setTitle("test");  
  startActivity(intent0);  
  TestA.this.finish();
  //CityWeather.this.finish();
  }
 
  });
  }
   
   
}

对应的mian.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"
  />
<Button android:text="试试" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>

Activity02的代码如下:

package An.Android;

import android.app.Activity;
import android.os.Bundle;

public class anotherActivity extends Activity{
/** Called when the activity is first created. */
  @Override
   
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.another);
  };

}
对应的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="你好啊!"
  />

</LinearLayout>

AndroidManifest.xml 对应如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="An.Android"
  android:versionCode="1"
  android:versionName="1.0">


  <application android:icon="@drawable/icon" android:label="@string/app_name">
  <activity android:name=".TestA"
  android:label="@string/app_name">
  相关解决方案