2014-10-25 5 views
0

Я делаю приложение для Android. У меня есть основное действие (не по умолчанию MainActivity.java, другая деятельность с именем HotelPresentacion.java, которая имеет 3 кнопки для вставки/проверки регистров или выхода из приложения).Android-приложение разбилось

Если я касаюсь кнопки Registrar, возможно, я могу зарегистрироваться, но приложение неожиданно останавливается. Если я прикасаюсь к кнопке Registros, я могу визуализировать регистры своего приложения, но когда я касаюсь одного регистра (короткий или длинный короткий), чтобы визуализировать или редактировать приложение снова, неожиданно останавливается.

Я изменил свой androidmanifest.xml, чтобы установить HotelPresentacion.java в качестве моего начального действия по умолчанию.

Это код HotelPresentacion.java

package com.example.lab007; 

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

public class HotelPresentacion extends Activity{ 

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

    public void onReservar(View v){ 
     Intent i=new Intent(HotelPresentacion.this, ReservacionFormulario.class); 
     startActivity(i); 
    } 

    public void onVer(View v){ 
     Intent i=new Intent(HotelPresentacion.this, MainActivity.class); 
     startActivity(i); 
    } 

    public void onSalir(View v){ 
     finish(); 
    } 

} 

Мой androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.lab007" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="21" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".HotelPresentacion" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="com.example.lab007.ReservacionFormulario" 
      android:label="@string/app_name" > 
     </activity> 
     <activity 
      android:name="com.example.lab007.MainActivity" 
      android:label="@string/app_name" > 
     </activity> 
    </application> 

</manifest> 

LogCat

10-25 11:16:38.880: E/AndroidRuntime(4247): FATAL EXCEPTION: main 
10-25 11:16:38.880: E/AndroidRuntime(4247): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lab007/com.example.lab007.ReservacionFormulario}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 
10-25 11:16:38.880: E/AndroidRuntime(4247):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 
10-25 11:16:38.880: E/AndroidRuntime(4247):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 
10-25 11:16:38.880: E/AndroidRuntime(4247):  at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
10-25 11:16:38.880: E/AndroidRuntime(4247):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 
10-25 11:16:38.880: E/AndroidRuntime(4247):  at android.os.Handler.dispatchMessage(Handler.java:99) 
10-25 11:16:38.880: E/AndroidRuntime(4247):  at android.os.Looper.loop(Looper.java:130) 
10-25 11:16:38.880: E/AndroidRuntime(4247):  at android.app.ActivityThread.main(ActivityThread.java:3687) 
10-25 11:16:38.880: E/AndroidRuntime(4247):  at java.lang.reflect.Method.invokeNative(Native Method) 
10-25 11:16:38.880: E/AndroidRuntime(4247):  at java.lang.reflect.Method.invoke(Method.java:507) 
10-25 11:16:38.880: E/AndroidRuntime(4247):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
10-25 11:16:38.880: E/AndroidRuntime(4247):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 
10-25 11:16:38.880: E/AndroidRuntime(4247):  at dalvik.system.NativeStart.main(Native Method) 
10-25 11:16:38.880: E/AndroidRuntime(4247): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 
10-25 11:16:38.880: E/AndroidRuntime(4247):  at android.app.ListActivity.onContentChanged(ListActivity.java:243) 
10-25 11:16:38.880: E/AndroidRuntime(4247):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:212) 
10-25 11:16:38.880: E/AndroidRuntime(4247):  at android.app.Activity.setContentView(Activity.java:1657) 
10-25 11:16:38.880: E/AndroidRuntime(4247):  at com.example.lab007.ReservacionFormulario.onCreate(ReservacionFormulario.java:39) 
10-25 11:16:38.880: E/AndroidRuntime(4247):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
10-25 11:16:38.880: E/AndroidRuntime(4247):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 
10-25 11:16:38.880: E/AndroidRuntime(4247):  ... 11 more 

Класс ReservacionFormulario.java

package com.example.lab007; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.ListActivity; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.DatePicker; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.Spinner; 
import android.widget.Toast; 

public class ReservacionFormulario extends ListActivity{ 

    private ComplejoDBAdapter dbAdapterComplejo; 
    private ComplejoSpinnerAdapter complejoSpinnerAdapter; 

