我正在尝试创建一个适用于在线数据库的应用程序,我检查了所有的PHP工作正常,因此服务器端没有错误,但每当我的这部分活动开始时,应用程序崩溃并再次发生到上一页。
package hp.bunk.com.bunk;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
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.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class displaybunk extends AppCompatActivity implements View.OnClickListener {
String y, n, m, t;
Button b1, b2, b3;
String url = "URL OF PHP";
TextView y1 = (TextView) findViewById(R.id.yes);
TextView m1 = (TextView) findViewById(R.id.majority);
TextView n1 = (TextView) findViewById(R.id.no);
TextView t1 = (TextView) findViewById(R.id.details);
Bundle extras = getIntent().getExtras();
String id;
Boolean vie;
String url1= "URL OF PHP";
String url2 = "URL OF PHP";
String url3 = "URL OF PHP";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_displaybunk);
b1 = (Button) findViewById(R.id.button6);
b2 = (Button) findViewById(R.id.button7);
b3 = (Button) findViewById(R.id.button8);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
if (extras != null) {
id = extras.getString("new_variable_name");
}
RelativeLayout re = (RelativeLayout) findViewById(R.id.relative);
SharedPreferences sha1 = getSharedPreferences("bunks.com.android",MODE_PRIVATE);
vie = sha1.getBoolean(id,false);
if (vie == true){
re.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(),"you have already voted ",Toast.LENGTH_LONG).show();
}
StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject object = new JSONObject(response);
y = object.getString("yes");
m = object.getString("depend");
n = object.getString("no");
t = object.getString("yes");
t1.setText(t);
n1.setText(n);
m1.setText(m);
y1.setText(y);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return super.getParams();
}
};
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button6:
Intent intent = getIntent();
SharedPreferences sha = getSharedPreferences("bunks.com.android", MODE_PRIVATE);
SharedPreferences.Editor edit = sha.edit();
edit.putBoolean(id, true);
edit.commit();
RequestQueue requestqueue = new Volley().newRequestQueue(getApplicationContext());
StringRequest request = new StringRequest(Request.Method.POST, url1, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.names().get(0).equals("Sucess")) {
Toast.makeText(getApplicationContext(),"Your Vote has been Counted",Toast.LENGTH_LONG);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> re = new HashMap<String, String>();
re.put("id", id);
return re;
}
};
requestqueue.add(request);
finish();
startActivity(intent);
break;
case R.id.button7:
Intent intent1 = getIntent();
SharedPreferences sha1 = getSharedPreferences("bunks.com.android", MODE_PRIVATE);
SharedPreferences.Editor edit1 = sha1.edit();
edit1.putBoolean(id, true);
edit1.commit();
RequestQueue requestqueue1 = new Volley().newRequestQueue(getApplicationContext());
StringRequest request1 = new StringRequest(Request.Method.POST, url2, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.names().get(0).equals("Sucess")) {
Toast.makeText(getApplicationContext(), "Your Vote has been Counted", Toast.LENGTH_LONG);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> re = new HashMap<String, String>();
re.put("id", id);
return re;
}
};
requestqueue1.add(request1);
finish();
startActivity(intent1);
break;
case R.id.button8:
Intent intent2 = getIntent();
SharedPreferences sha2 = getSharedPreferences("bunks.com.android",MODE_PRIVATE);
SharedPreferences.Editor edit2 = sha2.edit();
edit2.putBoolean(id,true);
edit2.commit();
RequestQueue requestqueue2 = new Volley().newRequestQueue(getApplicationContext());
StringRequest request2 = new StringRequest(Request.Method.POST, url3, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.names().get(0).equals("Sucess")) {
Toast.makeText(getApplicationContext(), "Your Vote has been Counted", Toast.LENGTH_LONG);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> re = new HashMap<String, String>();
re.put("id", id);
return re;
}
};
requestqueue2.add(request2);
finish();
startActivity(intent2);
break;
}
}
}
这里也是我的日志文件:
04-07 01:23:01.329 11852-11852/hp.bunk.com.bunk D/OpenGLRenderer: Enabling debug mode 0
04-07 01:23:03.229 11852-11852/hp.bunk.com.bunk E/ViewRootImpl: sendUserActionEvent() mView == null
04-07 01:28:11.289 11852-11852/hp.bunk.com.bunk E/ViewRootImpl: sendUserActionEvent() mView == null
04-07 01:28:16.869 11852-11852/hp.bunk.com.bunk D/AndroidRuntime: Shutting down VM
04-07 01:28:16.869 11852-11852/hp.bunk.com.bunk W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x418b8da0)
04-07 01:28:16.879 11852-11852/hp.bunk.com.bunk E/AndroidRuntime: FATAL EXCEPTION: main
Process: hp.bunk.com.bunk, PID: 11852
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{hp.bunk.com.bunk/hp.bunk.com.bunk.displaybunk}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2313)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2500)
at android.app.ActivityThread.access$900(ActivityThread.java:171)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5679)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:68)
at android.support.v7.app.AppCompatDelegateImplV7.<init>(AppCompatDelegateImplV7.java:145)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:28)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:42)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:186)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:168)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:508)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:180)
at hp.bunk.com.bunk.displaybunk.<init>(displaybunk.java:31)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2304)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2500)
at android.app.ActivityThread.access$900(ActivityThread.java:171)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5679)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
你不能像现在这样在类构造函数中调用getFindById:
TextView y1 = (TextView) findViewById(R.id.yes);
TextView m1 = (TextView) findViewById(R.id.majority);
TextView n1 = (TextView) findViewById(R.id.no);
TextView t1 = (TextView) findViewById(R.id.details);
你必须等到onCreate()期间视图膨胀之后,所以将这些调用移到findViewById()到那一点。
答案 1 :(得分:0)
我找到了一个解决方案......捆绑附加功能;他们应该在onCreate()之后初始化。顺便感谢@Doug Stevenson