2015-06-14 84 views
0

我有一个使用Android Studio的奇怪问题。我无法设置TextView(tAx)的文本,引用不是'空指针',并且我没有错误或异常,只是我没有看到文本。无法在文本视图中设置文本

我的主类:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    setContentView(R.layout.main); 
    super.onCreate(savedInstanceState); 

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title); 


    tAx = (TextView) findViewById(R.id.Ax); 
    tAx.setText("prova"); 
    // Set up the custom title 
    mTitle = (TextView) findViewById(R.id.title_left_text); 
    // mTitle.setText(R.string.app_name); 
    mTitle = (TextView) findViewById(R.id.title_right_text); 

    // Get local Bluetooth adapter 
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 

    // If the adapter is null, then Bluetooth is not supported 
    if (mBluetoothAdapter == null) { 
     Toast.makeText(this, "Bluetooth is not available",   Toast.LENGTH_LONG).show(); 
     finish(); 
     return; 
    } 
} 

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:weightSum="1"> 

<TextView android:id="@+id/Ax" 
    android:layout_width="198dp" 
    android:layout_height="38dp" 
    android:textSize="20dp" 
    android:visibility="visible" 
    android:background="#ffffffff" 
    android:textColor="#fff" 
/> 


</LinearLayout> 

谢谢你!

更新:

谢谢你!所以现在我可以设置文本但只有一次(第一次)!在另一个课程中,我尝试通过引用来设置文本(使用静态和公开的引用税),但它们不会更改,但仍然是第一个,并且控制台上的Android Studio打印错误:

Only the original thread that created a view hierarchy can touch its views

RemoteBluetooth.tAx.setText(String.valueOf(Ax));

+0

这就是你所有的布局的XML? – ridsatrio

+0

调用super.onCreate后调用setContentView –

+0

是的,它是我所有的布局的xml。我现在开始使用Android。 – alessandroAmedei

回答

0

删除android:weightSum="1"

,并检查您xml文件的其余部分,因为当你在你的xml文件语法错误发生这种情况。

1

您可以通过使用以小写字母开头的ID来解决您的错误。 Android似乎遇到了以大写字母开头的问题。

0

你只是把它白色绘制成白色。将textColor属性更改为其他值。

+0

谢谢你!所以现在我只能设置文本一次!在另一个课程中,我尝试通过引用来设置文本(使用静态和公共的tAx),但它们不会改变,但仍然是第一个!你可以帮我吗?谢谢。 'RemoteBluetooth.tAx.setText(String.valueOf(Ax));' – alessandroAmedei

+0

根据你发布的错误判断,看起来你不能。更新视图只能从UI线程进行管理 - 看起来像您正在尝试从另一个线程设置文本。这可能是一个新问题的材料,但我想这种问题在这里已经有了答案。 – natario

0

你用相同的颜色值设置背景和文字颜色,诅咒你看不到任何改变。

将文本的颜色更改为与背景不同的颜色。

<?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" 
    android:weightSum="1"> 

    <TextView 
     android:id="@+id/Ax" 
     android:layout_width="198dp" 
     android:layout_height="38dp" 
     android:background="#ffffffff" 
     android:textColor="#000" 
     android:textSize="20dp" 
     android:visibility="visible" /> 
</LinearLayout>