简体   繁体   English

Android字符串资源未找到异常

[英]Android String Resource not found Exception

So in my app i have an edit text that gets changed depending on the click of a button inside my list view. 所以在我的应用程序中,我有一个编辑文本,根据列表视图中的按钮单击更改。 To do this i have used an interface and custom adapter. 为此,我使用了接口和自定义适配器。 The app launches fine but when i try to click a button the app crashes and gives a 'resource not found exception'. 该应用程序启动正常,但当我尝试单击按钮时,应用程序崩溃并提供“资源未找到异常”。 Can anyone help fix this error? 任何人都可以帮助修复此错误?

My main activity 我的主要活动

package com.example.rory.dbtest;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.pinchtapzoom.R;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;

public class MyActivity extends Activity implements MyAdapterInterface{

    private CustomCursorAdapter customAdapter;
    public ListView list1;

    com.example.rory.dbtest.DBAdapter db = new com.example.rory.dbtest.DBAdapter(this);
    public MyActivity mMyActivity;


    //public EditText TotalCost;
    //public EditText TotalLitres;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        list1 = (ListView)findViewById(R.id.data_list);
        db.open();

        mMyActivity = this;

        Button addBtn = (Button)findViewById(R.id.add);
        addBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(MyActivity.this, addassignment.class);
                startActivity(i);
            }
        });

        Button deleteBtn = (Button)findViewById(R.id.delete);
        deleteBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(MyActivity.this, Delete.class);
                startActivity(i);
            }
        });

        Button updateBtn = (Button)findViewById(R.id.update);
        updateBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent i = new Intent(MyActivity.this, Update.class);
                startActivity(i);
            }
        });


        try {
            String destPath = "/data/data/" + getPackageName() + "/databases/AssignmentDB";
            File f = new File(destPath);
            if (!f.exists()) {
                CopyDB( getBaseContext().getAssets().open("mydb"),
                        new FileOutputStream(destPath));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        new Handler().post(new Runnable() {
            @Override
            public void run() {
                //Log.d("test", "customadapter is " + customAdapter.toString());
                //Log.d("test", "databaseHelper is " + databaseHelper.toString());
                customAdapter = new CustomCursorAdapter(MyActivity.this, db.getAllRecords(), mMyActivity);
                list1.setAdapter(customAdapter);
            }
        });

    }

    public void updateEditText(int value)
    {
        EditText myEditText = (EditText)findViewById(R.id.edit1);
        myEditText.setText(value);
    }

    private class DBAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        //private ArrayList<>

        @Override
        public int getCount() {

            return 0;
        }

        @Override
        public Object getItem(int arg0) {

            return null;
        }

        @Override
        public long getItemId(int arg0) {

            return 0;
        }

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {

            return null;
        }

    }

    public void CopyDB(InputStream inputStream, OutputStream outputStream)
            throws IOException {
        //---copy 1K bytes at a time---
        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, length);
        }
        inputStream.close();
        outputStream.close();
    }
}

My custom adapter 我的定制适配器

package com.example.rory.dbtest;

/**
 * Created by Rory on 02/11/2014.
 */
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.pinchtapzoom.R;


public class CustomCursorAdapter extends CursorAdapter{

    public int counter = 0;
    private MyAdapterInterface mMyInterface;

    public CustomCursorAdapter(Context context, Cursor c, MyAdapterInterface myInterface) {

        super(context, c);
        this.context = context;
        this.mMyInterface = myInterface;
    }
    Context context;


    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        // when the view will be created for first time,
        // we need to tell the adapters, how each item will look
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View retView = inflater.inflate(R.layout.row, parent, false);

        return retView;
    }

    public void bindView(View view, Context context, Cursor cursor) {
        // here we are setting our data
        // that means, take the data from the cursor and put it in views
        TextView textViewPersonName = (TextView) view.findViewById(R.id.item1);
        textViewPersonName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1))));

        final EditText edit1 = (EditText) view.findViewById(R.id.runningTotal);

        Button plusButton = (Button) view.findViewById(R.id.plusButton);
        plusButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                counter++;
                edit1.setText(Integer.toString(counter));
                mMyInterface.updateEditText(counter);
            }
        });

        final Button minusButton = (Button) view.findViewById(R.id.minusButton);
        minusButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                counter--;
                edit1.setText(Integer.toString(counter));
                mMyInterface.updateEditText(counter);
            }
        });
    }
}

My logcat error 我的logcat错误

Process: com.example.rory.dbtest, PID: 32185
android.content.res.Resources$NotFoundException: String resource ID #0xffffffff
        at android.content.res.Resources.getText(Resources.java:244)
        at android.widget.TextView.setText(TextView.java:3888)
        at com.example.rory.dbtest.MyActivity.updateEditText(MyActivity.java:112)
        at com.example.rory.dbtest.CustomCursorAdapter$2.onClick(CustomCursorAdapter.java:66)
        at android.view.View.performClick(View.java:4438)

The problem is that you're trying to set an int to your editText which needs to be a string. 问题是你正在尝试为你的editText设置一个int,它需要是一个字符串。 A simple solution is to change this line 一个简单的解决方案是改变这一行

myEditText.setText(value);

To this 对此

myEditText.setText(String.valueOf(value));

As pointed out by the 2 previous commenters. 正如前两位评论者所指出的那样。

Your variable counter is an int, you can't set the EditText text to an int , it needs to be a String. 您的变量counter是一个int,您不能将EditText文本设置为int ,它需要是一个String。


Use edit1.setText(String.valueOf(counter)); 使用edit1.setText(String.valueOf(counter)); and myEditText.setText(String.valueOf(value)); myEditText.setText(String.valueOf(value));

In

public void updateEditText(int value)
{
    EditText myEditText = (EditText)findViewById(R.id.edit1);
    myEditText.setText(value);
}

the second line gets an int, which is supposed to be an Id stored in the R.string 第二行得到一个int,它应该是存储在R.string中的Id

As @zgc7009 said, you need to change 正如@ zgc7009所说,你需要改变

myEditText.setText(value);

to

myEditText.setText(String.valueOf(value));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM