gpt4 book ai didi

Android 无法从 Activity 访问 textView 组件

转载 作者:行者123 更新时间:2023-11-29 15:30:18 26 4
gpt4 key购买 nike

我有以下 Activity 。

package org.dewsworld.ui;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class DetailList extends Activity {

TextView title = (TextView) findViewById(R.id.title) ;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

setContentView(R.layout.detail_list);

title.setText("hello world");
}

}

哪个操作detail_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:weightSum="1">
<TextView android:id="@+id/title" android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_height="wrap_content" android:layout_width="match_parent"
android:text="@string/headline" />
<ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="match_parent"></ListView>

</LinearLayout>

但是,当我运行它时,出现运行时错误。 LogCat 是,

enter image description here

最佳答案

这样试试

public class DetailList extends Activity {
TextView title;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_list);

title = (TextView) findViewById(R.id.title) ;
title.setText("hello world");
}
}

关于Android 无法从 Activity 访问 textView 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7595636/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com