我正在研究学校项目。 setText()方法可以工作一段时间,而其他时候似乎没有做任何事情。无法找出原因。下面是我的java类,我试图将值设置为视图。我可以在下面的日志语句中找到正确的值。
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class CourseDetail extends Activity{
TextView txtcourse;
TextView txtcoursetitle;
TextView txthours;
TextView txtsession;
TextView txtunits;
TextView txtconditions;
TextView txtdescription;
String course,courseTitle,hours,units,conditions,description,session;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.course_details);
Intent i = getIntent();
course = i.getStringExtra("cid");
courseTitle = i.getStringExtra("name");
session = i.getStringExtra("session");
hours = i.getStringExtra("hours");
conditions = i.getStringExtra("conditions");
description= i.getStringExtra("description");
units= i.getStringExtra("units");
Log.i("Inside coursedetails","coursedetails"+course+"|"+courseTitle+"|"+session+"|"+hours+"|"+conditions+"|"+description);
txtcourse = (TextView) findViewById(R.id.course);
txtcoursetitle = (TextView) findViewById(R.id.coursetitle);
txthours = (TextView) findViewById(R.id.hours);
txtconditions = (TextView) findViewById(R.id.conditions);
txtsession = (TextView) findViewById(R.id.session);
txtunits = (TextView) findViewById(R.id.units);
txtdescription = (TextView) findViewById(R.id.description);
txtcourse.setText(course);
txtcoursetitle.setText(courseTitle);
txthours.setText(hours);
txtconditions.setText(conditions);
txtsession.setText(session);
txtunits.setText(units);
txtdescription.setText(description);
}
}
xml布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/customshape"
android:padding="7px"
>
<TextView
android:id="@+id/tab2_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Courses"
android:textAlignment="inherit"
android:textColor="#FFF"
android:textSize="20dp"
android:textStyle="bold"
android:gravity="center" />
</RelativeLayout>
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
android:padding="5dp" >
<TextView
android:id="@+id/title1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#cac9c9"
android:padding="5dp"
android:text="Course"
android:textColor="#040404"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="@+id/course"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="5dp"
android:textColor="#040404"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans"
android:layout_weight="1"
android:layout_gravity="fill" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
android:padding="5dp" >
<TextView
android:id="@+id/title2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Course Title"
android:textColor="#040404"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans"
android:background="#cac9c9"
android:padding="5dp" />
<TextView
android:id="@+id/coursetitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="5dp"
android:textColor="#040404"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans"
android:layout_weight="1"
android:layout_gravity="fill" />
</TableRow>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
android:padding="5dp" >
<TextView
android:id="@+id/title1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Session"
android:textColor="#040404"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans"
android:background="#cac9c9"
android:padding="5dp" />
<TextView
android:id="@+id/session"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="5dp"
android:textColor="#040404"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans"
android:layout_weight="1"
android:layout_gravity="fill" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
android:padding="5dp" >
<TextView
android:id="@+id/title3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hours"
android:textColor="#040404"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans"
android:background="#cac9c9"
android:padding="5dp" />
<TextView
android:id="@+id/hours"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="5dp"
android:textColor="#040404"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans"
android:layout_weight="1"
android:layout_gravity="fill" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
android:padding="5dp" >
<TextView
android:id="@+id/title4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Units"
android:textColor="#040404"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans"
android:background="#cac9c9"
android:padding="5dp" />
<TextView
android:id="@+id/units"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="5dp"
android:textColor="#040404"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans"
android:layout_weight="1"
android:layout_gravity="fill" />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginTop="2dp"
android:padding="5dp" >
<TextView
android:id="@+id/title5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Conditions"
android:textColor="#040404"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans"
android:background="#cac9c9"
android:padding="5dp" />
<TextView
android:id="@+id/conditions"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="5dp"
android:textColor="#040404"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans"
android:layout_weight="1"
android:layout_gravity="fill" />
</TableRow>
</TableLayout>
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="fill"
android:layout_weight="1"
android:ellipsize="end"
android:singleLine="false"
android:textColor="#040404"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans"
android:padding="5dp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
以下是日志的屏幕截图
以下是详细视图的屏幕截图
正在运作的案例
答案 0 :(得分:1)
尝试这种方式是演示示例,你必须根据你的要求进行修改。
<强> 1。 activity_main.xml中强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:background="@android:color/darker_gray"
android:orientation="vertical" >
<TextView
android:id="@+id/tab2_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Courses"
android:textAlignment="inherit"
android:textColor="#FFF"
android:textSize="20dp"
android:textStyle="bold"
android:background="@android:color/black"
android:gravity="center" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center">
<TextView
android:id="@+id/title1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="#cac9c9"
android:padding="5dp"
android:text="Course"
android:textColor="#040404"
android:gravity="right"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="@+id/course"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:padding="5dp"
android:textColor="#040404"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center">
<TextView
android:id="@+id/title2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Course Title"
android:textColor="#040404"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans"
android:gravity="right"
android:background="#cac9c9"
android:padding="5dp" />
<TextView
android:id="@+id/coursetitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:padding="5dp"
android:textColor="#040404"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center">
<TextView
android:id="@+id/title1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Session"
android:textColor="#040404"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans"
android:gravity="right"
android:background="#cac9c9"
android:padding="5dp" />
<TextView
android:id="@+id/session"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:padding="5dp"
android:textColor="#040404"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center">
<TextView
android:id="@+id/title3"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Hours"
android:textColor="#040404"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans"
android:gravity="right"
android:background="#cac9c9"
android:padding="5dp" />
<TextView
android:id="@+id/hours"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:padding="5dp"
android:textColor="#040404"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center">
<TextView
android:id="@+id/title4"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Units"
android:textColor="#040404"
android:textSize="15dp"
android:textStyle="bold"
android:gravity="right"
android:typeface="sans"
android:background="#cac9c9"
android:padding="5dp" />
<TextView
android:id="@+id/units"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:padding="5dp"
android:textColor="#040404"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center">
<TextView
android:id="@+id/title5"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Conditions"
android:textColor="#040404"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans"
android:gravity="right"
android:background="#cac9c9"
android:padding="5dp" />
<TextView
android:id="@+id/conditions"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:padding="5dp"
android:textColor="#040404"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans"
android:layout_weight="1"/>
</LinearLayout>
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_weight="1">
<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#040404"
android:textSize="15dp"
android:textStyle="bold"
android:typeface="sans"
android:padding="5dp" />
</ScrollView>
</LinearLayout>
<强> 2。 MyActivity 强>
public class MyActivity extends Activity {
TextView txtcourse;
TextView txtcoursetitle;
TextView txthours;
TextView txtsession;
TextView txtunits;
TextView txtconditions;
TextView txtdescription;
String course,courseTitle,hours,units,conditions,description,session;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = getIntent();
course = i.getStringExtra("cid")==null?"Demo Course":i.getStringExtra("cid");
courseTitle = i.getStringExtra("name")==null?"Demo Name":i.getStringExtra("name");
session = i.getStringExtra("session")==null?"Demo Session":i.getStringExtra("session");
hours = i.getStringExtra("hours")==null?"Demo Hours":i.getStringExtra("hours");
conditions = i.getStringExtra("conditions")==null?"Demo Conditions":i.getStringExtra("conditions");
description= i.getStringExtra("description")==null?"Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions Demo Descriptions":i.getStringExtra("description");
units= i.getStringExtra("units")==null?"Demo Units":i.getStringExtra("units");
Log.i("Inside coursedetails", "coursedetails" + course + "|" + courseTitle + "|" + session + "|" + hours + "|" + conditions + "|" + description);
txtcourse = (TextView) findViewById(R.id.course);
txtcoursetitle = (TextView) findViewById(R.id.coursetitle);
txthours = (TextView) findViewById(R.id.hours);
txtconditions = (TextView) findViewById(R.id.conditions);
txtsession = (TextView) findViewById(R.id.session);
txtunits = (TextView) findViewById(R.id.units);
txtdescription = (TextView) findViewById(R.id.description);
txtcourse.setText(course);
txtcoursetitle.setText(courseTitle);
txthours.setText(hours);
txtconditions.setText(conditions);
txtsession.setText(session);
txtunits.setText(units);
txtdescription.setText(description);
}
}