2

您好,我在 onCreate() 方法中遇到了 FATAL EXCEPTION,代码如下

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;
        }
}

当前详细信息.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

4 回答 4

2

ClassCastException: android.widget.EditText

你有一个ClassCastException. 来自 comeby、name 和 loc 的一个不是EditText.

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

比清理和重建你的项目

于 2013-05-30T13:03:56.897 回答
1

删除R.java并清理您的项目,然后再次构建。

于 2013-05-30T13:05:15.530 回答
0

根据日志 cat 错误是由您的TextView. (查找记录中写有“Caused By”的部分)。

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

我也会检查其他布局元素。当您使用 aButtonTextView任何其他布局组件作为实例变量时,您需要在声明它的位置初始化它

private Button ok =  null;

然后你可以onCreate像你一样调用它。但是简单地声明 Button ok 会给你一个错误。

于 2013-05-30T13:08:16.420 回答
0

你的代码对我来说很好,因为你没有发布你的 MainActivity.java,所以我无法测试。我想问题来自意图线。

于 2013-05-30T13:32:48.027 回答