2012-02-14 2 views
0

Я следовал за репетитором, где обнаружил, что мое приложение приближается к действию, хотя я правильно следил за инструктором. Вот мой файл,неожиданное закрытие силы в моей заявке

EmployeeList.java

package samples.employeedirectory; 

import android.app.ListActivity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleCursorAdapter; 

public class EmployeeList extends ListActivity { 

     protected EditText searchText; 
     protected SQLiteDatabase db; 
     protected Cursor cursor; 
     protected ListAdapter adapter; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     db = (new DatabaseHelper(this)).getWritableDatabase(); 
     searchText = (EditText) findViewById (R.id.searchText); 
    } 

    public void search(View view) { 
     // || is the concatenation operation in SQLite 
       cursor = db.rawQuery("SELECT _id, firstName, lastName, title FROM employee WHERE firstName || ' ' || lastName LIKE ?", 
               new String[]{"%" + searchText.getText().toString() + "%"}); 
       adapter = new SimpleCursorAdapter(
           this, 
           R.layout.employee_list_item, 
           cursor, 
           new String[] {"firstName", "lastName", "title"}, 
           new int[] {R.id.firstName, R.id.lastName, R.id.title}); 
       setListAdapter(adapter); 
    } 

    public void onListItemClick(ListView parent, View view, int position, long id) { 
     Intent intent = new Intent(this, EmployeeDetails.class); 
     Cursor cursor = (Cursor) adapter.getItem(position); 
     intent.putExtra("EMPLOYEE_ID", cursor.getInt(cursor.getColumnIndex("_id"))); 
     startActivity(intent); 
    } 

} 

**EmployeeDetails.java** 


package samples.employeedirectory; 

import android.app.Activity; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.os.Bundle; 
import android.widget.TextView; 

public class EmployeeDetails extends Activity { 

     protected TextView employeeName; 
     protected TextView employeeTitle; 
     protected TextView officePhone; 
     protected TextView cellPhone; 
     protected TextView email; 
    protected int employeeId; 

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

     employeeId = getIntent().getIntExtra("EMPLOYEE_ID", 0); 
     SQLiteDatabase db = (new DatabaseHelper(this)).getWritableDatabase(); 
     Cursor cursor = db.rawQuery("SELECT emp._id, emp.firstName, emp.lastName, emp.title, emp.officePhone, emp.cellPhone, emp.email, emp.managerId, mgr.firstName managerFirstName, mgr.lastName managerLastName FROM employee emp LEFT OUTER JOIN employee mgr ON emp.managerId = mgr._id WHERE emp._id = ?", 
           new String[]{""+employeeId}); 

     if (cursor.getCount() == 1) 
     { 
       cursor.moveToFirst(); 

       employeeName = (TextView) findViewById(R.id.employeeName); 
       employeeName.setText(cursor.getString(cursor.getColumnIndex("firstName")) + " " + cursor.getString(cursor.getColumnIndex("lastName"))); 

       employeeTitle = (TextView) findViewById(R.id.title); 
       employeeTitle.setText(cursor.getString(cursor.getColumnIndex("title"))); 

       officePhone = (TextView) findViewById(R.id.officePhone); 
       officePhone.setText(cursor.getString(cursor.getColumnIndex("officePhone"))); 

       cellPhone = (TextView) findViewById(R.id.cellPhone); 
       cellPhone.setText(cursor.getString(cursor.getColumnIndex("cellPhone"))); 

       email = (TextView) findViewById(R.id.email); 
       email.setText(cursor.getString(cursor.getColumnIndex("email"))); 

     } 

    } 

} 
**DatabaseHelper.java** 

package samples.employeedirectory; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

public class DatabaseHelper extends SQLiteOpenHelper { 

     public static final String DATABASE_NAME = "employee_directory"; 

     public DatabaseHelper(Context context) { 
       super(context, DATABASE_NAME, null, 1); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
       /* 
       * Create the employee table and populate it with sample data. 
       * In step 6, we will move these hardcoded statements to an XML document. 
       */ 
       String sql = "CREATE TABLE IF NOT EXISTS employee (" + 
               "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + 
               "firstName TEXT, " + 
               "lastName TEXT, " + 
               "title TEXT, " + 
               "officePhone TEXT, " + 
               "cellPhone TEXT, " + 
               "email TEXT, " + 
               "managerId INTEGER)"; 
       db.execSQL(sql); 

       ContentValues values = new ContentValues(); 

       values.put("firstName", "John"); 
       values.put("lastName", "Smith"); 
       values.put("title", "CEO"); 
       values.put("officePhone", "617-219-2001"); 
       values.put("cellPhone", "617-456-7890"); 
       values.put("email", "[email protected]"); 
       db.insert("employee", "lastName", values); 

       values.put("firstName", "Robert"); 
       values.put("lastName", "Jackson"); 
       values.put("title", "VP Engineering"); 
       values.put("officePhone", "617-219-3333"); 
       values.put("cellPhone", "781-444-2222"); 
       values.put("email", "[email protected]"); 
       values.put("managerId", "1"); 
       db.insert("employee", "lastName", values); 

       values.put("firstName", "Marie"); 
       values.put("lastName", "Potter"); 
       values.put("title", "VP Sales"); 
       values.put("officePhone", "617-219-2002"); 
       values.put("cellPhone", "987-654-3210"); 
       values.put("email", "[email protected]"); 
       values.put("managerId", "1"); 
       db.insert("employee", "lastName", values); 

       values.put("firstName", "Lisa"); 
       values.put("lastName", "Jordan"); 
       values.put("title", "VP Marketing"); 
       values.put("officePhone", "617-219-2003"); 
       values.put("cellPhone", "987-654-7777"); 
       values.put("email", "[email protected]"); 
       values.put("managerId", "2"); 
       db.insert("employee", "lastName", values); 

       values.put("firstName", "Christophe"); 
       values.put("lastName", "Coenraets"); 
       values.put("title", "Evangelist"); 
       values.put("officePhone", "617-219-0000"); 
       values.put("cellPhone", "617-666-7777"); 
       values.put("email", "[email protected]"); 
       values.put("managerId", "2"); 
       db.insert("employee", "lastName", values); 

       values.put("firstName", "Paula"); 
       values.put("lastName", "Brown"); 
       values.put("title", "Director Engineering"); 
       values.put("officePhone", "617-612-0987"); 
       values.put("cellPhone", "617-123-9876"); 
       values.put("email", "[email protected]"); 
       values.put("managerId", "2"); 
       db.insert("employee", "lastName", values); 

       values.put("firstName", "Mark"); 
       values.put("lastName", "Taylor"); 
       values.put("title", "Lead Architect"); 
       values.put("officePhone", "617-444-1122"); 
       values.put("cellPhone", "617-555-3344"); 
       values.put("email", "[email protected]"); 
       values.put("managerId", "2"); 
       db.insert("employee", "lastName", values); 

     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
       db.execSQL("DROP TABLE IF EXISTS employees"); 
       onCreate(db); 
     } 

} 

