我正在制作一个Android应用程序,我需要它来保存移动设备上的一些数据joker ammount以及哪个joker使用得分...现在我已经按照一些教程进行了操作
(1),但是当我启动读取数据的功能(procitaj)时,应用程序崩溃了。
(2)代码来自发送数据并启动sql函数的java类。
(1):
package com.peky.smartornot;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class Sql {
public static final String KEY_ROWID= "_id";
public static final String KEY_JOKERS= "joker";
public static final String KEY_JOKERAMMOUNT= "ammount";
private static final String DATABASE_NAME= "SQL";
private static final String DATABASE_TABLE= "peoples_table";
private static final int DATABASE_VERSION= 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_JOKERS + " INTEGER, " +
KEY_JOKERAMMOUNT + " INTEGER);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public Sql(Context c){
ourContext=c;
}
public Sql open(){
ourHelper = new DbHelper(ourContext);
ourDatabase=ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long spremi(int brojjokera, int kolicina) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_JOKERS, brojjokera);
cv.put(KEY_JOKERAMMOUNT, kolicina);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public int procitaj() {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_ROWID, KEY_JOKERS,
KEY_JOKERAMMOUNT };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
null, null);
int citac = 0;
int iJoker = c.getColumnIndex(KEY_JOKERS);
int iJokerammount = c.getColumnIndex(KEY_JOKERAMMOUNT);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
if (c.getInt(iJoker) == 1)
citac = c.getInt(iJokerammount);
}
return citac;
}
}
(2):
package com.peky.smartornot;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class Redeemcode extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_redeemcode);
}
public void provjeri(View view){
EditText josip=(EditText)findViewById(R.id.test);
TextView proba=(TextView)findViewById(R.id.povrat);
String provjera=josip.toString();
int kolicina=5,brojac=0;
int brojjokera=1;
Sql ulaz= new Sql(this);
if (provjera=="josip"){
ulaz.open();
ulaz.spremi(brojjokera, kolicina);
brojac= ulaz.procitaj();
ulaz.close();
}
proba.setText(brojac);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.redeemcode, menu);
return false;
}
}
非常感谢!
还有一件事我没有尝试捕获异常,因为我不知道为什么,我正在使用Eclipse作为Android应用程序。
以下是应用崩溃时的 logcat :
03-12 10:32:57.351: I/GATE(29692): <GATE-M>DEV_ACTION_COMPLETED</GATE-M>
03-12 10:32:57.391: I/Ads(29692): Received ad url: <url: "http://googleads.g.doubleclick.net:80/mads/gma?preqs=0&session_id=2391090699000457102&seq_num=1&u_w=320&msid=com.peky.smartornot&js=afma-sdk-a-v6.4.1&bas_off=0&net=wi&app_name=1.android.com.peky.smartornot&hl=hr&gnt=0&carrier=21803&u_audio=1&kw&u_sd=1.5&ms=x5WFrcufvr_jzmPGXul4pXm4j0_I44XneDQq7ReQ_eLwcdKhxzFKbueLE1RMfqGTrWLijIGhRF6KQk2Z4bBjbsZdMD1lZk_GA2j5WHh-fcmTPk_T5tktJPhgi1F7udQELi_0s8K5vsXrbHmdQzkLi7p3Z1ZHyV0UnV3f4Rdr73bIrVn5IKpwepIRjYWjcfhWY7dYssfzdTnow7M6uHw3E6UOZk_9ozep83qqS9XRECg9sSoaARsBkg1dnTdswTVGFvbUFsfoB1z3tU_UFMRCGp6_9GmyprRN9pScxSLmla7rK3dKsIhIZn44urBYCIiMMNAh7H5G0Z4y-6YE0ImyQQ&mv=80250010.com.android.vending&format=320x50_mb&oar=0&u_h=533&bas_on=0&ptime=0&imbf=%40installed_markets%40&u_so=p&output=html®ion=mobile_app&u_tz=60&client_sdk=1&ex=1&client=ca-app-pub-6451932264844765&slotname=2000735339&kw_type=broad&gsb=wi&caps=inlineVideo_interactiveVideo_mraid1_th_autoplay_mediation_sdkAdmobApiForAds_di&eid=46621026&jsv=77&urll=951" type: "admob" afmaNotifyDt: "null" activationOverlayUrl: "null" useWebViewLoadUrl: "false">
03-12 10:32:57.421: I/Ads(29692): Ad url modified to http://googleads.g.doubleclick.net:80/mads/gma?preqs=0&session_id=2391090699000457102&seq_num=1&u_w=320&msid=com.peky.smartornot&js=afma-sdk-a-v6.4.1&bas_off=0&net=wi&app_name=1.android.com.peky.smartornot&hl=hr&gnt=0&carrier=21803&u_audio=1&kw&u_sd=1.5&ms=x5WFrcufvr_jzmPGXul4pXm4j0_I44XneDQq7ReQ_eLwcdKhxzFKbueLE1RMfqGTrWLijIGhRF6KQk2Z4bBjbsZdMD1lZk_GA2j5WHh-fcmTPk_T5tktJPhgi1F7udQELi_0s8K5vsXrbHmdQzkLi7p3Z1ZHyV0UnV3f4Rdr73bIrVn5IKpwepIRjYWjcfhWY7dYssfzdTnow7M6uHw3E6UOZk_9ozep83qqS9XRECg9sSoaARsBkg1dnTdswTVGFvbUFsfoB1z3tU_UFMRCGp6_9GmyprRN9pScxSLmla7rK3dKsIhIZn44urBYCIiMMNAh7H5G0Z4y-6YE0ImyQQ&mv=80250010.com.android.vending&format=320x50_mb&oar=0&u_h=533&bas_on=0&ptime=0&imbf=8009&u_so=p&output=html®ion=mobile_app&u_tz=60&client_sdk=1&ex=1&client=ca-app-pub-6451932264844765&slotname=2000735339&kw_type=broad&gsb=wi&caps=inlineVideo_interactiveVideo_mraid1_th_autoplay_mediation_sdkAdmobApiForAds_di&eid=46621026&jsv=77&urll=951
03-12 10:32:57.421: I/Ads(29692): Request scenario: Online server request.
03-12 10:32:59.001: D/webview(29692): webview.destroy() is called.
03-12 10:32:59.121: D/webviewglue(29692): nativeDestroy view: 0x1f4d900
03-12 10:32:59.201: E/Ads(29692): Cannot find adapter class 'com.google.ads.mediation.adfalcon.AdFalconAdapter'. Did you link the ad network's mediation adapter? Skipping ad network.
03-12 10:32:59.251: I/Ads(29692): The following was caught and handled:
03-12 10:32:59.251: I/Ads(29692): java.lang.ClassNotFoundException: com.google.ads.mediation.adfalcon.AdFalconAdapter
03-12 10:32:59.251: I/Ads(29692): at java.lang.Class.classForName(Native Method)
03-12 10:32:59.251: I/Ads(29692): at java.lang.Class.forName(Class.java:217)
03-12 10:32:59.251: I/Ads(29692): at java.lang.Class.forName(Class.java:172)
03-12 10:32:59.251: I/Ads(29692): at com.google.ads.g.a(SourceFile:133)
03-12 10:32:59.251: I/Ads(29692): at com.google.ads.i.run(SourceFile:116)
03-12 10:32:59.251: I/Ads(29692): at android.os.Handler.handleCallback(Handler.java:605)
03-12 10:32:59.251: I/Ads(29692): at android.os.Handler.dispatchMessage(Handler.java:92)
03-12 10:32:59.251: I/Ads(29692): at android.os.Looper.loop(Looper.java:137)
03-12 10:32:59.251: I/Ads(29692): at android.app.ActivityThread.main(ActivityThread.java:4517)
03-12 10:32:59.251: I/Ads(29692): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 10:32:59.251: I/Ads(29692): at java.lang.reflect.Method.invoke(Method.java:511)
03-12 10:32:59.251: I/Ads(29692): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
03-12 10:32:59.251: I/Ads(29692): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
03-12 10:32:59.251: I/Ads(29692): at dalvik.system.NativeStart.main(Native Method)
03-12 10:32:59.251: I/Ads(29692): Caused by: java.lang.NoClassDefFoundError: com/google/ads/mediation/adfalcon/AdFalconAdapter
03-12 10:32:59.251: I/Ads(29692): ... 14 more
03-12 10:32:59.251: I/Ads(29692): Caused by: java.lang.ClassNotFoundException: com.google.ads.mediation.adfalcon.AdFalconAdapter
03-12 10:32:59.251: I/Ads(29692): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
03-12 10:32:59.251: I/Ads(29692): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
03-12 10:32:59.251: I/Ads(29692): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
03-12 10:32:59.251: I/Ads(29692): ... 14 more
03-12 10:32:59.401: I/webclipboard(29692): clipservice: android.sec.clipboard.ClipboardExManager@41756900
03-12 10:32:59.421: V/(29692): NotificationPresenterImpl::setSettingsValue 0
03-12 10:32:59.421: V/(29692): NotificationPresenterImpl::setSettingsValue 0
03-12 10:32:59.461: V/(29692): NotificationPresenterImpl::setSettingsValue 0
03-12 10:32:59.461: V/(29692): NotificationPresenterImpl::setSettingsValue 0
03-12 10:32:59.571: I/webclipboard(29692): clipservice: android.sec.clipboard.ClipboardExManager@41756900
03-12 10:32:59.631: V/(29692): NotificationPresenterImpl::setSettingsValue 0
03-12 10:32:59.631: V/(29692): NotificationPresenterImpl::setSettingsValue 0
03-12 10:32:59.641: V/(29692): NotificationPresenterImpl::setSettingsValue 0
03-12 10:32:59.641: V/(29692): NotificationPresenterImpl::setSettingsValue 0
03-12 10:32:59.641: V/(29692): NotificationPresenterImpl::setSettingsValue 0
03-12 10:32:59.641: V/(29692): NotificationPresenterImpl::setSettingsValue 0
03-12 10:32:59.731: I/Ads(29692): To get test ads on this device, call adRequest.addTestDevice("E98E8A47D904C0A2B26B4C6086D15D31");
03-12 10:32:59.731: D/WML_SISO(29692): InitPasteboardJni
03-12 10:32:59.741: D/WML_SISO(29692): InitPasteboardJni
03-12 10:32:59.791: I/Ads(29692): adRequestUrlHtml: <html><head><script src="http://media.admob.com/sdk-core-v40.js"></script><script>AFMA_buildAdURL({"preqs":1,"session_id":"2391090699000457102","seq_num":"2","slotname":"ca-app-pub-6451932264844765\/2000735339\/qdata=AJ5UM1mTKdIGl-MVybKseLdwmpuMfGDwim_n1Rnxv-mBdOo_oLz2FNd8wG-PY_ofQs4x94NPQ1X7LP85Au8WtrFJUm7o-pJ4-MEXhBrcO5pEVBEDRw&allocation_ids=1931280969623834","u_w":320,"msid":"com.peky.smartornot","js":"afma-sdk-a-v6.4.1","bas_off":0,"imbf":"8009","net":"wi","app_name":"1.android.com.peky.smartornot","hl":"hr","gnt":0,"carrier":"21803","u_audio":1,"kw":[],"u_sd":1.5,"ms":"IMvf_JNXEuQ7-mQpjt4bQ_R3ro8ex-TIO5eSXXT42wW47vLO9Bn8Ka7zz9x4OCQoOxK8do2XGRTKyvuoZ5sbve6vDM0IqjAW5Kdq6qQITkLmdVPF1Q2zXKH6tGbWpm5hTWlDK6HH5S1d2XsZzk1_hEWfOKAf0DQiX41vp49AsnB_lOqNhj8C1DJLbD8LQe1ArC67TA8DQVwbPAY1Ea44SH3PsBe09eTxVkrkAw87CLNhJt3DY2N8b7Uz1gFaC61IDoONjJmyhdpezQjLQjDGfhbOyR13m_vpVLEtjf-V-r8vKxMIeF3CfDE8QR1q1bJBVzzncKK08_WlbYcOanTf5A","mv":"80250010.com.android.vending","isu":"E98E8A47D904C0A2B26B4C6086D15D31","format":"320x50_mb","oar":0,"extras":{"_norefresh":"t","gw":1},"u_h":533,"pt":1,"bas_on":0,"ptime":6862});</script></head><body></body></html>
03-12 10:33:00.271: I/Ads(29692): Received ad url: <url: "http://googleads.g.doubleclick.net:80/mads/gma?preqs=1&session_id=2391090699000457102&seq_num=2&u_w=320&msid=com.peky.smartornot&js=afma-sdk-a-v6.4.1&bas_off=0&imbf=8009&net=wi&app_name=1.android.com.peky.smartornot&hl=hr&gnt=0&carrier=21803&u_audio=1&kw&u_sd=1.5&ms=IMvf_JNXEuQ7-mQpjt4bQ_R3ro8ex-TIO5eSXXT42wW47vLO9Bn8Ka7zz9x4OCQoOxK8do2XGRTKyvuoZ5sbve6vDM0IqjAW5Kdq6qQITkLmdVPF1Q2zXKH6tGbWpm5hTWlDK6HH5S1d2XsZzk1_hEWfOKAf0DQiX41vp49AsnB_lOqNhj8C1DJLbD8LQe1ArC67TA8DQVwbPAY1Ea44SH3PsBe09eTxVkrkAw87CLNhJt3DY2N8b7Uz1gFaC61IDoONjJmyhdpezQjLQjDGfhbOyR13m_vpVLEtjf-V-r8vKxMIeF3CfDE8QR1q1bJBVzzncKK08_WlbYcOanTf5A&mv=80250010.com.android.vending&format=320x50_mb&oar=0&u_h=533&bas_on=0&ptime=6862&u_so=p&output=html®ion=mobile_app&u_tz=60&client_sdk=1&ex=1&client=ca-app-pub-6451932264844765&slotname=2000735339&qdata=AJ5UM1mTKdIGl-MVybKseLdwmpuMfGDwim_n1Rnxv-mBdOo_oLz2FNd8wG-PY_ofQs4x94NPQ1X7LP85Au8WtrFJUm7o-pJ4-MEXhBrcO5pEVBEDRw&allocation_ids=1931280969623834&kw_type=broad&askip=1&gsb=wi&caps=inlineVideo_interactiveVideo_mraid1_th_autoplay_mediation_sdkAdmobApiForAds_di&_norefresh=t&gw=1&eid=46621026&jsv=77&urll=1114" type: "admob" afmaNotifyDt: "null" activationOverlayUrl: "null" useWebViewLoadUrl: "false">
03-12 10:33:00.281: I/Ads(29692): Request scenario: Online server request.
03-12 10:33:00.281: I/GATE(29692): <GATE-M>DEV_ACTION_COMPLETED</GATE-M>
03-12 10:33:03.591: I/GATE(29692): <GATE-M>DEV_ACTION_COMPLETED</GATE-M>
03-12 10:33:03.591: D/webview(29692): webview.destroy() is called.
03-12 10:33:03.591: D/webviewglue(29692): nativeDestroy view: 0x2035d00
03-12 10:33:03.661: I/Ads(29692): onReceiveAd()
03-12 10:33:03.901: W/ResourceType(29692): No package identifier when getting value for resource number 0x00000000
03-12 10:33:03.901: D/AndroidRuntime(29692): Shutting down VM
03-12 10:33:03.901: W/dalvikvm(29692): threadid=1: thread exiting with uncaught exception (group=0x40c26a68)
03-12 10:33:03.931: E/AndroidRuntime(29692): FATAL EXCEPTION: main
03-12 10:33:03.931: E/AndroidRuntime(29692): java.lang.IllegalStateException: Could not execute method of the activity
03-12 10:33:03.931: E/AndroidRuntime(29692): at android.view.View$1.onClick(View.java:3099)
03-12 10:33:03.931: E/AndroidRuntime(29692): at android.view.View.performClick(View.java:3571)
03-12 10:33:03.931: E/AndroidRuntime(29692): at android.view.View$PerformClick.run(View.java:14247)
03-12 10:33:03.931: E/AndroidRuntime(29692): at android.os.Handler.handleCallback(Handler.java:605)
03-12 10:33:03.931: E/AndroidRuntime(29692): at android.os.Handler.dispatchMessage(Handler.java:92)
03-12 10:33:03.931: E/AndroidRuntime(29692): at android.os.Looper.loop(Looper.java:137)
03-12 10:33:03.931: E/AndroidRuntime(29692): at android.app.ActivityThread.main(ActivityThread.java:4517)
03-12 10:33:03.931: E/AndroidRuntime(29692): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 10:33:03.931: E/AndroidRuntime(29692): at java.lang.reflect.Method.invoke(Method.java:511)
03-12 10:33:03.931: E/AndroidRuntime(29692): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
03-12 10:33:03.931: E/AndroidRuntime(29692): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
03-12 10:33:03.931: E/AndroidRuntime(29692): at dalvik.system.NativeStart.main(Native Method)
03-12 10:33:03.931: E/AndroidRuntime(29692): Caused by: java.lang.reflect.InvocationTargetException
03-12 10:33:03.931: E/AndroidRuntime(29692): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 10:33:03.931: E/AndroidRuntime(29692): at java.lang.reflect.Method.invoke(Method.java:511)
03-12 10:33:03.931: E/AndroidRuntime(29692): at android.view.View$1.onClick(View.java:3094)
03-12 10:33:03.931: E/AndroidRuntime(29692): ... 11 more
03-12 10:33:03.931: E/AndroidRuntime(29692): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
03-12 10:33:03.931: E/AndroidRuntime(29692): at android.content.res.Resources.getText(Resources.java:260)
03-12 10:33:03.931: E/AndroidRuntime(29692): at android.widget.TextView.setText(TextView.java:3680)
03-12 10:33:03.931: E/AndroidRuntime(29692): at com.peky.smartornot.Redeemcode.provjeri(Redeemcode.java:32)
03-12 10:33:03.931: E/AndroidRuntime(29692): ... 14 more
03-12 10:33:38.361: I/Process(29692): Sending signal. PID: 29692 SIG: 9
答案 0 :(得分:1)
来自logcat:
03-12 10:33:03.931: E/AndroidRuntime(29692): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
03-12 10:33:03.931: E/AndroidRuntime(29692): at android.content.res.Resources.getText(Resources.java:260)
03-12 10:33:03.931: E/AndroidRuntime(29692): at android.widget.TextView.setText(TextView.java:3680)
03-12 10:33:03.931: E/AndroidRuntime(29692): at com.peky.smartornot.Redeemcode.provjeri(Redeemcode.java:32)
因此,在您的provjeri()
中,您尝试使用资源ID为0的setText()
。
来自代码:
public void provjeri(View view){
//...
TextView proba=(TextView)findViewById(R.id.povrat);
//...
int kolicina=5,brojac=0;
//...
proba.setText(brojac);
使用例如
proba.setText("" + brojac);
因此使用setText(CharSequence)
代替期望资源ID的setText(int)
。