Java Android Studio TextView.text不更新
我稍微修改了我的第一个应用程序。我把它变成了一个方便、简单的应用程序,可以计算行驶里程和燃油消耗量,按下按钮后,开始新的活动,以升/100公里为单位显示燃油消耗量。Java Android Studio TextView.text不更新,java,android,android-activity,textview,Java,Android,Android Activity,Textview,我稍微修改了我的第一个应用程序。我把它变成了一个方便、简单的应用程序,可以计算行驶里程和燃油消耗量,按下按钮后,开始新的活动,以升/100公里为单位显示燃油消耗量。 我面临的问题是,无论在单击按钮之前输入什么值,输出总是显示62,14 l/100km。我猜这是第一次计算的结果,没有更新 strings.xml: <resources> <string name="app_name">FuelEcoCalc</string> <string
我面临的问题是,无论在单击按钮之前输入什么值,输出总是显示62,14 l/100km。我猜这是第一次计算的结果,没有更新 strings.xml:
<resources>
<string name="app_name">FuelEcoCalc</string>
<string name="action_settings">Settings</string>
<string name="edit_miles">Distance in miles</string>
<string name="edit_litres">Fuel qty in litres</string>
<string name="button_calculate">Calculate</string>
<string name="title_activity_main">MainActivity</string>
<string name="title_activity_display_message">My Message</string>
<string name="hello_world">Hello World</string>
</resources>
DisplayMessageActivity.java:
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class MyActivity extends ActionBarActivity {
public final static String MILES_MESSAGE = "com.mycompany.myfirstapp.MESSAGE";
public final static String FUEL_MESSAGE = "com.mycompany.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_my, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editMiles = (EditText) findViewById(R.id.edit_miles);
String miles = editMiles.getText().toString();
EditText editFuel = (EditText) findViewById(R.id.edit_litres);
String fuel = editFuel.getText().toString();
intent.removeExtra(MILES_MESSAGE);
intent.removeExtra(FUEL_MESSAGE);
intent.putExtra(MILES_MESSAGE, miles);
intent.putExtra(FUEL_MESSAGE, fuel);
startActivity(intent);
}
}
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;
import java.text.DecimalFormat;
public class DisplayMessageActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String miles = intent.getStringExtra(MyActivity.MILES_MESSAGE);
String fuel = intent.getStringExtra(MyActivity.FUEL_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
String message = "";
try {
double milesTravelled = Double.parseDouble(miles);
double fuelTaken = Double.parseDouble(fuel);
double mileToKm = 1.609344;
double kmTravelled = milesTravelled * mileToKm;
double fuelUsagePer100Km = (fuelTaken * 100) / kmTravelled;
DecimalFormat df = new DecimalFormat("#.##");
String output = df.format(fuelUsagePer100Km).toString();
message = output + " litres/100km";
} catch (Exception e) {
message = "Invalid input";
}
textView.setText(message);
setContentView(textView);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wojtek.myfirstapp" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MyActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.wojtek.myfirstapp.MyActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.wojtek.myfirstapp.MyActivity" />
</activity>
</application>
</manifest>
可能是因为在DisplayMessageActivity.java中,您没有在布局中获得对TextView的引用
TextView textView = (TextView)findViewById(R.id.textView);
您还需要在布局文件中为TextView提供一个id
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.wojtek.myfirstapp.DisplayMessageActivity">
<TextView android:id="@+id/textView" android:text="@string/title_activity_display_message" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
希望它现在能为您工作。试着调试一下,您是否得到了不同的字符串英里数和字符串燃料值?每次转到“显示活动”时。。我想你应该按id使用textview,我试过了。应用程序在单击按钮后停止。您是否在“活动”\u display\u message.xml中为textview提供了id?工作起来很有魅力!:)非常感谢。顺便说一下。。。我不必找维比伊德,这不是很奇怪吗?很高兴这对你有用。而且,您没有使用在布局文件中定义的TextView。事实上,即使你从布局文件中删除TextView,你的应用程序也能正常工作。您在这里所做的是使用setContentView(TextView)以编程方式添加一个新的TextView。最好的做法是在xml布局文件中定义TextView,然后使用findViewById()在代码中使用它。清理项目并从设备中删除apk,然后随意重新安装更新这可能是在您有足够的代表发表评论之前发布的,但这不是答案。我建议删除它,以避免投反对票。
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;
import java.text.DecimalFormat;
public class DisplayMessageActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String miles = intent.getStringExtra(MyActivity.MILES_MESSAGE);
String fuel = intent.getStringExtra(MyActivity.FUEL_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
String message = "";
try {
double milesTravelled = Double.parseDouble(miles);
double fuelTaken = Double.parseDouble(fuel);
double mileToKm = 1.609344;
double kmTravelled = milesTravelled * mileToKm;
double fuelUsagePer100Km = (fuelTaken * 100) / kmTravelled;
DecimalFormat df = new DecimalFormat("#.##");
String output = df.format(fuelUsagePer100Km).toString();
message = output + " litres/100km";
} catch (Exception e) {
message = "Invalid input";
}
textView.setText(message);
setContentView(textView);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wojtek.myfirstapp" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MyActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.wojtek.myfirstapp.MyActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.wojtek.myfirstapp.MyActivity" />
</activity>
</application>
</manifest>
05-09 05:54:12.917 2106-2141/com.example.wojtek.myfirstapp W/EGL_emulation﹕ eglSurfaceAttrib not implemented
05-09 05:54:12.917 2106-2141/com.example.wojtek.myfirstapp W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa64ae220, error=EGL_SUCCESS
TextView textView = (TextView)findViewById(R.id.textView);
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.wojtek.myfirstapp.DisplayMessageActivity">
<TextView android:id="@+id/textView" android:text="@string/title_activity_display_message" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
public final static String MILES_MESSAGE = "com.mycompany.myfirstapp.MESSAGEMILES";
public final static String FUEL_MESSAGE = "com.mycompany.myfirstapp.MESSAGEFUEL";