and my XML files, 

**main.xml** 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <EditText 
      android:id="@+id/searchText" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 

     <Button 
      android:id="@+id/searchButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Search" /> 
    </LinearLayout> 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

</LinearLayout> 

**employee_list_item.xml** 

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    android:padding="8px" > 

    <TextView 
     android:id="@+id/firstName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/lastName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="6px" 
     android:layout_toRightOf="@id/firstName" /> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/firstName" /> 

</RelativeLayout> 

**employee_details.xml** 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

     <TextView 
       android:id="@+id/employeeName" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
     <TextView 
       android:id="@+id/title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
     <TextView 
       android:id="@+id/officePhone" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
     <TextView 
       android:id="@+id/cellPhone" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
     <TextView 
       android:id="@+id/email" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 

</LinearLayout> 

и мой LogCat показывает следующее,

02-14 15:15:58.254: I/jdwp(599): Ignoring second debugger -- accepting and dropping 
02-14 15:15:59.443: D/AndroidRuntime(599): Shutting down VM 
02-14 15:15:59.454: W/dalvikvm(599): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
02-14 15:15:59.503: E/AndroidRuntime(599): FATAL EXCEPTION: main 
02-14 15:15:59.503: E/AndroidRuntime(599): java.lang.RuntimeException: Unable to start activity ComponentInfo{samples.employeedirectory/samples.employeedirectory.EmployeeList}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 
02-14 15:15:59.503: E/AndroidRuntime(599): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
02-14 15:15:59.503: E/AndroidRuntime(599): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
02-14 15:15:59.503: E/AndroidRuntime(599): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
02-14 15:15:59.503: E/AndroidRuntime(599): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
02-14 15:15:59.503: E/AndroidRuntime(599): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-14 15:15:59.503: E/AndroidRuntime(599): at android.os.Looper.loop(Looper.java:123) 
02-14 15:15:59.503: E/AndroidRuntime(599): at android.app.ActivityThread.main(ActivityThread.java:4627) 
02-14 15:15:59.503: E/AndroidRuntime(599): at java.lang.reflect.Method.invokeNative(Native Method) 
02-14 15:15:59.503: E/AndroidRuntime(599): at java.lang.reflect.Method.invoke(Method.java:521) 
02-14 15:15:59.503: E/AndroidRuntime(599): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
02-14 15:15:59.503: E/AndroidRuntime(599): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
02-14 15:15:59.503: E/AndroidRuntime(599): at dalvik.system.NativeStart.main(Native Method) 
02-14 15:15:59.503: E/AndroidRuntime(599): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 
02-14 15:15:59.503: E/AndroidRuntime(599): at android.app.ListActivity.onContentChanged(ListActivity.java:245) 
02-14 15:15:59.503: E/AndroidRuntime(599): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:201) 
02-14 15:15:59.503: E/AndroidRuntime(599): at android.app.Activity.setContentView(Activity.java:1647) 
02-14 15:15:59.503: E/AndroidRuntime(599): at samples.employeedirectory.EmployeeList.onCreate(EmployeeList.java:25) 
02-14 15:15:59.503: E/AndroidRuntime(599): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
02-14 15:15:59.503: E/AndroidRuntime(599): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
02-14 15:15:59.503: E/AndroidRuntime(599): ... 11 more 

Я был бы благодарен, если кто-то помочь мне с, как я новый to android technology

+0

вставив LogCat и указывая на конкретную линию, которая бросает эту ошибку поможет нам больше весь проекту. –

+0

Выход Logcat необходим !! –

+0

, пожалуйста, добавьте логин вашей ошибки. – Natali

ответ

0

Проблема в вашем файле main.xml. Изменение идентификатора вашего ListView, чтобы использовать «Android» префикс:

<ListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 
+0

Спасибо @mark. Но когда я ищу конкретный контакт, ответа нет. Я совершенно уверен, что контакт должен быть получен. – mahi

+0

Исключение в вашем логарифме по той причине, которую я дал. Вы спросили, что вызывает Силу Закрыть, и именно это я ответил. –

0

эй ур активность EmployeeList расширения ListActivity и еще у сделали setContentView();

ли 1 вещь

  1. он должен продлить активность
  2. найти ListView по FindViewById();
  3. комплекта адаптер, связанный с ним

Надеется, что это будет работать

Смежные вопросы