    private int modo ; 
    private long id ; 
    private Reservacion reserva = new Reservacion(this); 

    private EditText nombre; 
    private EditText apellidos; 
    //private DatePicker fechaInicio; 
    //private DatePicker fechaFin; 
    private Spinner complejo ; 

    private Button boton_guardar; 
    private Button boton_cancelar; 

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

     Intent intent = getIntent(); 
     Bundle extra = intent.getExtras(); 

     if (extra == null) return; 

     nombre = (EditText) findViewById(R.id.nombre); 
     apellidos = (EditText) findViewById(R.id.apellidos); 
     //fechaInicio = (DatePicker) findViewById(R.id.); 
     //fechaInicio = (DatePicker) findViewById(R.id.); 
     complejo = (Spinner) findViewById(R.id.complejo); 

     boton_guardar = (Button) findViewById(R.id.boton_guardar); 
     boton_cancelar = (Button) findViewById(R.id.boton_cancelar); 

     complejoSpinnerAdapter = new ComplejoSpinnerAdapter(this, Complejo.getAll(this, null)); 
     complejo.setAdapter(complejoSpinnerAdapter); 

     if (extra.containsKey(ReservacionDBAdapter.C_COL_ID)){ 
      id = extra.getLong(ReservacionDBAdapter.C_COL_ID); 
      consultar(id); 
     } 

     establecerModo(extra.getInt(ReservacionActivity.C_MODO)); 

     boton_guardar.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v){ 
       guardar(); 
      } 
     }); 

     boton_cancelar.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v){ 
       cancelar(); 
      } 
     });  
    } 

    private void establecerModo(int m) 
    { 
     this.modo = m ; 

     if (modo == ReservacionActivity.C_VISUALIZAR){ 
      this.setTitle(nombre.getText().toString()); 
      this.setEdicion(false); 
     } 
     else if (modo == ReservacionActivity.C_CREAR){ 
      this.setTitle("Nuevo reservacion"); 
      this.setEdicion(true); 
     } 
     else if (modo == ReservacionActivity.C_EDITAR){ 
      this.setTitle("Editar reservacion"); 
      this.setEdicion(true); 
     } 
    } 

    private void consultar(long id){ 
     reserva = Reservacion.find(this, id); 

     nombre.setText(reserva.getNombre()); 
     apellidos.setText(reserva.getApellidos()); 
     complejo.setSelection(complejoSpinnerAdapter.getPositionById(reserva.getComplejoId())); 
    } 

    private void setEdicion(boolean opcion){ 
     nombre.setEnabled(opcion); 
     apellidos.setEnabled(opcion); 
     complejo.setEnabled(opcion); 

     // Controlamos visibilidad de botonera 
     LinearLayout v = (LinearLayout) findViewById(R.id.botonera); 

     if (opcion) 
      v.setVisibility(View.VISIBLE); 
     else 
      v.setVisibility(View.GONE); 
    } 

    private void guardar(){ 

     /*verificar si funciona*/ 

     try{ 
      if(nombre.length()<=0){ 
       reserva.setNombre(nombre.getText().toString()); 
      } 
     }catch(Exception e){ 

     } 

     reserva.setApellidos(apellidos.getText().toString()); 
     reserva.setComplejoId(complejo.getSelectedItemId()); 

     reserva.save(); 

     if (modo == ReservacionActivity.C_CREAR){ 
      Toast.makeText(ReservacionFormulario.this, "Creado", Toast.LENGTH_SHORT).show(); 
     } 
     else if (modo == ReservacionActivity.C_EDITAR){ 
      Toast.makeText(ReservacionFormulario.this, "Modificado", Toast.LENGTH_SHORT).show(); 
     } 
     setResult(RESULT_OK); 
     finish(); 
    } 

    private void cancelar(){ 
     setResult(RESULT_CANCELED, null); 
     finish(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     menu.clear(); 

     if (modo == ReservacionActivity.C_VISUALIZAR) 
      getMenuInflater().inflate(R.menu.reservacion_formulario_ver, menu);  
     else 
      getMenuInflater().inflate(R.menu.reservacion_formulario_editar, menu);  
     return true; 
    } 

    @Override 
    public boolean onMenuItemSelected(int featureId, MenuItem item) { 

     switch (item.getItemId()){ 
      case R.id.menu_eliminar: 
       borrar(id); 
       return true;     
      case R.id.menu_cancelar: 
       cancelar(); 
       return true;     
      case R.id.menu_guardar: 
       guardar(); 
       return true;     
      case R.id.menu_editar: 
       establecerModo(ReservacionActivity.C_EDITAR); 
       return true; 
     }  
     return super.onMenuItemSelected(featureId, item); 
    } 

    private void borrar(final long id){ 
     AlertDialog.Builder dialogEliminar = new AlertDialog.Builder(this); 

     dialogEliminar.setIcon(android.R.drawable.ic_dialog_alert); 
     dialogEliminar.setTitle("Eliminar"); 
     dialogEliminar.setMessage("¿Desea eliminar?"); 
     dialogEliminar.setCancelable(false); 

     dialogEliminar.setPositiveButton(getResources().getString(android.R.string.ok), new DialogInterface.OnClickListener() { 

      public void onClick(DialogInterface dialog, int boton) { 
       reserva.delete(); 
       Toast.makeText(ReservacionFormulario.this, "Eliminado", Toast.LENGTH_SHORT).show(); 

       setResult(RESULT_OK); 
       finish(); 
      } 
     }); 

     dialogEliminar.setNegativeButton(android.R.string.no, null);   
     dialogEliminar.show(); 
    } 
} 

