老师,为什么我不能传递数字到otherActivity呢?
package com.example.ui_fragment; import android.content.DialogInterface; import android.content.Intent; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; public static final String KEY = "KEY"; private Button button,button2,button3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.i(TAG, "onCreate: "); initView(); initLintener(); } private void initView(){ button = (Button)findViewById(R.id.button); button2 = (Button)findViewById(R.id.button2); button3 = (Button)findViewById(R.id.button3); } private void initLintener(){ ButtonLinstener buttonLinstener = new ButtonLinstener(); button.setOnClickListener(buttonLinstener); button2.setOnClickListener(buttonLinstener); button3.setOnClickListener(buttonLinstener); } private class ButtonLinstener implements View.OnClickListener{ @Override public void onClick(View view) { switch (view.getId()){ case R.id.button: openActivity(); break; case R.id.button2: openDialogActivity(); break; case R.id.button3: openDialog(); break; } } } private void openActivity(){ Intent intent =new Intent(); intent.setClass(MainActivity.this,OtherActivity.class); //intent.putExtra(KEY, getString(R.string.values)); intent.putExtra("key",123); startActivity(intent); } private void openDialogActivity(){ //final Intent intent = new Intent(MainActivity.this, DialogActivity.class); //startActivity(intent); DialogActivity myDialog=new DialogActivity(MainActivity.this); myDialog.show(); } private void openDialog(){ AlertDialog.Builder builder=new AlertDialog.Builder(this); builder.setTitle("您喜欢甜品吗?"); builder.setMessage("如果喜欢请点击确定"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Toast.makeText(MainActivity.this,"喜欢甜品",Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消",null); builder.show(); } @Override protected void onStart() { super.onStart(); Log.i(TAG, "onStart: "); } @Override protected void onResume() { super.onResume(); Log.i(TAG, "onResume: "); } @Override protected void onPause() { super.onPause(); Log.i(TAG, "onPause: "); } @Override protected void onStop() { super.onStop(); Log.i(TAG, "onStop: "); } @Override protected void onDestroy() { super.onDestroy(); Log.i(TAG, "onDestroy: "); } @Override protected void onRestart() { super.onRestart(); Log.i(TAG, "onRestart: "); } }
package com.example.ui_fragment;
import android.app.Activity;
import android.app.IntentService;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.TextView;
public class OtherActivity extends Activity{
private static final String TAG = "OtherActivity";
private TextView textView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
Log.i(TAG, "onCreate: ");
//接收数据并显示出来;
if(getIntent()!=null){
Intent intent = getIntent();
int Values = intent.getIntExtra("key",0);
textView = (TextView)findViewById(R.id.textView);
textView.setText(Values);
}
// //发送短信
// Uri uri = Uri.parse("smsto://0800000123");
// Intent intent1 = new Intent(Intent.ACTION_SENDTO,uri);
// intent1.putExtra("sms_body","The SMS TEXT");
// startActivity(intent1);
}
@Override
protected void onStart() {
super.onStart();
Log.i(TAG, "onStart: ");
}
@Override
protected void onResume() {
super.onResume();
Log.i(TAG, "onResume: ");
}
@Override
protected void onPause() {
super.onPause();
Log.i(TAG, "onPause: ");
}
@Override
protected void onStop() {
super.onStop();
Log.i(TAG, "onStop: ");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i(TAG, "onDestroy: ");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i(TAG, "onRestart: ");
}
}
06-28 15:58:54.995 1615-1894/? I/ActivityManager: START u0 {cmp=com.example.ui_fragment/.OtherActivity (has extras)} from uid 10080 on display 0
06-28 15:58:55.016 32011-32011/com.example.ui_fragment I/MainActivity: onPause:
06-28 15:58:55.050 32011-32011/com.example.ui_fragment I/OtherActivity: onCreate:
06-28 15:58:55.052 32011-32011/com.example.ui_fragment E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ui_fragment, PID: 32011
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ui_fragment/com.example.ui_fragment.OtherActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x7b
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x7b
at android.content.res.Resources.getText(Resources.java:331)
at android.widget.TextView.setText(TextView.java:4554)
at com.example.ui_fragment.OtherActivity.onCreate(OtherActivity.java:27)
at android.app.Activity.performCreate(Activity.java:6662)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
06-28 15:58:55.058 1615-6738/? W/ActivityManager: Force finishing activity com.example.ui_fragment/.OtherActivity
06-28 15:58:55.069 1615-6738/? W/ActivityManager: Force finishing activity com.example.ui_fragment/.MainActivity
06-28 15:58:55.572 1615-1628/? W/ActivityManager: Activity pause timeout for ActivityRecord{6e26d17 u0 com.example.ui_fragment/.OtherActivity t143 f}
06-28 15:59:06.161 1615-1628/? W/ActivityManager: Activity destroy timeout for ActivityRecord{6e26d17 u0 com.example.ui_fragment/.OtherActivity t143 f}
Activity destroy timeout for ActivityRecord{2c3a62f u0 com.example.ui_fragment/.MainActivity t143 f}
06-28 15:59:06.162 1615-1628/? I/WindowManager: Destroying surface Surface(name=com.example.ui_fragment/com.example.ui_fragment.MainActivity) called by com.android.server.wm.WindowStateAnimator.destroySurface:2016 com.android.server.wm.WindowStateAnimator.destroySurfaceLocked:882 com.android.server.wm.WindowState.removeLocked:1456 com.android.server.wm.WindowManagerService.removeWindowInnerLocked:2484 com.android.server.wm.WindowManagerService.removeWindowLocked:2442 com.android.server.wm.WindowManagerService.removeWindowLocked:2311 com.android.server.wm.AppWindowToken.removeAllWindows:530 com.android.server.wm.AppWindowToken.removeAppFromTaskLocked:326
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 5461 份
- 解答问题 7235 个
此次推出的专题为Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始学习Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星