开发者

My Android application crash can't figure out why?

开发者 https://www.devze.com 2023-03-22 22:30 出处:网络
I get the error (see picture) I can\'t figure out what going on. It literally say that my RadioGroup cannot be cast to TextView ? but why ? R.id.myMessage is not a RadioButtom !

I get the error (see picture)

My Android application crash can't figure out why?

I can't figure out what going on. It literally say that my RadioGroup cannot be cast to TextView ? but why ? R.id.myMessage is not a RadioButtom !

Activity

public class Ch7_JoshRadioGroup extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final TextView myMessage = (TextView) findViewById(R.id.myMessage);

        // Gestion du bouton "Hello, Joshua"
        Button myButton = (Button) findViewById(R.id.myButton);

        myButton.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                // View est un type générale
                // v est le bouton dans ce contexte-ci
                //Button thi开发者_如何学PythonsOne = (Button) v;
                //thisOne.setText("You pushed this button!");

                // Envoi un toast d'une longue durée lorsque le bouton est appuyé
                Toast.makeText(Ch7_JoshRadioGroup.this, "test", Toast.LENGTH_LONG); 
            }
        });
    }
}

Layout :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    android:id="@+id/myMessage"
    />

<Button 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello_joshua"
    android:id="@+id/myButton"
    />

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:orientation="horizontal">

    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#ff0000"
        android:textColor="#000000"
        android:text="red"
    />

    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#00ff00"
        android:textColor="#000000"
        android:text="green"
    />

    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#0000ff"
        android:textColor="#000000"
        android:text="blue"
    />
</LinearLayout>

<RadioGroup
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/myRadioGroup"
    android:orientation="horizontal">

    <RadioButton
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="35dp"
        android:layout_weight="1"
        android:id="@+id/myRadioButtonRed"
     />

    <RadioButton
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="35dp"
        android:layout_weight="1"
        android:id="@+id/myRadioButtonGreen"
     />

     <RadioButton
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="35dp"
        android:layout_weight="1"
        android:id="@+id/myRadioButtonBlue"
     />

</RadioGroup>

</LinearLayout>


Delete the in your xml. IF you are NOT using.

I assumed you want to set the text of the button

Button myButton = (Button) findViewById(R.id.myButton);

    myButton.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            myButton.seText("You pushed this button");

            // Envoi un toast d'une longue durée lorsque le bouton est appuyé
            Toast.makeText(Ch7_JoshRadioGroup.this, "test", Toast.LENGTH_LONG); 
        }

   }

When do this, try Clean-ing your project Project -> Clean select the project and hit OK.

Edit: Your xml should look like this if you dont need the RadioGroup

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    android:id="@+id/myMessage"
    />

<Button 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello_joshua"
    android:id="@+id/myButton"
    />

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:orientation="horizontal">

    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#ff0000"
        android:textColor="#000000"
        android:text="red"
    />

    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#00ff00"
        android:textColor="#000000"
        android:text="green"
    />

    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#0000ff"
        android:textColor="#000000"
        android:text="blue"
    />
</LinearLayout>
</LinearLayout>


This should fix it.

Project->Clean->Clean All Projects [OK]
Run->Run


Shouldn't you use findViewById within a function scope? Try this:

// Gestion du bouton "Hello, Joshua"
Button myButton = null;

myButton.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {

         myButton = (Button) findViewById(R.id.myButton)

        // View est un type générale
        // v est le bouton dans ce contexte-ci
        //Button thisOne = (Button) v;
        //thisOne.setText("You pushed this button!");

        // Envoi un toast d'une longue durée lorsque le bouton est appuyé
        Toast.makeText(Ch7_JoshRadioGroup.this, "test", Toast.LENGTH_LONG); 
    }
});
0

精彩评论

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