有一个问题困扰我好几天了。怎么也解决不了。
我在一个工程里面创建了2个activity,一个主activity 一个子activity 。在主activity里面有一个button点击事件,跳转到子activity。代码说明:工程名称:fyjTest 包名称:com.mypackage.test 主activity名称:TestActivity 子activity名称 :About主activity TestActivity.java代码:package com.mypackage.test;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;public class TestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View view = findViewById(R.id.ok);
view.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(TestActivity.this,About.class);
startActivity(i);
}
});
}
}主activity的配置文件 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"> <Button android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>子activity:About.java 的代码 package com.mypackage.test;
import com.mypackage.test.R;import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;public class About extends Activity{
public void onCreated(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
}子activity:About.java的配置文件about.xml<?xml version="1.0" encoding="utf-8"?><ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip">
<TextView
android:id="@+id/about_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/about_text" />
</ScrollView>
工程的AndroidManifest.xml的文件 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypackage.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name"
android:debuggable="true">
<activity android:name=".TestActivity"
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=".About"
android:label="@string/about_title">
</activity>
</application>
<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="8" />
</manifest>
res\value\string.xml的文件内容如下:<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, TestActivity!</string>
<string name="app_name"></string>
<string name="about_title">About Android Sudoku</string>
<string name="about_text">\
Sudoku is a logic-based number placement puzzle.
Starting with a partially completed 9x9 grid, the
objective is to fill the grid so that each
row, each column, and each of the 3x3 boxes
(also called <i>blocks</i>) contains the digits
1 to 9 exactly once.
</string>
</resources>
最后的问题是:如果正常跳转到About activity里面,应该能运行其onCreate方法。能过显示出一个TextView 并在TextView里面显示字符串:
Sudoku is a logic-based number placement puzzle.
Starting with a partially completed 9x9 grid, the
objective is to fill the grid so that each
row, each column, and each of the 3x3 boxes
(also called <i>blocks</i>) contains the digits
1 to 9 exactly once.可是最后运行的结果:点击button 调转到About 但是不显示TextView。 请问各位,这是什么原因。多谢了。
我在一个工程里面创建了2个activity,一个主activity 一个子activity 。在主activity里面有一个button点击事件,跳转到子activity。代码说明:工程名称:fyjTest 包名称:com.mypackage.test 主activity名称:TestActivity 子activity名称 :About主activity TestActivity.java代码:package com.mypackage.test;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;public class TestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View view = findViewById(R.id.ok);
view.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(TestActivity.this,About.class);
startActivity(i);
}
});
}
}主activity的配置文件 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"> <Button android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>子activity:About.java 的代码 package com.mypackage.test;
import com.mypackage.test.R;import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;public class About extends Activity{
public void onCreated(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
}子activity:About.java的配置文件about.xml<?xml version="1.0" encoding="utf-8"?><ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip">
<TextView
android:id="@+id/about_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/about_text" />
</ScrollView>
工程的AndroidManifest.xml的文件 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mypackage.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name"
android:debuggable="true">
<activity android:name=".TestActivity"
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=".About"
android:label="@string/about_title">
</activity>
</application>
<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="8" />
</manifest>
res\value\string.xml的文件内容如下:<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, TestActivity!</string>
<string name="app_name"></string>
<string name="about_title">About Android Sudoku</string>
<string name="about_text">\
Sudoku is a logic-based number placement puzzle.
Starting with a partially completed 9x9 grid, the
objective is to fill the grid so that each
row, each column, and each of the 3x3 boxes
(also called <i>blocks</i>) contains the digits
1 to 9 exactly once.
</string>
</resources>
最后的问题是:如果正常跳转到About activity里面,应该能运行其onCreate方法。能过显示出一个TextView 并在TextView里面显示字符串:
Sudoku is a logic-based number placement puzzle.
Starting with a partially completed 9x9 grid, the
objective is to fill the grid so that each
row, each column, and each of the 3x3 boxes
(also called <i>blocks</i>) contains the digits
1 to 9 exactly once.可是最后运行的结果:点击button 调转到About 但是不显示TextView。 请问各位,这是什么原因。多谢了。
解决方案 »
- 请教androidi imageview点击丢失焦点问题
- Android 布局全用代码写(来者有分)
- xml解析,求高手指点为什么报错了
- 如何做一个"正在连接,请稍等..."的提示框,然后继续往下运行,完成后工作后由程序关掉
- 让listView默认执行第一个item android
- 这个return返回语句的区别在哪?
- Android的异常有没有好的解决办法啊
- Delphi XE5 Android
- 最令程序员恶心的循环
- 400多套安卓源码,Android +4高级编程第三版及源码,云盘下载
- 要做一个andriod版的B2C网上商城,有经验的给点意见
- Java.lang.IndexOutOfBoundException错误
public void onCreated(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
}崩溃了,看了半天才发现您的方法名称写错了,是onCreate而不是onCreated。