activity_reservacion_formulario.xml

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/ScrollView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:padding="10dp" > 

     <!-- Nombre --> 

     <TextView 
      android:id="@+id/label_nombre" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Nombre" /> 

     <EditText 
      android:id="@+id/nombre" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_below="@+id/label_nombre" 
      android:maxLength="40" 
      android:ems="10" /> 

     <!-- Apellidos --> 

     <TextView 
      android:id="@+id/label_apellidos" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/nombre" 
      android:maxLength="60" 
      android:text="Apellidos" /> 

     <EditText 
      android:id="@+id/apellidos" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_below="@+id/label_apellidos" 
      android:ems="10" /> 

     <!-- Fecha de Inicio --> 

     <!-- <TextView 
      android:id="@+id/label_fechainicio" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/apellidos" 
      android:text="Fecha de Inicio" /> 

     <DatePicker 
      android:id="@+id/datePickerInicio" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/label_fechainicio" /> --> 

     <!-- Fecha de Fin --> 

     <!-- <TextView 
      android:id="@+id/label_fechafin" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/datePickerInicio" 
      android:text="Fecha de Fin" /> 

     <DatePicker 
      android:id="@+id/datePickerFin" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/label_fechafin" /> --> 

     <!-- Spinner --> 
     <TextView 
      android:id="@+id/label_complejo" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/datePickerFin" 
      android:text="Complejo" /> 

     <Spinner 
      android:id="@+id/complejo" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/label_complejo" /> 

     <TextView 
      android:id="@+id/label_ciudad" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Complejo" 
      android:layout_below="@+id/complejo" /> 

     <LinearLayout 
      android:id="@+id/botonera" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"    
      android:layout_marginTop="20dp" 
      android:gravity="center" 
      android:layout_below="@+id/datePickerFin" 
      android:orientation="horizontal" > 

      <Button 
       android:id="@+id/boton_cancelar" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Cancelar" /> 

      <Button 
       android:id="@+id/boton_guardar" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Guardar" /> 
     </LinearLayout> 
    </RelativeLayout> 

</ScrollView> 

Это весь проект:

Project

Я appreciante любой помощи для решения моей проблемы. Спасибо

+0

На какой строке приложение разбилось? Вы отлаживали его? Можете ли вы показать свой логарифм, чтобы мы могли видеть, где проблема ?. –

+0

сначала инициализируйте и определите свои кнопки в своем классе java. – prakash

+0

публикация logcat помогает –

ответ

0

Из журнала аварии я угадываю его.

Изменение ListActivity к Activity

1

Заменить

public class ReservacionFormulario extends ListActivity{ 

с

public class ReservacionFormulario extends Activity{ 

Как вы расширили ListActivity так Android ищет в виде списка со списком Id, но в в вашем XML нет такого типа ListView.

+1

Хорошо. Он работает, но кнопка Guardar и Cancelar en activity не работает. – ArCiGo

+0

@ArCiGo Задайте новый вопрос. –

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