2018-04-22 00:14
浏览 53


Good day to all.

I'm creating a social android app for posting offers of services for the skilled & talented within our univerity. I'm using the conventional languages for android: Java and XML.

But whenever I invoke the createOffer method within the CreateOfferActivity the app crashes without producing any exceptions and simply restarts.

Here's the code for CreateOfferActivity:

package com.italent.italent;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;


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

import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class CreateOfferActivity extends AppCompatActivity {

private EditText offerTitle;
private EditText offerDesc;
private EditText offerMinPrice;
private EditText offerMaxPrice;

private Spinner offerCategory;

private Button crtOffer;

String offCategory;
String imagePath;

public double minPrice;
public double maxPrice;

private static final String TAG = "CreateOfferActivity";
private static final String URL_FOR_CREATING_OFFER = "https://*****/create_offer.php";
ProgressDialog progressDialog;

protected void onCreate(Bundle savedInstanceState) {

    offerTitle = (EditText) findViewById(;
    offerDesc = (EditText) findViewById(;
    offerMinPrice = (EditText) findViewById(;
    offerMaxPrice = (EditText) findViewById(;

    offerCategory = (Spinner) findViewById(;

    crtOffer = (Button) findViewById(;

    ArrayAdapter<CharSequence> arrAdapter = ArrayAdapter.createFromResource(this,
            R.array.category_Array, android.R.layout.simple_spinner_item);

    // Specify the layout to use when the list of choices appears

    // Apply the adapter to the spinner

    //Create Offer button onClick listener
    crtOffer.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Toast.makeText(CreateOfferActivity.this, "It works...", Toast.LENGTH_LONG).show();


private void submitOfferForm() {

    String offTitle = offerTitle.getText().toString();
    String offDesc = offerDesc.getText().toString();

    //////////Replace image path later with a valid one
    imagePath = "Some Image Path";

    offCategory = offerCategory.getSelectedItem().toString();

    //getting sharedPreferences file named userInfo to retrieve MemberName, Username and branch
    SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
    String ofMName = sharedPreferences.getString("MemberName:", "");
    String ofUName = sharedPreferences.getString("UserName:", "");
    String ofBranch = sharedPreferences.getString("Branch:", "");

    String mnPri = " " + minPrice;
    String mxPri = " " + maxPrice;

    createOffer(ofMName, ofUName, offTitle, offDesc, ofBranch, mnPri, mxPri, imagePath, offCategory);

private void createOffer(final String oMName, final String oUName,  final String oTitle, final String oText,
                          final String oBranch,final String oMinPri, final String oMaxPri, final String oImage, final String oCategory) {
    // Tag used to cancel the request
    String cancel_req_tag = "offer";

    progressDialog.setMessage("Adding your offer...");

    StringRequest strReq = new StringRequest(Request.Method.POST,
            URL_FOR_CREATING_OFFER, new Response.Listener<String>() {

        public void onResponse(String response) {
            Log.d(TAG, "Offer Response: " + response.toString());

            try {
                JSONObject jObj = new JSONObject(response);
                boolean error = jObj.getBoolean("error");

                if (!error) { // error is set to false, meaning no error
                    //String user = jObj.getJSONObject("offer").getString("name");
                    Toast.makeText(CreateOfferActivity.this, "Your Offer has been created!", Toast.LENGTH_SHORT).show();

                    // Launch Home activity
                    Intent HomeIntent = new Intent(CreateOfferActivity.this, HomeActivity.class);
                } else {

                    String errorMsg = jObj.getString("error_msg");
                    Toast.makeText(CreateOfferActivity.this, errorMsg, Toast.LENGTH_LONG).show();
            } catch (JSONException e) {

    }, new Response.ErrorListener() {

        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Error in creating offer: " + error.getMessage());
                    error.getMessage(), Toast.LENGTH_LONG).show();
    }) {
        protected Map<String, String> getParams() {
            // Posting params to create offer url
            Map<String, String> params = new HashMap<String, String>();

            params.put("offerMemberName", oMName);
            params.put("offerUserName", oUName);
            params.put("offerTitle", oTitle);
            params.put("OfferText", oText);
            params.put("OfferBranch", oBranch);
            params.put("OfferDateTime", DateFormat.getDateTimeInstance().format(new Date()));
            params.put("OfferMinPrice", "" + oMinPri);
            params.put("OfferMaxPrice", "" + oMaxPri);
            params.put("OfferCategory", oCategory);
            params.put("OfferImage", oImage);

            return params;

    // Adding request to request queue
    AppSingleton.getInstance(getApplicationContext()).addToRequestQueue(strReq, cancel_req_tag);

private void showDialog() {
    if (!progressDialog.isShowing());

private void hideDialog() {
    if (progressDialog.isShowing())

And the code for create_offer.php


require_once 'update_user_info.php';
$db = new update_user_info();

// json response array
$response = array("error" => FALSE);

if (isset($_POST['offerTitle']) && isset($_POST['offerMname']) &&         isset($_POST['offerBranch']) && isset($_POST['offerText']) && isset($_POST['offerImageURL']) && 
isset($_POST['offerDateTime']) && isset($_POST['offerMinPrice']) && isset($_POST['offerMaxPrice']) && isset($_POST['offerCategory']) && isset($_POST['offerUname'])) {

// receiving the post params
$ofTitle = $_POST['offerTitle'];
$ofMName = $_POST['offerMname'];
$ofBranch = $_POST['offerBranch'];
$ofText = $_POST['offerText'];
$ofImageURL = $_POST['offerImageURL'];
$ofDateTime = $_POST['offerDateTime'];
$ofMinPri = $_POST['offerMinPrice'];
$ofMaxPri = $_POST['offerMaxPrice'];
$ofCategory = $_POST['offerCategory'];
$ofUName = $_POST['offerUname'];

// check if user is already existed with the same email
if (!($db->checkExistingUserThruUname($ofUName))) {
    // user doesn't exist
    $response["error"] = TRUE;
    $response["error_msg"] = "Visitors cannot post an offer. Please register first.";
    echo json_encode($response);
} else {
    // create a new offer 
    $user = $db->storeOfferInfo($ofTitle, $ofMName, $ofBranch, $ofText, $ofImageURL, $ofDateTime, $ofMinPri, $ofMaxPri, $ofCategory, $ofUName);
    if ($offer) {
        // offer stored successfully
        $response["error"] = FALSE;
        $response["offer"]["offTitle"] = $offer["offTitle"];
        $response["offer"]["offMname"] = $offer["offMname"];
        $response["offer"]["offBranch"] = $offer["offBranch"];
        $response["offer"]["offText"] = $offer["offText"];
        $response["offer"]["offImageURL"] = $offer["offImageURL"];
        $response["offer"]["offDateTime"] = $offer["offDateTime"];
        $response["offer"]["offMinPrice"] = $offer["offMinPrice"];
        $response["offer"]["offMaxPrice"] = $offer["offMaxPrice"];
        $response["offer"]["offCategory"] = $offer["offCategory"];
        $response["offer"]["offUname"] = $offer["offUname"];

        echo json_encode($response);
    } else {
        // offer failed to store
        $response["error"] = TRUE; 
        $response["error_msg"] = "Unknown error occurred in offer creation!";
        echo json_encode($response);
} else {
$response["error"] = TRUE;
$response["error_msg"] = "A required field is missing!";
echo json_encode($response);

This is the Logcat:

04-22 03:24:38.950 27998-27998/com.italent.italent E/AndroidRuntime: FATAL EXCEPTION: main
                                                                 Process: com.italent.italent, PID: 27998
                                                                 java.lang.NullPointerException: Attempt to invoke virtual method 'void' on a null object reference
                                                                     at com.italent.italent.CreateOfferActivity.createOffer(
                                                                     at com.italent.italent.CreateOfferActivity.submitOfferForm(
                                                                     at com.italent.italent.CreateOfferActivity.access$000(
                                                                     at com.italent.italent.CreateOfferActivity$1.onClick(
                                                                     at android.view.View.performClick(
                                                                     at android.widget.TextView.performClick(
                                                                     at android.view.View$
                                                                     at android.os.Handler.handleCallback(
                                                                     at android.os.Handler.dispatchMessage(
                                                                     at android.os.Looper.loop(
                                                                     at java.lang.reflect.Method.invoke(Native Method)

P.S. i used almost the exact same java code, php code and database for registration and it works fine but i can't find why it doesn't work with this class.

I appreciate any guidlines or help. Thank you!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dqp99585 2018-04-22 00:35

    progressDialog variable is not initialized. Add following code in your activity onCreate()

     ProgressDialog progressDialog = new ProgressDialog(this);
    解决 无用
    打赏 举报

相关推荐 更多相似问题