2012-07-26 53 views
0

我正在使用一个我正在经历的android编程书籍的例子。这个练习的要点是当我点击“关于”按钮时,一个新的活动应该开始并显示一些文本。出于某种原因,即使在我的IDE中的图形布局中显示文本,文本也不会显示出来。我使用手机作为模拟器,手机运行的是Android 4.0.3。我正在使用eclipse。这里是我的代码:为什么我的TextView不能正确显示它的字符串?

主要活动:

package org.example.sodoku; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 

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

     View continueButton= findViewById(R.id.continue_button); 
     continueButton.setOnClickListener(this); 
     View newButton= findViewById(R.id.new_button); 
     newButton.setOnClickListener(this); 
     View aboutButton= findViewById(R.id.about_button); 
     aboutButton.setOnClickListener(this); 
     View exitButton= findViewById(R.id.exit_button); 
     exitButton.setOnClickListener(this); 
    } 

    public void onClick(View v) { 
     // TODO Auto-generated method stub 

     switch (v.getId()){ 
     case R.id.about_button: 
      Intent i = new Intent(this, About.class); 
      startActivity(i); 
      break; 
     } 

    } 
} 

主要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" 
    android:background="@color/background" 
    android:gravity="center" 
    android:padding="35dip"> 
    <TextView 
     android:text="@string/main_title" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_gravity="center" 
     android:layout_marginBottom="20dip" 
     android:textSize="24.5sp" /> 
    <TableLayout 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_gravity="center" 
     android:stretchColumns="*"> 
     <TableRow> 
     <Button 
      android:id="@+id/continue_button" 
      android:text="@string/continue_label" /> 
     <Button 
      android:id="@+id/new_button" 
      android:text="@string/new_game_label" /> 
     </TableRow> 
     <TableRow> 
     <Button 
      android:id="@+id/about_button" 
      android:text="@string/about_label" /> 
     <Button 
      android:id="@+id/exit_button" 
      android:text="@string/exit_label" /> 
     </TableRow> 
    </TableLayout> 
</LinearLayout> 

关于类:

package org.example.sodoku; 

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


public class About extends Activity { 
    protected void OnCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.about); 





    } 

} 

有关XML:

<?xml version="1.0" encoding="utf-8"?> 

<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_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" > 
</TextView> 


    </ScrollView> 

[编辑]忘了字符串的XML和清单:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <string name="hello">Hello World, Sudoku!</string> 
    <string name="app_name">Sudoku</string> 
    <string name="main_title">Android Sodoku</string> 
    <string name="continue_label">Continue</string> 
    <string name="new_game_label">New Game</string> 
    <string name="about_label">About</string> 
    <color name="background">#3500ffff</color> 
    <string name="exit_label">Exit</string> 
    <string name="about_title">About Android Sudoku</string> 
    <string name="about_text">fuck your ethnicity</string> 
</resources> 

清单:

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

    <uses-sdk android:minSdkVersion="10" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name=".Sudoku" 
      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> 

</manifest> 

任何帮助将不胜感激,谢谢。

+1

什么是字符串“about_text”的值(我认为是在你的res/values/strings.xml中)? – matt5784 2012-07-26 22:35:50

+0

请注意'Amount.xml'中的大写'A' [如果这是您在项目中命名的方式]。你的java代码有'amount.xml' – Nerd 2012-07-26 22:44:43

+0

你是否在清单文件中添加了About.class? LogCat中是否有任何错误? – cliff2310 2012-07-26 22:52:31

回答

6

您已在关于活动的拼写错误onCreate()方法(比较OnCreate()onCreate()),所以你不实际覆盖基类的方法。用这个替换你的onCreate:

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.about); 
} 
1

它可以帮助你。使用这个作为about.xml。

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/scroller" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:fillViewport="true" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:orientation="vertical" > 

    <TextView 
     android:id="@+id/about_content" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/about_text" > 
    </TextView> 

    </LinearLayout> 

</ScrollView> 
+0

将它放入LinearLayout会有什么不同? ScrollView不能自己显示TextView吗? (诚​​实的问题) – matt5784 2012-07-26 22:53:17

+0

@ matt5784应该没有区别AFAIK – FoamyGuy 2012-07-26 22:55:27

相关问题