mercredi 23 décembre 2015

Fatal exception show error unable to start activity component when i'm running this app

I'm new to Android app development and right now I'm trying to make a simple display all record from sqlite database in listview app. The compiler doesn't detect any errors but when run it gets a runtime errors.
main_Activity.java this is mainactivity class package com.example.login_userauthentication;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

public void Register(View v)
{
    startActivity(new Intent(getApplicationContext(),Reg.class));
}
public void Login(View v)
{
    startActivity(new Intent(getApplicationContext(),Login.class));
}

public void Show_allrecord(View v)
{
    startActivity(new Intent(getApplicationContext(),Show_record.class));
}
public void update_record(View v)
{
    startActivity(new Intent(getApplicationContext(),Update.class));
}


    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}



Show_Record.java(this is main class that is show all the userdetailed in listview)

package com.example.login_userauthentication;

import java.util.ArrayList;
import java.util.List;



import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ListView;

public class Show_record extends Activity
{


    Show_recordAdapter showrecord_adapter;
    ListView listview;
    ArrayList<Student> studentlist;
    Database helper=new Database();
    protected void onCreate(Bundle savedInstanceState)
    {

        System.out.println("show sumit");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_recordlist);
        System.out.println("show_record activity");
        System.out.println("show sumit");
        helper=new Database();
        listview = (ListView) findViewById(R.id.listView1);
        studentlist=new ArrayList<Student>();
        studentlist = helper.getAllrecord();
        System.out.println(studentlist);
        System.out.println("show sumit");
        showrecord_adapter=new Show_recordAdapter(getApplicationContext(),studentlist);
        listview.setAdapter(showrecord_adapter);

//      listview.setAdapter(new Show_recordAdapter(this, studentlist,
//              getLayoutInflater()));

    }


}



package com.example.login_userauthentication;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

show_recordAdapter.java

this is the custom adapter class

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class Show_recordAdapter extends BaseAdapter 
{
    Context context;
    ArrayList<Student> empList;
    private static LayoutInflater inflater = null;


      public Show_recordAdapter(Context context,ArrayList<Student> empList)
      {
          context=this.context;
          empList=this.empList;
          inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

      }

      public int getCount() {
        // TODO Auto-generated method stub
        return empList.size();
    }

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        //final ViewItem item;
        if(convertView==null)

            convertView = inflater.inflate(R.layout.listview_iteams, null);
        //item=new ViewItem();
        TextView UemailTextview=(TextView)convertView.findViewById(R.id.uemail);
        TextView upassTextview=(TextView)convertView.findViewById(R.id.upass);
        TextView UnameTextview=(TextView)convertView.findViewById(R.id.uname);
        TextView UmobileTextview=(TextView)convertView.findViewById(R.id.umobile);
//      convertView.setTag(item);   
//      }
//      else
//      {
//          item=(ViewItem)convertView.getTag();
//      }

        Student student=new Student();
        student=empList.get(position);
        UnameTextview.setText("emailid: "+student.getEmailid());
        upassTextview.setText("password: "+student.getPassword());
        UnameTextview.setText("name: "+student.getName());
        UmobileTextview.setText("mobile: "+student.getMobileno());
        return convertView;
    }



//  private class ViewItem
//  {
//      TextView UemailTextview;
//      TextView upassTextview;
//      TextView UnameTextview;
//      TextView UmobileTextview;
//  }
}

Databaseclass(in which defined all the filelds of table and also defined methods)
package com.example.login_userauthentication;



import java.util.ArrayList;
import java.util.List;







import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.provider.SyncStateContract.Helpers;
import android.util.Log;



public class Database
{
    public static final String TABLE_NAME = "student";
    public static final String USER_EMAILID= "emailid";
    public static final String USER_PASSWORD = "password";
    public static final String USER_NAME = "name";
    public static final String USER_CONTACT = "mobileno";
    public static final String Tag = "jobplacement";


    public static Databse_helper dbhelper = null;
    public static SQLiteDatabase db = null;
    static Context context1 = null;    
    public static boolean addUser(Context context, String email, String password, String name,String mobile)
    {
        context1=context;
        try {
        dbhelper=new Databse_helper(context);
        db=dbhelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(Database.USER_EMAILID,email);
        values.put(Database.USER_PASSWORD,password);
        values.put(Database.USER_NAME,name);
        values.put(Database.USER_CONTACT,mobile);
        long rowid = db.insert(Database.TABLE_NAME, null, values);
        Log.d("Users", "Inserted into TRANSACTIONS " + rowid);
        db.close();
        return true;
        } catch (Exception e) {
            // TODO: handle exception
        }
        return false;
    }

