Kivy+Buildozer开发Android加密货币价格应用联网崩溃求助
Hey there! Let's figure out why your KivyMD crypto price app is crashing on Android as soon as it starts—especially since it works fine on desktop and when you use a test dictionary. I’ve dealt with similar issues before, so here’s what’s likely going wrong and how to fix it:
1. The #1 Culprit: UI Updates in Background Threads
Your got_json callback function is directly modifying the UI (adding TwoLineListItem to listview) from the background thread that UrlRequest uses to fetch data. Kivy requires all UI changes to happen on the main thread—desktop environments might let this slip by, but Android's strict thread enforcement will cause an immediate crash.
Fix This: Use Clock.schedule_once to Run UI Code on Main Thread
First, add the Clock import at the top of your script:
from kivy.clock import Clock
Then rewrite your got_json function to wrap UI updates in a main-thread scheduled task:
def got_json(req, result): # Helper function that runs safely on the main thread def update_ui(_): try: for coin, prices in result.items(): item = TwoLineListItem( text=coin, secondary_text=f"{prices['USD']} USD" ) listview.add_widget(item) except Exception as e: print(f"Error processing API data: {str(e)}") # Log errors for debugging # Schedule the UI update to run on main thread Clock.schedule_once(update_ui)
2. Double-Check Buildozer Permissions
Even though you mentioned adding permissions, let’s confirm your buildozer.spec has the critical internet access permission. Look for this line and ensure it’s present:
android.permissions = INTERNET
You can optionally add ACCESS_NETWORK_STATE to help with network status checks (though it’s not strictly required here):
android.permissions = INTERNET, ACCESS_NETWORK_STATE
3. Add Error Handling to Catch Data Mismatches
Sometimes the API response might not match your expected structure (e.g., a missing "USD" key for a coin), which would trigger an unhandled exception and crash the app. The try-except block in the update_ui function above will catch these issues and log them, making debugging easier.
4. Verify Buildozer Dependencies
Make sure your buildozer.spec lists all required packages correctly. For your app, the requirements should look like this:
requirements = python3,kivy,kivymd
UrlRequest is part of Kivy, so you don’t need extra network packages—just ensure these core dependencies are properly listed.
5. Quick Optimization: Clean Up Your API Request
A small side note: Your API URL has LTC listed twice in the fsyms parameter. Removing the duplicate won’t fix the crash, but it will clean up your request:
req = UrlRequest('https://min-api.cryptocompare.com/data/pricemulti?fsyms=BTC,BCH,LTC,XLM,DASH,XEM,LIBRA,BNB,ETH,XMR,XRP&tsyms=USD', got_json)
After making these changes, rebuild your app with buildozer android debug deploy run—the main fix here is moving UI updates to the main thread, which is the most common cause of such crashes in Kivy Android apps.
内容的提问来源于stack exchange,提问作者Manhal

