gpt4 book ai didi

android - 我的应用程序在 startActivity() 上崩溃

转载 作者:行者123 更新时间:2023-11-30 00:27:50 26 4
gpt4 key购买 nike

当我输入我的凭据并按下按钮时,我需要进入一个新 Activity ,即反馈 1。但是,每次按下按钮时我的应用程序都会崩溃

这是 MainActivity 的代码:

package com.example.bohra.feedback;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


public class MainActivity extends AppCompatActivity {

EditText username1;
EditText password1;
Button login;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

login = (Button) findViewById(R.id.login);
username1 = (EditText) findViewById(R.id.username1);
password1 = (EditText) findViewById(R.id.password1);



login.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
loginfunc();

}

});
}


private void loginfunc() {

fixedcon.username = username1.getText().toString();
fixedcon.password = password1.getText().toString();
String url = fixedcon.LOGIN_URL + username1.getText().toString().trim();

StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {

showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});

RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}

private void showJSON(String response) {

String pass = "";

try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(fixedcon.JSON_ARRAY);
JSONObject collegeData = result.getJSONObject(0);
pass = collegeData.getString(fixedcon.KEY_PASSWORD);

} catch (JSONException e) {
e.printStackTrace();
}


if (pass.equals(fixedcon.password)) {
Intent androidsolved_intent = new Intent(getApplicationContext(), feedback1.class);
startActivity(androidsolved_intent);

}

else
{ Toast.makeText(getApplicationContext(), "Wrong Credentials",Toast.LENGTH_SHORT).show();
}
}

这是我的 AndroidManifest 文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bohra.feedback">
<uses-permission android:name="android.permission.INTERNET" />
<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=".feedback1" />
<activity android:name=".feedback2" />
<activity android:name=".feedback3" />
<activity android:name=".feedback4" />
<activity android:name=".feedback5" />
<activity android:name=".feedback6" />
<activity android:name=".feedback7" />
<activity android:name=".feedback8" />
<activity android:name=".feedback9" />
<activity android:name=".feedback10" />
<activity android:name=".finalconfirm" ></activity>
</application>

最佳答案

在新的 Intent 语句中使用 MainActivity.this 删除 getApplicationContext()

关于android - 我的应用程序在 startActivity() 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45044369/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com