如何在Android中解决ResourcesNotFoundException

时间:2013-07-08 12:32:32

标签: android

这是我的代码,我有一个问题,关于ResourcesNotFoundException的错误消息这是查看员工的一段代码,它有2个按钮,一个要删除,另一个要做更新。

我不知道这个例外的原因。

package com.example.task_9;

import java.util.ArrayList;
import com.example.task_9.my_dataBase.employees_con;
import com.example.task_9.my_dataBase.my_database;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class manage extends Activity {

    ListView listview;

    TextView totalrecords,tv;
    //my_database db;
    public ArrayList<employees> emp_list ;

     employees_con con;
        //Button   logout;


    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();    
        setContentView(R.layout.manage);

        tv = (TextView) findViewById(R.id.textView1);
        tv.setText("Employees Record:");
        totalrecords = (TextView) findViewById(R.id.textView2);
        listview = (ListView) findViewById(R.id.listView1);
          // logout=(Button) findViewById(R.id.button1);

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
         emp_list = new ArrayList<employees>();
        emp_list.clear();
        con = new employees_con(this);  
    //  db = new my_database(getApplicationContext());
    //  db.getWritableDatabase();
        ArrayList<employees> e_list =con.getAllData() ;


        for (int i = 0; i < e_list.size(); i++) {
            Log.d("for_getall ", "ok "); 

            int e_id = e_list.get(i).getId();
            Log.d("fun_getall ", "ok "); 
        //  System.out.println("tidno>>>>>" + tidno);
            String e_name = e_list.get(i).getUser_name();
            String e_password = e_list.get(i).getPassword();
            int e_age = e_list.get(i).getAge();
            int e_status = e_list.get(i).getStatus();
            Log.d("fun_getall ", "ok "); 
            employees emp_model = new employees();

            emp_model.setId(e_id);
            emp_model.setUser_name(e_name);
            emp_model.setPassword(e_password);
            emp_model.setAge(e_age);
            emp_model.setStatus(e_status);
            Log.d("addlist ", "ok "); 
            emp_list.add(emp_model);

        }
        Log.d("out for ", "ok "); 
        totalrecords.setText("Total Records :-" + emp_list.size());
        listview.setAdapter(new ListAdapter(this));
        con.close();

    }

    private class ListAdapter extends BaseAdapter {
        //ArrayList<employees> data=new ArrayList<employees>();
        Context contxt;

        TextView id;
        TextView user_name;
        TextView password;
        TextView age;
        TextView status;
        ImageView img;

        employees obj;
        LayoutInflater inflater;

        public ListAdapter(Context context) {
            // TODO Auto-generated constructor stub
            contxt=context;
            inflater = LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return emp_list.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View v, ViewGroup parent) {

            // TODO Auto-generated method stub
            /*if (v == null) {

                v = inflater.inflate(R.layout.view, null);}*/

              if (v == null)      
              {         
                  LayoutInflater vi = (LayoutInflater)contxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);   
              v = vi.inflate(R.layout.view, null);  // album_list.xml this layout of the custom listview
                Log.d("view if", "ok "); 

              }

             id=(TextView) v.findViewById(R.id.textView6);
             user_name=(TextView) v.findViewById(R.id.textView7);
             password=(TextView) v.findViewById(R.id.textView8);
             age=(TextView) v.findViewById(R.id.textView9);
             status=(TextView) v.findViewById(R.id.textView10);
             img =(ImageView) v.findViewById(R.id.imageView1);

                Log.d("out if", "ok "); 

                  obj = emp_list.get(position);
            user_name.setText(obj.getUser_name());
            id.setText(obj.getId());    
            password.setText(obj.getPassword());
            age.setText(obj.getAge());
            status.setText(obj.getStatus());
            Log.d("view  else ", "ok ");
            final int temp = position;
            (v.findViewById(R.id.button1))
                    .setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
                            int emp_id=emp_list.get(temp).getId();
                            String emp_name = emp_list.get(temp).getUser_name();

                            String emp_password = emp_list.get(temp).getPassword();
                            int emp_age=emp_list.get(temp).getAge();
                            int emp_status=emp_list.get(temp).getStatus();

                            Intent intent = new Intent(manage.this,update.class);


                            Bundle bundle = new Bundle();
                            bundle.putInt("id", emp_id);
                            bundle.putString("name", emp_name);
                            bundle.putString("password", emp_password);
                            bundle.putInt("age", emp_age);
                            bundle.putInt("status", emp_status);

                            intent.putExtras(bundle);
                            startActivity(intent);

                        }
                    });

            (v.findViewById(R.id.button2))
                    .setOnClickListener(new OnClickListener() {
                        employees_con con=new employees_con(contxt);

                        public void onClick(View arg0) {

                            AlertDialog.Builder alertbox = new AlertDialog.Builder(manage.this);

                            alertbox.setCancelable(true);
                            alertbox.setMessage("Are you sure you want to delete ?");
                            alertbox.setPositiveButton("Yes",
                                    new DialogInterface.OnClickListener() {

                                        public void onClick(
                                                DialogInterface arg0, int arg1) {

                                            con.DeleteEmp(emp_list.get(temp).getUser_name().toString());
                                            con.close();

                                            manage.this.onResume();

                                            Toast.makeText(
                                                    getApplicationContext(),
                                                    "Record Deleted...",
                                                    Toast.LENGTH_SHORT).show();

                                        }

                                    });
                            alertbox.setNegativeButton("No",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(
                                                DialogInterface arg0, int arg1) {
                                            Toast.makeText(
                                                    getApplicationContext(),
                                                    "No Clicked...",
                                                    Toast.LENGTH_SHORT).show();


                                        }
                                    });
                            alertbox.show();
                        }
                    });

            return v;
    }

    }

}
}

2 个答案:

答案 0 :(得分:2)

更改

  id.setText(obj.getId()); 

  id.setText(String.valueOf(obj.getId()));    

如果您传递一个int作为TextView的参数,那么android会查找一个String,其id为您提供的int。如果未找到则抛​​出异常

答案 1 :(得分:0)

一个重要的是,您应该使用Activity.onCreate()方法代替Activity.onStart()