    public static boolean userAuthentication(Context context, String uname, String password) {

        Cursor trans = null;
        try {
            dbhelper=new Databse_helper(context);
            db = dbhelper.getWritableDatabase();
            trans = db.rawQuery("select " + USER_PASSWORD + " from " + TABLE_NAME + " where " + USER_EMAILID
                    + " = '" + uname + "'", null);
            if (trans.getCount() == 0) 
            {
                Log.d("userAuthentication", "Zero record");
            } 

            else 
            {
                while (trans.moveToNext()) 
                {
                    String pass = trans.getString(trans.getColumnIndex(USER_PASSWORD));
                    if (password.equals(pass))
                    {
                        return true;
                    } else {
                        return false;
                    }
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
        return false;
    }


    public ArrayList<Student> getAllProducts() 
    {


        db = dbhelper.getWritableDatabase();
        List<Student> productList = new ArrayList<Student>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + Database.TABLE_NAME;
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Student st = new Student();
                st.setEmailid(cursor.getString(0));
                st.setPassword(cursor.getString(1));
                st.setName(cursor.getString(2));
                st.setMobileno(cursor.getString(3));
//              product.setID(Integer.parseInt(cursor.getString(0)));
//              product.setName(cursor.getString(1));
//              product.setNumber(Integer.parseInt(cursor.getString(2)));
//              product.setPieces(Integer.parseInt(cursor.getString(3)));
//              product.setPrice(Integer.parseInt(cursor.getString(4)));
//              product.setTotPrice(Integer.parseInt(cursor.getString(5)));
                // Adding contact to list
                productList.add(st);
            } while (cursor.moveToNext());
        }

        // return contact list
        return (ArrayList<Student>) productList;
    }
      public static boolean  updateEntry(Student st)
        {

            db=dbhelper.getWritableDatabase();
            // Define the updated row content.
            ContentValues updatedValues = new ContentValues();
            // Assign values for each row.
            updatedValues.put(USER_EMAILID, st.getEmailid());
            updatedValues.put(USER_NAME,st.getName());
            updatedValues.put(USER_CONTACT, st.getMobileno());

            //String where=Database.USER_EMAILID +"=+?";
           // String where="emailid +=?";
             int row_id=db.update(Database.TABLE_NAME,updatedValues, "emailid=?", new String[]{st.getEmailid()});
             Log.d("login", "updated record " +row_id);
            return true;
        }


      public ArrayList<Student> getAllrecord()
        {
            dbhelper=new Databse_helper(context1);
            db=dbhelper.getWritableDatabase();
            List<Student> emplist=new ArrayList<Student>();
            String rowquery="select * from "+TABLE_NAME;
            Cursor cursor=db.rawQuery(rowquery, null);
            if (cursor.moveToFirst()) 
            {
                do 
                {   Student st = new Student();
                st.setEmailid(cursor.getString(0));
                st.setPassword(cursor.getString(1));
                st.setName(cursor.getString(2));
                st.setMobileno(cursor.getString(3));
                    // Adding Translate to list

                    Student emp1=new Student();
                   emplist.add(st);
                } 
                while (cursor.moveToNext());
            }

            // return Translate list
            return (ArrayList<Student>) emplist;




       }      



}


Database_helper.java(in which defined create table and create database)
package com.example.login_userauthentication;



import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class Databse_helper extends SQLiteOpenHelper
{
    static final int DATABASE_VERSION = 2;
    static final String DATABASE_NAME = "collage.db";
    public static final String Tag = "jobplacement";

    public Databse_helper(Context cxt) 
    {
        super(cxt, DATABASE_NAME, null, DATABASE_VERSION);
        Log.i(Tag, "DBHelper class constructer called");
        Log.i(Tag, "Database Created Successfully");
    }
    public void onCreate(SQLiteDatabase db) 
    {
        createTables(db);

    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {
         db.execSQL("DROP TABLE IF EXISTS " + Database.TABLE_NAME);

            // Create tables again
            onCreate(db);

    }
    private void createTables(SQLiteDatabase database)
    {

        String users_table_sql = "create table " + Database.TABLE_NAME + " ( " + Database.USER_EMAILID
                + " TEXT," + Database.USER_PASSWORD + " TEXT," + Database.USER_NAME
                + " TEXT," + Database.USER_CONTACT + " TEXT)";
     try {
     database.execSQL(users_table_sql);
     Log.d(Tag, "table created successfully");

//     protected static final String player = ("CREATE TABLE " + table_player
//              + " (" + player_id + " INTEGER PRIMARY KEY AUTOINCREMENT, "
//              + player_name + " TEXT, " + player_dob + " INTEGER," + player_gender + " TEXT);");
//     
//     
//     "CREATE TABLE " + TABLE_PRODUCTS + "("
//     + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
//     + KEY_NO + " TEXT," + KEY_PIECES + " TEXT,"+ KEY_PRICE + " TEXT," + KEY_TOT_PRICE + " TEXT" + ")";

        } catch (Exception ex)
        {
            Log.d("Accounts", "Error in DBHelper.onCreate() : " + ex.getMessage());
        }
    }



}

Aucun commentaire:

Enregistrer un commentaire