在onCreate()中获取FATAL EXCEPTION

时间:2013-05-30 13:02:00

标签: android android-activity

您好我在onCreate()方法中使用以下代码获取FATAL EXCEPTION :: 我在将这些行的布局添加到按钮,Textviews和Timepicker等视图时遇到异常 请帮我解决以下异常

05-30 18:21:29.921: E/AndroidRuntime(14486): FATAL EXCEPTION: main
05-30 18:21:29.921: E/AndroidRuntime(14486): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nirbhaya/com.example.nirbhaya.CurrentDetails}: java.lang.ClassCastException: android.widget.EditText
05-30 18:21:29.921: E/AndroidRuntime(14486):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
05-30 18:21:29.921: E/AndroidRuntime(14486):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
05-30 18:21:29.921: E/AndroidRuntime(14486):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-30 18:21:29.921: E/AndroidRuntime(14486):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
05-30 18:21:29.921: E/AndroidRuntime(14486):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-30 18:21:29.921: E/AndroidRuntime(14486):    at android.os.Looper.loop(Looper.java:130)
05-30 18:21:29.921: E/AndroidRuntime(14486):    at android.app.ActivityThread.main(ActivityThread.java:3687)
05-30 18:21:29.921: E/AndroidRuntime(14486):    at java.lang.reflect.Method.invokeNative(Native Method)
05-30 18:21:29.921: E/AndroidRuntime(14486):    at java.lang.reflect.Method.invoke(Method.java:507)
05-30 18:21:29.921: E/AndroidRuntime(14486):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-30 18:21:29.921: E/AndroidRuntime(14486):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-30 18:21:29.921: E/AndroidRuntime(14486):    at dalvik.system.NativeStart.main(Native Method)
05-30 18:21:29.921: E/AndroidRuntime(14486): Caused by: java.lang.ClassCastException: android.widget.EditText
05-30 18:21:29.921: E/AndroidRuntime(14486):    at com.example.nirbhaya.CurrentDetails.onCreate(CurrentDetails.java:27)
05-30 18:21:29.921: E/AndroidRuntime(14486):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-30 18:21:29.921: E/AndroidRuntime(14486):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
05-30 18:21:29.921: E/AndroidRuntime(14486):    ... 11 more

package com.example.nirbhaya;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;

public class CurrentDetails extends Activity implements OnClickListener{

    Button ok;
    EditText loc,comeby,name;
    TimePicker setTime;
    String location,Name,comingby,destTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.currentdetails);

        ok = (Button)findViewById(R.id.bOK);
        loc = (EditText)findViewById(R.id.etPreLoc);
        comeby = (EditText)findViewById(R.id.etComing);
        name = (EditText)findViewById(R.id.etDriver);
        setTime = (TimePicker)findViewById(R.id.timePicker1);

        ok.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        if(v.getId() == R.id.bOK)
        {
            location = loc.getText().toString();
            Name = name.getText().toString();
            comingby = comeby.getText().toString();
            destTime = setTime.getCurrentHour().toString() + ":" 
                     + setTime.getCurrentMinute().toString();


            Intent i = new Intent(this,MainActivity.class);
            i.putExtra("NAME", Name);
            i.putExtra("LOC", location);
            i.putExtra("COMEBY", comingby);
            i.putExtra("TIME", destTime);
            startActivity(i);
        }

    }
    public void onBackPressed() {
        // do something on back.
        Log.i("AFSDFDS","Back Pressed");
        System.exit(0);
        return;
        }
}

currentdetails.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#CACACA"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="94dp"
        android:text="Present Details"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/etPreLoc"
        android:text="Coming By"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/textView4"
        android:layout_below="@+id/textView1"
        android:text="Present Location"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/etPreLoc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignParentRight="true"
        android:ems="10"
        android:hint="Where are u now?"
        android:inputType="textPersonName" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView3"
        android:layout_toLeftOf="@+id/etDriver"
        android:text="Vehicle details"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView4"
        android:text="Expected time to reach by"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TimePicker
        android:id="@+id/timePicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView5" />

    <Button
        android:id="@+id/bOK"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/timePicker1"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="42dp"
        android:text="OK" />

    <EditText
        android:id="@+id/etDriver"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/etComing"
        android:layout_toRightOf="@+id/textView3"
        android:ems="10"
        android:hint="Name of driver/vehicle no"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/etComing"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/etPreLoc"
        android:ems="10"
        android:hint="car/auto/other" />

</RelativeLayout>

4 个答案:

答案 0 :(得分:2)

ClassCastException: android.widget.EditText

你有ClassCastException。一个来自comeby,name和loc不是EditText

检查您的布局(R.layout.currentdetails

比清理和重建项目

答案 1 :(得分:1)

删除R.java并清理项目,然后重新构建。

答案 2 :(得分:0)

根据log cat,错误是由TextView引起的。 (查找记录中“引起”的部分)。

05-30 18:21:29.921: E/AndroidRuntime(14486): Caused by: java.lang.ClassCastException: android.widget.EditText

我也会检查其他布局元素。当您使用ButtonTextView或任何其他布局组件作为实例变量时,需要在声明它的地方对其进行初始化

private Button ok =  null;

然后你就可以像onCreate那样在呼叫中呼叫它。但只是声明Button ok会给你一个错误。

答案 3 :(得分:0)

由于您没有发布您的MainActivity.java,因此我的代码无法正常工作,我无法测试。我猜这个问题来自意图线。