有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么我的文本视图不能正确显示其字符串?

我正在使用我正在阅读的安卓编程书中的一个例子。本练习的要点是,当我单击“关于”按钮时,一个新的活动应该开始并显示一些文本。由于某些原因,即使文本显示在我的IDE的图形布局中,文本也不会显示。我用我的手机作为模拟器,我的手机运行的是安卓4.0.3。我正在使用eclipse。这是我的密码:

主要活动:

package org.example.sodoku;

import 安卓.app.Activity;
import 安卓.content.Intent;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.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:安卓="http://schemas.安卓.com/apk/res/安卓"
   安卓:orientation="vertical"
   安卓:layout_width="fill_parent"
   安卓:layout_height="fill_parent"
   安卓:background="@color/background"
   安卓:gravity="center"
   安卓:padding="35dip">
   <TextView
      安卓:text="@string/main_title"
      安卓:layout_height="wrap_content"
      安卓:layout_width="wrap_content"
      安卓:layout_gravity="center"
      安卓:layout_marginBottom="20dip"
      安卓:textSize="24.5sp" />
   <TableLayout
      安卓:layout_height="wrap_content"
      安卓:layout_width="wrap_content"
      安卓:layout_gravity="center"
      安卓:stretchColumns="*">
      <TableRow>
         <Button
            安卓:id="@+id/continue_button"
            安卓:text="@string/continue_label" />
         <Button
            安卓:id="@+id/new_button"
            安卓:text="@string/new_game_label" />
      </TableRow>
      <TableRow>
         <Button
            安卓:id="@+id/about_button"
            安卓:text="@string/about_label" />
         <Button
            安卓:id="@+id/exit_button"
            安卓:text="@string/exit_label" />
      </TableRow>
   </TableLayout>
</LinearLayout>

关于课程:

package org.example.sodoku;

import 安卓.app.Activity;
import 安卓.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:安卓="http://schemas.安卓.com/apk/res/安卓"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:padding="10dip">


    <TextView
        安卓:id="@+id/about_content"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        安卓: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:安卓="http://schemas.安卓.com/apk/res/安卓"
    package="org.example.sodoku"
    安卓:versionCode="1"
    安卓:versionName="1.0" >

    <uses-sdk 安卓:minSdkVersion="10" />

    <application
        安卓:icon="@drawable/ic_launcher"
        安卓:label="@string/app_name" >
        <activity
            安卓:name=".Sudoku"
            安卓:label="@string/app_name" >
            <intent-filter>
                <action 安卓:name="安卓.intent.action.MAIN" />

                <category 安卓:name="安卓.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity 
            安卓:name=".About"
            安卓:label="@string/about_title">
        </activity>
    </application>

</manifest>

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


共 (2) 个答案

  1. # 1 楼答案

    您在About活动(比较OnCreate()onCreate())中拼写错误了onCreate()方法,所以实际上并没有重写基类方法。将onCreate替换为以下内容:

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about);
    }
    
  2. # 2 楼答案

    这可能对你有帮助。用这一个作为例子。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>