尝试从数据库中检索时的resourcenotfoundexception

时间:2014-04-21 08:56:50

标签: android

我正在尝试从表中检索整数价格值。当我尝试传递一个字符串值时,我得到一个强制关闭并带有异常:

package com.example.shopkart;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class giftpage extends Activity {
    TextView txtfinalbill;
    Button btnshowtotal;
    String name,mailid;
    datamanager dm;
    String[] products,quantity;
    int total=0,price1;
    int[] price;
    String pricelist;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.giftpage);
        dm=new datamanager(this);
        name=getIntent().getExtras().getString("name");
        mailid=getIntent().getExtras().getString("mailid");
        txtfinalbill=(TextView) findViewById(R.id.txtfinalbill);
        btnshowtotal=(Button) findViewById(R.id.btnshowtotal);
        btnshowtotal.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                products=dm.getpurchaseproducts(mailid);
            quantity=dm.getpurchasequantity(mailid);
            price1=dm.getprice("Sony ps4");
            Toast.makeText(getApplicationContext(),price1,Toast.LENGTH_LONG).show();
//          for(int i=0;i<products.length;i++)
//          {
//              price[i]=dm.getprice(products[i]);
//              Toast.makeText(getApplicationContext(),price[i],Toast.LENGTH_LONG).show();
//          }





            }
        });

}

}

dm.getprice()是问题..

public int getprice(String product)
    {   
        Cursor cu=DB1.rawQuery("SELECT PRICE from products where NAME='"+product+"'",null);  
        cu.moveToFirst();


        if (!cu.isAfterLast())
        {
            do
            {


                price=cu.getInt(0);




            }

            while (cu.moveToNext());
            cu.close();
        }
        return price;

    }

logcat

04-21 04:52:16.255: E/AndroidRuntime(3340): FATAL EXCEPTION: main
04-21 04:52:16.255: E/AndroidRuntime(3340): Process: com.example.shopkart, PID: 3340
04-21 04:52:16.255: E/AndroidRuntime(3340): android.content.res.Resources$NotFoundException: String resource ID #0x9c36
04-21 04:52:16.255: E/AndroidRuntime(3340):     at android.content.res.Resources.getText(Resources.java:244)
04-21 04:52:16.255: E/AndroidRuntime(3340):     at android.widget.Toast.makeText(Toast.java:268)
04-21 04:52:16.255: E/AndroidRuntime(3340):     at com.example.shopkart.giftpage$1.onClick(giftpage.java:42)
04-21 04:52:16.255: E/AndroidRuntime(3340):     at android.view.View.performClick(View.java:4424)
04-21 04:52:16.255: E/AndroidRuntime(3340):     at android.view.View$PerformClick.run(View.java:18383)
04-21 04:52:16.255: E/AndroidRuntime(3340):     at android.os.Handler.handleCallback(Handler.java:733)
04-21 04:52:16.255: E/AndroidRuntime(3340):     at android.os.Handler.dispatchMessage(Handler.java:95)
04-21 04:52:16.255: E/AndroidRuntime(3340):     at android.os.Looper.loop(Looper.java:137)
04-21 04:52:16.255: E/AndroidRuntime(3340):     at android.app.ActivityThread.main(ActivityThread.java:4998)
04-21 04:52:16.255: E/AndroidRuntime(3340):     at java.lang.reflect.Method.invokeNative(Native Method)
04-21 04:52:16.255: E/AndroidRuntime(3340):     at java.lang.reflect.Method.invoke(Method.java:515)
04-21 04:52:16.255: E/AndroidRuntime(3340):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
04-21 04:52:16.255: E/AndroidRuntime(3340):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
04-21 04:52:16.255: E/AndroidRuntime(3340):     at dalvik.system.NativeStart.main(Native Method)

请帮忙,我在这里尝试传递字符串值(Sony ps4)而不是检索值。

2 个答案:

答案 0 :(得分:3)

改变这个:

Toast.makeText(getApplicationContext(),price1,Toast.LENGTH_LONG).show();

到此:

Toast.makeText(getApplicationContext(),price1+"",Toast.LENGTH_LONG).show();

Toast在邮件中接受String参数,并且您正在传递int

答案 1 :(得分:0)

我注意到了一些事情: -

  1. 当您访问onclick时,我无法在主体中看到dm.open()功能 活动或onclick我总是在open() close()onclick()数据库连接,因为它运行 活动加载后。

  2. 如果你打开了连接,我会建议删除所有函数并创建db的对象引用,以便为dat创建像void这样的函数

    onlyLog(){Log.e("Testing Referencing","Succeeded");}
    
    数据管理器和onclick中的

    只需调用此dm.onlyLog(); 这样你就可以确定问题是引用,连接还是功能。

  3. 另见@dubey答案。

  4. 希望有所帮助