PDA

View Full Version : کرش شدن برنامه آندروید موقع خواندن اطلاعات از mysql



farshid.pc
شنبه 22 اردیبهشت 1397, 12:38 عصر
با سلام خدمت دوستان عزیز لطفا راهنماییم کنید تو پروژه به یه مشکلی برخوردم 2-3 روزه هر چی میگردم سایتهای داخلی و خارجی رو گشتم چیزی پیدا نمیکنم
من می خوام از دیتابیس mysql داده ها رو با json پارس کنم و تو آندروید و داخل یه لیست ویو نمایش بدم اول تو یه هاست رایگان (gigfa) تست کردم برنامه کرش میشه اینم کد کرش:

$ adb shell am start -n "com.example.farshid.myconnect/com.example.farshid.myconnect.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 1823 on device emulator-5554
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
D/: HostConnection::get() New Host Connection established 0xb7a14f60, tid 1823
W/EGL_emulation: eglSurfaceAttrib not implemented
D/OpenGLRenderer: Enabling debug mode 0
D/dalvikvm: GC_FOR_ALLOC freed 72K, 5% free 2903K/3040K, paused 2ms, total 2ms
I/dalvikvm-heap: Grow heap (frag case) to 3.503MB for 635808-byte allocation
D/dalvikvm: GC_FOR_ALLOC freed 2K, 4% free 3522K/3664K, paused 1ms, total 2ms
D/AndroidRuntime: Shutting down VM
W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xaf849b20)
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.farshid.myconnect, PID: 1823
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.farshid.myconnect/com.example.farshid.myconnect.AllProductsActivity} : java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(Ac tivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThre ad.java:135)
at android.app.ActivityThread$H.handleMessage(Activit yThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:10 2)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.jav a:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.app.ListActivity.onContentChanged(ListActi vity.java:243)
at com.android.internal.policy.impl.PhoneWindow.setCo ntentView(PhoneWindow.java:293)
at android.app.Activity.setContentView(Activity.java: 1929)
at com.example.farshid.myconnect.AllProductsActivity. onCreate(AllProductsActivity.java:51)
at android.app.Activity.performCreate(Activity.java:5 231)
at android.app.Instrumentation.callActivityOnCreate(I nstrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(Ac tivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThre ad.java:135)
at android.app.ActivityThread$H.handleMessage(Activit yThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:10 2)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.jav a:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:595)
at dalvik.system.NativeStart.main(Native Method)
Application terminated.

و فایل php و فایل جاوای AllProductsActivityو MainActivity رو هم ضمیمه کردم
لطفا راهنمایم کنید
<?php

$host= "sbt-upvc.ir/myphp";
$dbname="sbtupvci_myShop";
$user = "sbtupvci_shop";
$pass = "farshid";

$response = array();

$con = mysqli_connect($host,$user,$pass,$dbname);
// get all products from products table
$sql = "select * from tblshop ";
$result = mysqli_query($con,$sql);
//$result = mysqli_query($con,"SELECT *FROM tblshop");

// check for empty result
if ($result->num_rows > 0) {
// looping through all results
// products node
$response["tblshop"] = array();

while ($row = mysqli_fetch_array($result)) {
// temp user array
$product = array();
$product["idImage"] = $row["idImage"];
$product["nameKala"] = $row["nameKala"];
$product["fiPrice"] = $row["fiPrice"];
$product["description"] = $row["description"];
//$product["updated_at"] = $row["updated_at"];



// push single product into final response array
array_push($response["tblshop"], $product);
}
// success
$response["success"] = 1;

// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No products found";

// echo no users JSON
echo json_encode($response);
}
?>

