我创建了将数据发送到数据库的注册活动,按钮必须打开另一个活动,即MainActivity.代码工作正常,但有一个问题.单击该按钮时,它会响应并将信息发送到数据库,但活动不会更改.
我怎样才能更改代码以打开另一个活动?
RegisterRequest.java
public class RegisterRequest extends StringRequest {private static final String REGISTER_REQUEST_URL = "http://loginacti.comli.com/Register.PHP";private Map<String, String> params;public RegisterRequest(String Expect, String Prize, String name, String Email, Response.Listener<String> Listener) { super(Method.POST, REGISTER_REQUEST_URL, Listener, null); params = new HashMap<>(); params.put("Expect", Expect); params.put("Prize", Prize); params.put("name", name); params.put("Email", Email);}@OverrIDepublic Map<String, String> getParams() { return params;}
Registeractivity.java
public class Registeractivity extends AppCompatActivity {@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_register); final EditText etUkNown = (EditText) findVIEwByID(R.ID.etUkNown); final EditText etPrize = (EditText) findVIEwByID(R.ID.etPrize); final EditText etname = (EditText) findVIEwByID(R.ID.etname); final EditText etEmail = (EditText) findVIEwByID(R.ID.etEmail); final button bRegister = (button) findVIEwByID(R.ID.bRegister); bRegister.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { final String Expect = etUkNown.getText().toString(); final String Prize = etPrize.getText().toString(); final String name = etname.getText().toString(); final String Email = etEmail.getText().toString(); Response.Listener<String> responseListener = new Response.Listener<String>(){ @OverrIDe public voID onResponse(String response) { try { JsONObject JsonResponse = new JsONObject(response); boolean success = JsonResponse.getBoolean("success"); if (success){ Intent intent = new Intent(Registeractivity.this, MainActivity.class); Registeractivity.this.startActivity(intent); }else{ AlertDialog.Builder builder = new AlertDialog.Builder(Registeractivity.this); builder.setMessage("Register Failed") .setNegativebutton("Retry", null) .create() .show(); } } catch (JsONException e) { e.printstacktrace(); } } }; RegisterRequest registerRequest = new RegisterRequest(Expect, Prize, name, Email, responseListener); RequestQueue queue = Volley.newRequestQueue(Registeractivity.this); queue.add(registerRequest); } });}
MainActivity.java
public class MainActivity extends AppCompatActivity {@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); final TextVIEw registerlink = (TextVIEw) findVIEwByID(R.ID.eRegister); registerlink.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { Intent registerIntent = new Intent(MainActivity.this, Registeractivity.class); MainActivity.this.startActivity(registerIntent); } });}
表现
<uses-permission androID:name="androID.permission.INTERNET"></uses-permission><application androID:allowBackup="true" androID:icon="@mipmap/ic_launcher" androID:label="@string/app_name" androID:supportsRtl="true" androID:theme="@style/Apptheme"> <activity androID:name=".MainActivity"> <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity androID:name=".Registeractivity"></activity></application>
activity_register.xml
<EditText androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:ID="@+ID/etUkNown" androID:layout_alignParenttop="true" androID:layout_alignParentleft="true" androID:layout_alignParentStart="true" androID:layout_margintop="60dp" androID:layout_alignParentRight="true" androID:layout_alignParentEnd="true" androID:hint="Numbers" /><EditText androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:ID="@+ID/etPrize" androID:hint="Secret CD" androID:layout_below="@+ID/etUkNown" androID:layout_alignParentleft="true" androID:layout_alignParentStart="true" androID:layout_alignRight="@+ID/etUkNown" androID:layout_alignEnd="@+ID/etUkNown" /><EditText androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:inputType="textPersonname" androID:ems="10" androID:ID="@+ID/etname" androID:hint="Full name" androID:layout_below="@+ID/etPrize" androID:layout_alignRight="@+ID/etPrize" androID:layout_alignEnd="@+ID/etPrize" androID:layout_alignParentleft="true" androID:layout_alignParentStart="true" /><EditText androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:inputType="textEmailAddress" androID:ems="10" androID:ID="@+ID/etEmail" androID:hint="New Email" androID:layout_below="@+ID/etname" androID:layout_alignParentleft="true" androID:layout_alignParentStart="true" androID:layout_alignRight="@+ID/etname" androID:layout_alignEnd="@+ID/etname" /><button androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Register" androID:ID="@+ID/bRegister" androID:layout_margintop="46dp" androID:layout_below="@+ID/etEmail" androID:layout_centerHorizontal="true" />
解决方法:
再添加一个权限ACCESS_NETWORK_STATE
<uses-permission androID:name="androID.permission.INTERNET"></uses-permission><uses-permission androID:name="androID.permission.ACCESS_NETWORK_STATE" /><application androID:allowBackup="true" androID:icon="@mipmap/ic_launcher" androID:label="@string/app_name" androID:supportsRtl="true" androID:theme="@style/Apptheme"> <activity androID:name=".MainActivity"> <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity androID:name=".Registeractivity"></activity></application>
总结 以上是内存溢出为你收集整理的java – 代码不起作用,按钮没有响应全部内容,希望文章能够帮你解决java – 代码不起作用,按钮没有响应所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)