当我输入凭据并按下按钮时,我需要转到新活动,即feedback1。但是,每次按下按钮
时,我的应用都会崩溃以下是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>
答案 0 :(得分:1)
在新的Intent语句中删除带有MainActivity.this的getApplicationContext()
答案 1 :(得分:0)
很难说没有看到堆栈跟踪,但您似乎从未声明或实例化您在方法fixedcon
中使用的对象loginfunc(...)
。
*FixedconObjectType* fixedcon = new *FixedconObjectType()*;