我的应用程序是在单击按钮切换到另一个活动时强制关闭。我在这里放我的代码

时间:2015-11-26 16:06:04

标签: java android

我正在尝试通过替换注册号,DOB,用户滚动来从URL解析json。但是当我点击提交按钮切换到结果活动时。它停止工作 MainActivity.java

 public class MainActivity extends AppCompatActivity {
 private String Regno;
 private String Rollno;
 private String Dob;
 private EditText regno;
 private EditText rollno;
 private EditText dob;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    regno=(EditText)findViewById(R.id.Registrationid);
    rollno=(EditText)findViewById(R.id.Rollno);
    dob=(EditText)findViewById(R.id.Dob);

   Button submit =(Button)findViewById(R.id.Submit);
    submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Regno=regno.getText().toString();
            Rollno=rollno.getText().toString();
            Dob=dob.getText().toString();
            Intent i= new Intent(MainActivity.this, Resultview.class);
            startActivity(i);
            i.putExtra("Regno",Regno);
            i.putExtra("Rollno",Rollno);
            i.putExtra("Dob",Dob);
        }
    });
  }
}

ResultActivity.java

public class Resultview extends AppCompatActivity {

Intent intent= getIntent();
String Regno = intent.getExtras().getString("Regno");
String Rollno = intent.getExtras().getString("Rollno");
String Dob = intent.getExtras().getString("Dob");
private final String url ="http://example.ubs.in/utpn/api/mobileroll/enquiry/"+Rollno+"/"+Regno+"/"+Dob+"/mobileapi";
private static final String TAG_NAME="name";
private static final String TAG_ROLL="roll";
private static final String TAG_REGNO="regno";
JSONArray result=null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_resultview);
                    ServiceHandler sh = new ServiceHandler();
                   JSONObject json = sh.getJSONFromUrl(url);

                        try {
                            result=json.getJSONArray(TAG_NAME);

                            for (int i = 0; i < result.length(); i++) {
                                JSONObject r = result.getJSONObject(i);

                                String name = r.getString(TAG_NAME);
                                TextView nameset=(TextView)findViewById(R.id.nameset);
                                nameset.setText( name);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
}

1 个答案:

答案 0 :(得分:1)

问题在于:

...
startActivity(i);
i.putExtra("Regno",Regno);
i.putExtra("Rollno",Rollno);
i.putExtra("Dob",Dob);

一旦你开始这项活动就迟到了......