Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

Android : TextView makes app stop

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!".

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

}

}

and here is the 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" /> 

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your layout is

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

and you have : EditText editText=(EditText) this.findViewById(R.id.textView);

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

AFAIK, this should be TextView textView=(TextView) this.findViewById(R.id.textView);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...