package com.example.farshid.myconnect;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class AllProductsActivity extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
// url to get all products list
private static String url_all_products = "http://sbt-upvc.ir/myphp/get_all_products.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "tblshop";
private static final String TAG_nameKala= "nameKala";
private static final String TAG_fiPrice = "fiPrice";
// products JSONArray
JSONArray tblshop = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_products);
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread
new LoadAllProducts().execute();
// Get listview
ListView lv = getListView();
// on seleting single product
// launching Edit Product Screen
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String nameKala = ((TextView) view.findViewById(R.id.pid)).getText()
.toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
EditProductActivity.class);
// sending pid to next activity
in.putExtra(TAG_fiPrice, nameKala);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});
}
// Response from Edit Product Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if result code 100
if (resultCode == 100) {
// if result code 100 is received
// means user edited/deleted product
// reload this screen again
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AllProductsActivity.this);
pDialog.setMessage("Loading products. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
tblshop = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
for (int i = 0; i < tblshop.length(); i++) {
JSONObject c = tblshop.getJSONObject(i);
// Storing each json item in variable
String nameKala = c.getString(TAG_nameKala);
String fiPrice = c.getString(TAG_fiPrice);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_nameKala, nameKala);
map.put(TAG_fiPrice, fiPrice);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getApplicationContext(),
NewProductActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
AllProductsActivity.this, productsList,
R.layout.list_item, new String[] { TAG_nameKala,
TAG_fiPrice},
new int[] { R.id.pid, R.id.name });
// updating listview
setListAdapter(adapter);
}
});
}
}
}





package com.example.farshid.myconnect;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends Activity {
Button btnViewProducts;
Button btnNewProduct;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Buttons
btnViewProducts = (Button) findViewById(R.id.btnViewProducts);
btnNewProduct = (Button) findViewById(R.id.btnCreateProduct);
// view products click event
btnViewProducts.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Launching All products Activity
Intent i = new Intent(getApplicationContext(), AllProductsActivity.class);
startActivity(i);
}
});
// view products click event
btnNewProduct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Launching create new product activity
Intent i = new Intent(getApplicationContext(), NewProductActivity.class);
startActivity(i);
}
});
}
}

الانم که هاست غیررایگان خریداری کردم php این خطاهارو میده

[12-May-2018 07:01:57 UTC] PHP Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/sbtupvci/public_html/myphp/get_all_products.php on line 10
[12-May-2018 07:01:57 UTC] PHP Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/sbtupvci/public_html/myphp/get_all_products.php on line 10
[12-May-2018 07:01:57 UTC] PHP Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in /home/sbtupvci/public_html/myphp/get_all_products.php on line 13

لطفا راهنمایی کنید
با تشکر

farshid.pc
شنبه 22 اردیبهشت 1397, 12:50 عصر
کسی نیست راهنمایی کنه؟

farhad_shiri_ex
شنبه 22 اردیبهشت 1397, 15:45 عصر
دوست عزیز حداقل زحمت میکشیدید کدها رو تو تگ های مناسب که در این تالار مرسوم هست منظورم همون BB Code هاست که پایین صفحات مبینید می نوشتید و یا خود فایلها رو ضمیمه میکردید!!!
چون معمولا کسی نمیاد کد های شما رو اینطوری مطالعه کنه.
به هر حال من کدها را بررسی میکنم همینجا اطلاع خواهم داد.

farhad_shiri_ex
شنبه 22 اردیبهشت 1397, 15:58 عصر
[12-May-2018 07:01:57 UTC] PHP Warning: mysqli_connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/sbtupvci/public_html/myphp/get_all_products.php on line 10
[12-May-2018 07:01:57 UTC] PHP Warning: mysqli_connect(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/sbtupvci/public_html/myphp/get_all_products.php on line 10
[12-May-2018 07:01:57 UTC] PHP Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in /home/sbtupvci/public_html/myphp/get_all_products.php on line 13

این خطا ها که میگه که نمیتونه به هاست شما وصل بشه! پس تو لایه وب سرور تون اشکال دارید
1- بجای استفاده از DNS name سایت از آدرس IP و Port استفاده کنید.!
2- آیا قبل از انتقال به سرور به WAMP ویا LAMP کنترل کردید که وب سرویس هاتون درست هست ؟!
3- اگر با ومپ سرور مشکلی نداشتید دسترسی به هاست تون چک کنید معمولا روی هاست های لینوکسی باید PHP Admin وجود داشته باشه.!
4- کلا قبل از انجام هر کاری اول یک درخواست HTML با خود PHP به هاست ارسال کنید ببنید اصلا وصل میشه یا نه! تا به سمت اندروید برسیم.

Nevercom
شنبه 22 اردیبهشت 1397, 16:24 عصر
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.farshid.myconnect/com.example.farshid.myconnect.AllProductsActivity} : java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'


این خطا در برنامه‌ی اندروید توضیح داده که مشکل چیه.

