开发者

cant use Textviews and buttons in the code

i have a service which notifies the user and if the notification is clicked a activity starts. the activity contains textviews and buttons. the layout is defined in R.layout.main . if i want the change the text of a text view or a button in the code, not in the xml, the apllication chrashes...

i dont know why.. here is the code of the activity

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    TextView thema = (TextView) findViewById(R.id.thema);
    TextView zeit = (TextView) findViewById(R.id.zeit);
    TextView datum = (TextView) findViewById(R.id.datum);
    TextView ort = (TextView) findViewById(R.id.ort);

    Button change = (Button) findViewById(R.id.changeDetails);
    Button save = (Button) findViewById(R.id.save);
    Button ignore = (Button) findViewById(R.id.ignore);
    Button cancel = (Button) findViewById(R.id.cancel);     

    //The string of the MainService class are not the problem they arent null...

    thema.setText(MainService.thema);
    zeit.setText(MainService.start);
    datum.setText(MainService.end);
    ort.setText(MainService.ort);


    setContentView(R.layout.main);
}

here the xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:id="@+id/tableLayout1" android:layout_height="fill_parent">

    <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content">
        <TextView android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="wrap_content" android:id="@+id/textView1" android:text="Thema:"></TextView>

    </TableRow>

    <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content">
        <TextView android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:layout_width="wrap_content" android:id="@+id/thema" android:text=""></TextView>
    </TableRow>

    <TableRow android:id="@+id/tableRow3" android:layout_width="wrap_content" android:layout_height="wrap_content">
        <TextView android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="wrap_content" android:id="@+id/textView3" android:text="Zeit:"></TextView>
    </TableRow>

    <TableRow android:id="@+id/tableRow4" android:layout_width="wrap_content" android:layout_height="wrap_content">
        <TextView android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:layout_width="wrap_content" android:id="@+id/zeit"></TextView>
    </TableRow>

    <TableRow android:id="@+id/tableRow5" android:layout_width="wrap_content" android:layout_height="wrap_content">
        <TextView android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="wrap_content" android:id="@+id/textView5" android:text="Datum:"></TextView>
    </TableRow>

    <TableRow android:id="@+id/tableRow6" android:layout_width="wrap_content" android:layout_height="wrap_content">
        <TextView android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:layout_width="wrap_content" android:id="@+id/datum"></TextView>
    </TableRow>

    <TableRow android:id="@+id/tableRow7" android:layout_width="wrap_content" android:layout_height="wrap_content">
        <TextView android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="wrap_content" android:id="@+id/textView7" android:text="Ort:"></TextView>
    </TableRow>


    <TableRow android:id="@+id/tableRow8" android:layout_width="wrap_content" android:layout_height="wrap_content">
        <TextView android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:layout_width="wrap_content" android:id="@+id/ort"></TextView>
    </TableRow>

    <TableRow android:id="@+id/tableRow10" android:layout_width="wrap_content" android:layout_height="wrap_content">
        <Button android:layout_width="fill_parent" android:layout_weight="1" android:layout_height="wrap_content" android:text="Details ändern" android:id="@+id/changeDetails"></Button>
    </TableRow>

                <View android:layout_height="2dip" android:background="#FF909090" />

                <View android:layout_height="5dip" android:background="@android:color/black"/>

    <TableRow android:id="@+id/tableRow9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:baselineAligned="false">
        <Button android:layout_width="100dp" android:layout_height="wrap_content" android:text="Speichern" android:layout_weight="0" android:layout_gravity="center_vertical" android:id="@+id/save"></Button>
        <Button android:layout_width="100dp" android:layout_height="wrap_content" android:text="Ignorieren" android:layout_weight="0" android:layout_gravity="center_vertical" android:id="@+id/ignore"></Button>
        <Button android:layout_width="100dp" android:la开发者_开发技巧yout_weight="0" android:layout_height="wrap_content" android:text="Abbrechen" android:layout_gravity="center_vertical" android:id="@+id/cancel"></Button>
    </TableRow>

Thanks


You should call setContentView(R.layout.main); before inflating the views, instead you'll always get an exception.


Please write this

setContentView(R.layout.main) line after the

super.onCreate(savedInstanceState) Line and before

TextView thema = (TextView) findViewById(R.id.thema);,

It will solve your problem.


setContentView(R.layout.main);            
TextView thema = (TextView) findViewById(R.id.thema);
TextView zeit = (TextView) findViewById(R.id.zeit);
TextView datum = (TextView) findViewById(R.id.datum);
TextView ort = (TextView) findViewById(R.id.ort);

Button change = (Button) findViewById(R.id.changeDetails);
Button save = (Button) findViewById(R.id.save);
Button ignore = (Button) findViewById(R.id.ignore);
Button cancel = (Button) findViewById(R.id.cancel);     
thema.setText("this is text");

that has worked but what is MainService?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