主activity:
package com.example.textlayout;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private EditText factorone,factortwo;
private TextView symbol;
private Button calculate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
factorone=(EditText)findViewById(R.id.factorone);
factortwo=(EditText)findViewById(R.id.factortwo);
symbol=(TextView)findViewById(R.id.symbol);
calculate=(Button)findViewById(R.id.calculate);
symbol.setText(R.string.symbol);
calculate.setText(R.string.calculate);
calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String factoroneString=factorone.getText().toString();//取两个EditText的值
String factortwoString=factortwo.getText().toString();
Intent intent=new Intent();
intent.putExtra(“one”, factoroneString);//将两个值存放 到intent对象中
intent.putExtra(“two”, factortwoString);
intent.setClass(MainActivity.this, result_activity.class);
MainActivity.this.startActivity(intent);
}
});
}
第二个activity:
package com.example.textlayout;
import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class result_activity extends Activity{
private TextView resultview;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
resultview=(TextView)findViewById(R.id.result);
Intent intent=getIntent();//得到intent对象中的值
String factoroneString=intent.getStringExtra(“one”);
String factortwoString=intent.getStringExtra(“two”);
int factoroneInt=Integer.parseInt(factoroneString);
int factortwoInt=Integer.parseInt(factortwoString);
int result=factoroneInt*factortwoInt;
resultview.setText(result);
}
}
配置文件:
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.textlayout”
android:versionCode=”1″
android:versionName=”1.0″ >
<uses-sdk
android:minSdkVersion=”14″
android:targetSdkVersion=”19″ />
<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”.MainActivity”
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=”.result_activity”
android:label=”@string/app_name”/>
</application>
</manifest>
logcat中的错误:
11-07 13:21:28.833: E/AndroidRuntime(2570): FATAL EXCEPTION: main
11-07 13:21:28.833: E/AndroidRuntime(2570): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.textlayout/com.example.textlayout.result_activity}: android.content.res.Resources$NotFoundException: String resource ID #0x18
11-07 13:21:28.833: E/AndroidRuntime(2570): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-07 13:21:28.833: E/AndroidRuntime(2570): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-07 13:21:28.833: E/AndroidRuntime(2570): at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-07 13:21:28.833: E/AndroidRuntime(2570): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-07 13:21:28.833: E/AndroidRuntime(2570): at android.os.Handler.dispatchMessage(Handler.java:99)
11-07 13:21:28.833: E/AndroidRuntime(2570): at android.os.Looper.loop(Looper.java:137)
11-07 13:21:28.833: E/AndroidRuntime(2570): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-07 13:21:28.833: E/AndroidRuntime(2570): at java.lang.reflect.Method.invokeNative(Native Method)
11-07 13:21:28.833: E/AndroidRuntime(2570): at java.lang.reflect.Method.invoke(Method.java:511)
11-07 13:21:28.833: E/AndroidRuntime(2570): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-07 13:21:28.833: E/AndroidRuntime(2570): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-07 13:21:28.833: E/AndroidRuntime(2570): at dalvik.system.NativeStart.main(Native Method)
11-07 13:21:28.833: E/AndroidRuntime(2570): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x18
11-07 13:21:28.833: E/AndroidRuntime(2570): at android.content.res.Resources.getText(Resources.java:229)
11-07 13:21:28.833: E/AndroidRuntime(2570): at android.widget.TextView.setText(TextView.java:3620)
11-07 13:21:28.833: E/AndroidRuntime(2570): at com.example.textlayout.result_activity.onCreate(result_activity.java:24)
11-07 13:21:28.833: E/AndroidRuntime(2570): at android.app.Activity.performCreate(Activity.java:5008)
11-07 13:21:28.833: E/AndroidRuntime(2570): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-07 13:21:28.833: E/AndroidRuntime(2570): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-07 13:21:28.833: E/AndroidRuntime(2570): … 11 more
package com.example.textlayout;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private EditText factorone,factortwo;
private TextView symbol;
private Button calculate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
factorone=(EditText)findViewById(R.id.factorone);
factortwo=(EditText)findViewById(R.id.factortwo);
symbol=(TextView)findViewById(R.id.symbol);
calculate=(Button)findViewById(R.id.calculate);
symbol.setText(R.string.symbol);
calculate.setText(R.string.calculate);
calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String factoroneString=factorone.getText().toString();//取两个EditText的值
String factortwoString=factortwo.getText().toString();
Intent intent=new Intent();
intent.putExtra(“one”, factoroneString);//将两个值存放 到intent对象中
intent.putExtra(“two”, factortwoString);
intent.setClass(MainActivity.this, result_activity.class);
MainActivity.this.startActivity(intent);
}
});
}
第二个activity:
package com.example.textlayout;
import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class result_activity extends Activity{
private TextView resultview;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
resultview=(TextView)findViewById(R.id.result);
Intent intent=getIntent();//得到intent对象中的值
String factoroneString=intent.getStringExtra(“one”);
String factortwoString=intent.getStringExtra(“two”);
int factoroneInt=Integer.parseInt(factoroneString);
int factortwoInt=Integer.parseInt(factortwoString);
int result=factoroneInt*factortwoInt;
resultview.setText(result);
}
}
配置文件:
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.textlayout”
android:versionCode=”1″
android:versionName=”1.0″ >
<uses-sdk
android:minSdkVersion=”14″
android:targetSdkVersion=”19″ />
<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”.MainActivity”
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=”.result_activity”
android:label=”@string/app_name”/>
</application>
</manifest>
logcat中的错误:
11-07 13:21:28.833: E/AndroidRuntime(2570): FATAL EXCEPTION: main
11-07 13:21:28.833: E/AndroidRuntime(2570): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.textlayout/com.example.textlayout.result_activity}: android.content.res.Resources$NotFoundException: String resource ID #0x18
11-07 13:21:28.833: E/AndroidRuntime(2570): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-07 13:21:28.833: E/AndroidRuntime(2570): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
11-07 13:21:28.833: E/AndroidRuntime(2570): at android.app.ActivityThread.access$600(ActivityThread.java:130)
11-07 13:21:28.833: E/AndroidRuntime(2570): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
11-07 13:21:28.833: E/AndroidRuntime(2570): at android.os.Handler.dispatchMessage(Handler.java:99)
11-07 13:21:28.833: E/AndroidRuntime(2570): at android.os.Looper.loop(Looper.java:137)
11-07 13:21:28.833: E/AndroidRuntime(2570): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-07 13:21:28.833: E/AndroidRuntime(2570): at java.lang.reflect.Method.invokeNative(Native Method)
11-07 13:21:28.833: E/AndroidRuntime(2570): at java.lang.reflect.Method.invoke(Method.java:511)
11-07 13:21:28.833: E/AndroidRuntime(2570): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-07 13:21:28.833: E/AndroidRuntime(2570): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-07 13:21:28.833: E/AndroidRuntime(2570): at dalvik.system.NativeStart.main(Native Method)
11-07 13:21:28.833: E/AndroidRuntime(2570): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x18
11-07 13:21:28.833: E/AndroidRuntime(2570): at android.content.res.Resources.getText(Resources.java:229)
11-07 13:21:28.833: E/AndroidRuntime(2570): at android.widget.TextView.setText(TextView.java:3620)
11-07 13:21:28.833: E/AndroidRuntime(2570): at com.example.textlayout.result_activity.onCreate(result_activity.java:24)
11-07 13:21:28.833: E/AndroidRuntime(2570): at android.app.Activity.performCreate(Activity.java:5008)
11-07 13:21:28.833: E/AndroidRuntime(2570): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-07 13:21:28.833: E/AndroidRuntime(2570): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
11-07 13:21:28.833: E/AndroidRuntime(2570): … 11 more
解决方案:20分
int result=factoroneInt*factortwoInt;
resultview.setText(result);
这是个Int,你把他换成String再赋值,改成这样
resultview.setText(String.valurOf(result));
resultview.setText(result);
这是个Int,你把他换成String再赋值,改成这样
resultview.setText(String.valurOf(result));