Android应用程序崩溃了

时间:2014-10-25 04:57:01

标签: java android eclipse

我正在为Android做一个应用程序。我有一个主要活动(不是默认的MainActivity.java,其他名为HotelPresentacion.java的活动有3个用于插入/检查寄存器的按钮或退出应用程序。)

如果我触摸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

我会感谢任何帮助解决我的问题。感谢

2 个答案:

答案 0 :(得分:1)

替换

public class ReservacionFormulario extends ListActivity{


public class ReservacionFormulario extends Activity{

由于您已经扩展了ListActivity,因此Android正在搜索带有Id列表的ListView,但在您的XML中没有任何此类类型的ListView。

答案 1 :(得分:0)

从崩溃日志中我猜它。

ListActivity更改为Activity