کلاس اکتیویتی شما از ListActivity مشتق شده، که لازمه در فابل Layoutی که واسه این اکتیویتی استفاده می کنید، یک ListView داشته باشید که id اون دقیقاً @android:id/list باشه.

مثلن چیزی شبیه به این


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">

<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>

<TextView android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>

farhad_shiri_ex
شنبه 22 اردیبهشت 1397, 19:17 عصر
البته همونطور که دوست مون هم توضیح دادن طبق داکیومنت خود کلاس ListActivity که در داخل کلاس نوشته شده است برای رفع اشکال سمت اندروید هم باید یک لیست ویو با ID list داشته باشید

/**
* Updates the screen state (current list and other views) when the
* content changes.
*
* @see Activity#onContentChanged()
*/
@Override
public void onContentChanged() {
super.onContentChanged();
View emptyView = findViewById(com.android.internal.R.id.empty);
mList = (ListView)findViewById(com.android.internal.R.id.l ist);
if (mList == null) {
throw new RuntimeException(
"Your content must have a ListView whose id attribute is " +
"'android.R.id.list'");
}
if (emptyView != null) {
mList.setEmptyView(emptyView);
}
mList.setOnItemClickListener(mOnClickListener);
if (mFinishedStart) {
setListAdapter(mAdapter);
}
mHandler.post(mRequestFocus);
mFinishedStart = true;
}

farshid.pc
شنبه 22 اردیبهشت 1397, 19:22 عصر
اقا سلام و خسته نباشید و با عرض معذرت کدهام رو درست و حسابی ننوشتم من داخل لایوت all_product یه لیست ویو با آیدی list دارم باید اون رو کجا تعریفش کنم یا اصلا باید تعریف کنم یا نه
اینم کد خطای جدید
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.D/dalvikvm: GC_FOR_ALLOC freed 70K, 5% free 2903K/3028K, paused 3ms, total 6ms
I/dalvikvm-heap: Grow heap (frag case) to 3.492MB for 635808-byte allocation
D/dalvikvm: GC_FOR_ALLOC freed 2K, 4% free 3522K/3652K, paused 17ms, total 17ms
D/AndroidRuntime: Shutting down VM
W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xaf833b20)
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.farshid.myconnect, PID: 2351
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.farshid.myconnect/com.example.farshid.myconnect.AllProductsActivity} : java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(Ac tivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThre ad.java:135)
at android.app.ActivityThread$H.handleMessage(Activit yThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:10 2)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.jav a:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.app.ListActivity.onContentChanged(ListActi vity.java:243)
at com.android.internal.policy.impl.PhoneWindow.setCo ntentView(PhoneWindow.java:293)
at android.app.Activity.setContentView(Activity.java: 1929)
at com.example.farshid.myconnect.AllProductsActivity. onCreate(AllProductsActivity.java:51)
at android.app.Activity.performCreate(Activity.java:5 231)
at android.app.Instrumentation.callActivityOnCreate(I nstrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(Ac tivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThre ad.java:135)
at android.app.ActivityThread$H.handleMessage(Activit yThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:10 2)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.jav a:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:595)
at dalvik.system.NativeStart.main(Native Method)
Application terminated.

farshid.pc
شنبه 22 اردیبهشت 1397, 19:41 عصر
اقا فرهاد یه راهنمایی بکن emptyView و mList چی هستن و کجا تعریفشون کنم؟

farhad_shiri_ex
شنبه 22 اردیبهشت 1397, 21:59 عصر
اقا فرهاد یه راهنمایی بکن emptyView و mList چی هستن و کجا تعریفشون کنم؟

دوست عزیز اون تکه کدی که من گذاشتم برای همین سورس کد ListActivity شما با اون کاری ندارید.صرفا جهت اطلاع شما گذاشتم که سورس را هم بخونید بیشتر متوجه می شوید.
تو همون اکتیویتی AllProductsActivity در متد OnCreate در لایه ای که Set Content View را انجام میدید باید یک لیست ویو داشته باشی فقط حتما بهش ID بده به نام list دقیقا همین . حالا اگر لایه های مختلفی داری تو لایه اصلی اکتیویتی، مشکلی نیست در هر لایه ای که جز زیر لایه های اصلی هستند، میتونید لیست ویو را اضافه کنید.