我的Android应用中存在以下问题:
setText不会更改textviews的文本
某些字符串和双打必须通过意图发送,并且必须显示在textviews
中我发送额外消息的代码部分:
private void showInfo(PointOfInterest poi){
Intent intent = new Intent(this, ShowInfo.class);
intent.putExtra("name", poi.getName().toString());
intent.putExtra("city", poi.getCity().toString());
intent.putExtra("country", poi.getCountry().toString());
intent.putExtra("date", poi.getDate().toString());
intent.putExtra("comment", poi.getComment().toString());
intent.putExtra("cat", poi.getCategorie().toString());
intent.putExtra("lng", poi.getLng());
intent.putExtra("lat", poi.getLat());
startActivity(intent);
}
我尝试显示它们:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
setContentView(R.layout.fragment_show_info);
namePoi = intent.getStringExtra("name");
TextView name = (TextView) findViewById(R.id.showNameInfo);
TextView city = (TextView) findViewById(R.id.showCityInfo);
TextView country = (TextView) findViewById(R.id.showCountryInfo);
TextView date = (TextView) findViewById(R.id.showDateInfo);
TextView comment = (TextView) findViewById(R.id.showCommentInfo);
TextView cat = (TextView) findViewById(R.id.showCatInfo);
lat = intent.getDoubleExtra("lat", 0);
lng = intent.getDoubleExtra("lng", 0);
TextView latView = (TextView) findViewById(R.id.showLatInfo);
TextView lngView = (TextView) findViewById(R.id.showLngInfo);
Log.d("debug", intent.getStringExtra("cat"));
CharSequence tmp = intent.getStringExtra("name");
name.setText(tmp);
city.setText(intent.getStringExtra("city"));
country.setText(intent.getStringExtra("country"));
date.setText(intent.getStringExtra("date"));
comment.setText(intent.getStringExtra("comment"));
name.setText(intent.getStringExtra("name"));
cat.setText(intent.getStringExtra("cat"));
lngView.setText(String.valueOf(lng));
latView.setText(String.valueOf(lat));
setContentView(R.layout.activity_show_info);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
xml片段:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.pointsofinterest.ShowInfo$PlaceholderFragment" >
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:" />
<TextView
android:id="@+id/showNameInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" " />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date:" />
<TextView
android:id="@+id/showDateInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" " />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Country:" />
<TextView
android:id="@+id/showCountryInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" " />
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="City:" />
<TextView
android:id="@+id/showCityInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" " />
</TableRow>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Latitude:" />
<TextView
android:id="@+id/showLatInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" " />
</TableRow>
<TableRow
android:id="@+id/tableRow8"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Longitude:" />
<TextView
android:id="@+id/showLngInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" " />
</TableRow>
<TableRow
android:id="@+id/tableRow7"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Categorie:" />
<TextView
android:id="@+id/showCatInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" " />
</TableRow>
<TableRow
android:id="@+id/tableRow9"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Comment:" />
<TextView
android:id="@+id/showCommentInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" " />
</TableRow>
<TableRow
android:id="@+id/tableRow6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="54dp"
android:text="Show on Map"
android:onClick="showOnMap"/>
</TableRow>
</TableLayout>
logcat中没有错误,我只是看到我的活动有空文本视图
答案 0 :(得分:1)
您的onCreate()函数错误。
....
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
setContentView(R.layout.activity_show_info);
PlaceholderFragment fragment = new PlaceholderFragment();
fragment.setArguments(intent.getExtras());
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, fragment).commit();
}
}
PlaceHolderFragment:
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_show_info, container, false);
namePoi = intent.getStringExtra("name");
name = (TextView) view.findViewById(R.id.showNameInfo);
city = (TextView) view.findViewById(R.id.showCityInfo);
country = (TextView) view.findViewById(R.id.showCountryInfo);
date = (TextView) view.findViewById(R.id.showDateInfo);
comment = (TextView) view.findViewById(R.id.showCommentInfo);
cat = (TextView) view.findViewById(R.id.showCatInfo);
lat = getArguments().getDouble("lat", 0);
lng = getArguments().getDouble("lng", 0);
latView = (TextView) view.findViewById(R.id.showLatInfo);
lngView = (TextView) view.findViewById(R.id.showLngInfo);
Log.d("debug", getArguments().getString("cat"));
CharSequence tmp = getArguments().getString("name");
name.setText(tmp);
city.setText(getArguments().getString("city"));
country.setText(getArguments().getString("country"));
date.setText(getArguments().getString("date"));
comment.setText(getArguments().getString("comment"));
name.setText(getArguments().getString("name"));
cat.setText(getArguments().getString("cat"));
lngView.setText(String.valueOf(lng));
latView.setText(String.valueOf(lat));
return view;
}