![](/img/trans.png)
[英]My app keeps crashing when I Open the Game Activity. I tried to create a game activity to be opened from the main activity
[英]My app is force closing when clicking on button to switch to another activity. here i am putting my code
我正在嘗試通過替換注冊號、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);
}
});
}
}
結果活動.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();
}
}
}
這是問題所在:
...
startActivity(i);
i.putExtra("Regno",Regno);
i.putExtra("Rollno",Rollno);
i.putExtra("Dob",Dob);
一旦你開始活動就晚了放額外的東西......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.