• Android开发之一个未解决的bug


    使用Activity之间传递数据的时候,出现了一个bug,但是没有找到哪里出错了。

    把代码和log都记录下来,以后研究

    代码:

    MainActivity.class

     1 package com.example.caradviser;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 import android.app.Activity;
     7 import android.content.Intent;
     8 import android.net.Uri;
     9 import android.os.Bundle;
    10 import android.util.Log;
    11 import android.view.View;
    12 import android.view.Window;
    13 import android.widget.Spinner;
    14 import android.widget.TextView;
    15 import android.widget.Toast;
    16 
    17 public class MainActivity extends Activity {
    18 
    19     private static final String TAG = "com.example.caradviser";
    20     TextView showBackText;
    21 
    22     @Override
    23     protected void onCreate(Bundle savedInstanceState) {
    24         super.onCreate(savedInstanceState);
    25         requestWindowFeature(Window.FEATURE_NO_TITLE);
    26         setContentView(R.layout.activity_main);
    27         showBackText=(TextView) findViewById(R.id.backMainActivity);
    28     }
    29     
    30     @Override
    31     protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    32         super.onActivityResult(requestCode, resultCode, intent);
    33 
    34         switch (requestCode) {
    35         case 123123:
    36             if (resultCode==RESULT_OK) {
    37                 if (intent==null||"".equals(intent)) {
    38                     Toast.makeText(MainActivity.this, "获取到的数据为空", Toast.LENGTH_SHORT).show();
    39                 }else {
    40                     String getData = intent.getStringExtra("data_return");
    41                     showBackText.setText(intent.getStringExtra("data_return"));
    42                 }
    43             }
    44             break;
    45 
    46         default:
    47             break;
    48         }
    49     }
    50 
    51     
    52     public void toAnotherActivity3(View v){
    53         String data ="Hello,这是传递的数据";
    54         Intent intent = new Intent(MainActivity.this,ThreeActivity.class);
    55         intent.putExtra("haha", data);
    56         startActivityForResult(intent, 123123);
    57     }
    58 }

    ThreeActivity.class

     1 package com.example.caradviser;
     2 
     3 import android.app.Activity;
     4 import android.content.Intent;
     5 import android.os.Bundle;
     6 import android.text.TextUtils;
     7 import android.view.View;
     8 import android.view.View.OnClickListener;
     9 import android.view.Window;
    10 import android.widget.Button;
    11 import android.widget.TextView;
    12 import android.widget.Toast;
    13 
    14 public class ThreeActivity extends Activity {
    15     
    16     private Button backToMainActivity;
    17     private String data;
    18     private TextView textView;
    19 
    20     @Override
    21     protected void onCreate(Bundle savedInstanceState) {
    22         // TODO Auto-generated method stub
    23         super.onCreate(savedInstanceState);
    24         requestWindowFeature(Window.FEATURE_NO_TITLE);
    25         setContentView(R.layout.three_layout);
    26         
    27         Intent intent = getIntent();
    28         data = intent.getStringExtra("haha");
    29         
    30         textView = (TextView) findViewById(R.id.showText);
    31             
    32         if (TextUtils.isEmpty(data)) {
    33             Toast.makeText(ThreeActivity.this, "没有接收到其他activity的数据", Toast.LENGTH_SHORT).show();
    34         }else {
    35             textView.setText(data);
    36         }
    37         
    38         
    39         backToMainActivity = (Button) findViewById(R.id.backMainActivity);
    40         backToMainActivity.setOnClickListener(new OnClickListener() {
    41             
    42             @Override
    43             public void onClick(View v) {
    44                 // TODO Auto-generated method stub
    45                 Intent intent = new Intent();
    46                 String data3= "将要返回到MainActivity";
    47                 intent.putExtra("data_return", data3);
    48                 setResult(RESULT_OK,intent);
    49                 finish();
    50             }
    51         });
    52     }
    53 
    54 }

    Log

     1 10-12 16:22:02.304: E/AndroidRuntime(16265): FATAL EXCEPTION: main
     2 10-12 16:22:02.304: E/AndroidRuntime(16265): Process: com.example.caradviser, PID: 16265
     3 10-12 16:22:02.304: E/AndroidRuntime(16265): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=123123, result=-1, data=Intent { (has extras) }} to activity {com.example.caradviser/com.example.caradviser.MainActivity}: java.lang.NullPointerException
     4 10-12 16:22:02.304: E/AndroidRuntime(16265):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3375)
     5 10-12 16:22:02.304: E/AndroidRuntime(16265):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3418)
     6 10-12 16:22:02.304: E/AndroidRuntime(16265):     at android.app.ActivityThread.access$1300(ActivityThread.java:141)
     7 10-12 16:22:02.304: E/AndroidRuntime(16265):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
     8 10-12 16:22:02.304: E/AndroidRuntime(16265):     at android.os.Handler.dispatchMessage(Handler.java:102)
     9 10-12 16:22:02.304: E/AndroidRuntime(16265):     at android.os.Looper.loop(Looper.java:136)
    10 10-12 16:22:02.304: E/AndroidRuntime(16265):     at android.app.ActivityThread.main(ActivityThread.java:5072)
    11 10-12 16:22:02.304: E/AndroidRuntime(16265):     at java.lang.reflect.Method.invokeNative(Native Method)
    12 10-12 16:22:02.304: E/AndroidRuntime(16265):     at java.lang.reflect.Method.invoke(Method.java:515)
    13 10-12 16:22:02.304: E/AndroidRuntime(16265):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    14 10-12 16:22:02.304: E/AndroidRuntime(16265):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
    15 10-12 16:22:02.304: E/AndroidRuntime(16265):     at dalvik.system.NativeStart.main(Native Method)
    16 10-12 16:22:02.304: E/AndroidRuntime(16265): Caused by: java.lang.NullPointerException
    17 10-12 16:22:02.304: E/AndroidRuntime(16265):     at com.example.caradviser.MainActivity.onActivityResult(MainActivity.java:42)
    18 10-12 16:22:02.304: E/AndroidRuntime(16265):     at android.app.Activity.dispatchActivityResult(Activity.java:5441)
    19 10-12 16:22:02.304: E/AndroidRuntime(16265):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3371)
    20 10-12 16:22:02.304: E/AndroidRuntime(16265):     ... 11 more

    具体什么原因造成的,目前我还无法确定,等待能力提高以后,回头看看这个问题。

  • 相关阅读:
    中国MOOC分数——Java
    axios的封装和使用
    ModelViewSet里的过滤、排序、分页、序列化设置
    Django和DjangoDRF的区别
    RESTFUL风格
    MVT和MVC的个人理解
    HTTP请求简单理解
    虚拟机创建Django项目并创建APP
    python列表元素添加的几种方法以及差异总结
    python时间格式
  • 原文地址:https://www.cnblogs.com/liyiran/p/4872041.html
Copyright © 2020-2023  润新知