انجمن سایت کلیدستان


رتبه موضوع:
  • 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
مشکل بسته شدن ناگهانی برنامه در مرحله تست

مشکل بسته شدن ناگهانی برنامه در مرحله تست

#1
سلام و احترام
بنده تقریبا تازه وارد در زمیینه برنامه نویسی اندروید هستم.

بنده به تازگی برنامه هایی که می نویسم با اینکه هیچ خطایی از طرف Android Studio داده نمیشه اما برنامه در مرحله تست بطور ناگهانی بسته می شه.

خواستم بدونم که:
1-آیا ممکنه به کامپایلر (اندروید استادیو یا ایکلیپس) ربط داشته باشه ؟ اگر بله کجاها باید چک بشن ؟
2-با توجه به سه سورس کد زیر که مربوط به MainActivity و myVolleyController و Manifest هست مشکل کجاست !!؟

این برنامه دارای دو دکمه برای پارس کردن json  و نمایش در یک TextView هست . یکی برای Object و دیگری برای Array. برنامه بدون مشکل باز میشه ولی وقتی روی هرکدوم از دو دکمه کلیک میکنم بسته میشه کلا!

ازین دست مشکلات زیاد برام پیش میاد که هیچ خطایی اندروید استادیو نمیگیره ولی برنامه بسته میشه ! ممنون میشم اگه عیب یابی دراین گونه مواقع رو هم آموزشش رو بهم معرفی کنید.

یک دنیا سپاس از الطاف همه دوستان

کد:
package net.aminapps.volleyjson;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;

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

import java.net.URL;

public class MainActivity extends AppCompatActivity {

   private static final String URLJsonObject="http://api.androidhive.info/volley/person_object.json";
   private static final String URLJsonArry = "http://api.androidhive.info/volley/person_array.json";
   private static String TAG = MainActivity.class.getSimpleName();
   public Button btnMakeObjectRequest,btnMakeArrayRequest;
   private ProgressDialog pDialog;
   private TextView txtResponse;
   private String jsonResponse;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
       setSupportActionBar(toolbar);

       FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
       fab.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                       .setAction("Action", null).show();
           }
       });

       btnMakeObjectRequest = (Button) findViewById(R.id.btnObjRequest);
       btnMakeArrayRequest = (Button) findViewById(R.id.btnArrayRequest);
       txtResponse = (TextView) findViewById(R.id.txtResponse);
       pDialog = new ProgressDialog(this);
       pDialog.setMessage("Please wait...");
       pDialog.setCancelable(false);
       btnMakeObjectRequest.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               makeJsonObjectRequest();
           }
       });
       btnMakeArrayRequest.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               makeJsonArrayRequest();
           }
       });
   }
   private void makeJsonObjectRequest() {
       showpDialog();
       JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
               URLJsonObject, null, new Response.Listener<JSONObject>() {
           public void onResponse(JSONObject response) {
               Log.d(TAG, response.toString());
               try {
                   String name = response.getString("name");
                   String email = response.getString("email");
                   JSONObject phone = response.getJSONObject("phone");
                   String home = phone.getString("home");
                   String mobile = phone.getString("mobile");
                   jsonResponse = "";
                   jsonResponse += "Name: " + name + "\n\n";
                   jsonResponse += "Email: " + email + "\n\n";
                   jsonResponse += "Home: " + home + "\n\n";
                   jsonResponse += "Mobile: " + mobile + "\n\n";

                   txtResponse.setText(jsonResponse);

               } catch (JSONException e) {
                   e.printStackTrace();
                   Toast.makeText(getApplicationContext(),
                           "Error: " + e.getMessage(),
                           Toast.LENGTH_LONG).show();
               }
               hidepDialog();
           }
       }, new Response.ErrorListener() {
           public void onErrorResponse(VolleyError error) {
               VolleyLog.d(TAG, "Error: " + error.getMessage());
               Toast.makeText(getApplicationContext(),
                       error.getMessage(), Toast.LENGTH_SHORT).show();
               // hide the progress dialog
               hidepDialog();
           }
       });
       myVolleyController.getInstance().addToRequestQueue(jsonObjReq);
   }
       private void makeJsonArrayRequest() {
           showpDialog();
           JsonArrayRequest req = new JsonArrayRequest(URLJsonArry,
                   new Response.Listener<JSONArray>() {
                       @Override
                       public void onResponse(JSONArray response) {
                           Log.d(TAG, response.toString());
                           try {
                               jsonResponse = "";
                               for (int i = 0; i < response.length(); i++) {
                                   JSONObject person = (JSONObject) response.get(i);
                                   String name = person.getString("name");
                                   String email = person.getString("email");
                                   JSONObject phone = person
                                           .getJSONObject("phone");
                                   String home = phone.getString("home");
                                   String mobile = phone.getString("mobile");

                                   jsonResponse += "Name: " + name + "\n\n";
                                   jsonResponse += "Email: " + email + "\n\n";
                                   jsonResponse += "Home: " + home + "\n\n";
                                   jsonResponse += "Mobile: " + mobile + "\n\n\n";

                               }
                               txtResponse.setText(jsonResponse);

                           } catch (JSONException e) {
                               e.printStackTrace();
                               Toast.makeText(getApplicationContext(),
                                       "Error: " + e.getMessage(),
                                       Toast.LENGTH_LONG).show();
                           }

                           hidepDialog();
                       }
                   }, new Response.ErrorListener() {
               @Override
               public void onErrorResponse(VolleyError error) {
                   VolleyLog.d(TAG, "Error: " + error.getMessage());
                   Toast.makeText(getApplicationContext(),
                           error.getMessage(), Toast.LENGTH_SHORT).show();
                   hidepDialog();
               }
           });
           myVolleyController.getInstance().addToRequestQueue(req);
       }

       private void showpDialog() {
           if (!pDialog.isShowing())
               pDialog.show();
       }

       private void hidepDialog() {
           if (pDialog.isShowing())
               pDialog.dismiss();
       }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       // Inflate the menu; this adds items to the action bar if it is present.
       getMenuInflater().inflate(R.menu.menu_main, menu);
       return true;
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
       // Handle action bar item clicks here. The action bar will
       // automatically handle clicks on the Home/Up button, so long
       // as you specify a parent activity in AndroidManifest.xml.
       int id = item.getItemId();

       //noinspection SimplifiableIfStatement
       if (id == R.id.action_settings) {
           return true;
       }

       return super.onOptionsItemSelected(item);
   }
}
کد:
package net.aminapps.volleyjson;

import android.app.Application;
import android.app.DownloadManager;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

/**
* Created by My PC on 12/14/2016.
*/
public class myVolleyController extends Application {
   public static final String TAG = myVolleyController.class.getSimpleName();
   private RequestQueue myRQ;
   private static myVolleyController myInstance;

   public void onCreate() {
       super.onCreate();
       myInstance = this;
   }

   public static synchronized myVolleyController getInstance() {
       return myInstance;
   }

   public RequestQueue getRequestQueue() {
       if (myRQ == null) {
           myRQ = Volley.newRequestQueue(getApplicationContext());
       }

       return myRQ;
   }

   public <T> void addToRequestQueue(Request<T> req, String tag) {
       req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
       getRequestQueue().add(req);
   }

   public <T> void addToRequestQueue(Request<T> req) {
       req.setTag(TAG);
       getRequestQueue().add(req);
   }

   public void cancelPendingRequests(Object tag) {
       if (myRQ != null) {
           myRQ.cancelAll(tag);
       }

   }
}



کد:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="net.aminapps.volleyjson">

   <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"
           android:label="@string/app_name"
           android:theme="@style/AppTheme.NoActionBar">
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />

               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>
   </application>

</manifest>
پاسخ

مشکل بسته شدن ناگهانی برنامه در مرحله تست

#2
با سلام دوست عزیز

برای این که بتونید خطای های رخ داده رو کنترل کنی میتوانید کد های خود را داخل تابع try{}catch استفاده بکنید به این صورت :

try{
//YourCode
}catch(Exception e){
Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT).show();
}

با استفاده از این روش خواهید فهمید که مشکل از کجاست
با سپاس
Iran
پاسخ
 سپاس شده توسط شماره مجازی امارات


پرش به انجمن:


کاربران در حال بازدید این موضوع: 1 مهمان