安卓的TextView使应用程序停止 [英] Android : TextView makes app stop

查看:178
本文介绍了安卓的TextView使应用程序停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 CalendarView ,只要您选择一个日期,一个新的活动opens.In新的活动我已经把一个按钮和一个的TextView ,我想那的TextView 来显示一个字符串。问题是,当我选择显示消息的日期,说:不幸的是,PROJECT_NAME已经停了!。

i have created a CalendarView that whenever you select a date a new activity opens.In that new activity i have put a button and aTextView and I want that TextView to display a string . The thing is that when i select a date a message is shown that says "Unfortunately,"project_name" has stopped!".

这里是新活动的code:

here is the new activity's code:

public class CalendarDate extends Activity {

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

    Bundle extras = getIntent().getExtras();
    String dday = extras.getString("currentday");
    String dmonth =extras.getString("currentmonth");
    String dyear = extras.getString("currentyear");
    ActionBar ab = getActionBar();
    ab.setTitle(dday+"/"+dmonth+"/"+dyear );



    Bundle extraz =getIntent().getExtras();
    String display = extras.getString("EXTRA_MESSAGE");


    LinearLayout layout=(LinearLayout) this.findViewById(R.id.Layout);
    EditText     editText=(EditText) this.findViewById(R.id.textView);
    Button       button=(Button) this.findViewById(R.id.btnAdd);

        LayoutParams lparams = new LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                TextView tv=new TextView(this);
                tv.setLayoutParams(lparams);
                tv.setText(display);
                layout.addView(tv);


}


public void sceduleEdit(View view) {


    Intent intent = new Intent(this, EditSc.class);
    startActivity(intent);

}

}

和这里的XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="CalendarDate" 
android:orientation="vertical"
android:id="@+id/Layout">

  <TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
    android:id="@+id/btnAdd"
    android:layout_width="100dip"
    android:layout_height="wrap_content"
    android:layout_marginBottom="34dp"
    android:layout_marginLeft="88dp"
    android:onClick="sceduleEdit" /> 

如果删除以下code,则该活动作为打开它应该

if remove the following code then the activity opens as it was supposed to

LinearLayout layout=(LinearLayout) this.findViewById(R.id.Layout);
EditText     editText=(EditText) this.findViewById(R.id.textView);
Button       button=(Button) this.findViewById(R.id.btnAdd);

    LayoutParams lparams = new LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            TextView tv=new TextView(this);
            tv.setLayoutParams(lparams);
            tv.setText(display);
            layout.addView(tv);

你有什么想法,为什么发生这种情况?很抱歉的长期问题,非常感谢事先!

do you have any ideas why this is happening? Sorry for the long question and thanks a lot in advance!

推荐答案

您的布局

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

和你有:的EditText EDITTEXT =(EditText上)this.findViewById(R.id.textView);

我们有一个 ClassCastException异常在这里,我想,作为一个TextView不能转换到一个EditText,因此您的应用程序崩溃。

We have a ClassCastException here I suppose, as a TextView can't be cast to an EditText, and so your app crashes.

据我所知,这应该是的TextView的TextView =(TextView中)this.findViewById(R.id.textView);

这篇关于安卓的TextView使应用程序停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