声明后无法识别按钮

时间:2014-04-29 01:46:39

标签: java android

正如您所看到的,通常在Eclipse中按钮工作时,当您向变量声明某些内容时,它将具有蓝色突出显示的文本。

我遇到的问题是在我声明按钮startBtn,aboutBtn; 之后,变量名称将始终变为蓝色,表示它正在工作。但是现在,对于我的MainActivity.class,这些变量根本不是蓝色的。我已经尝试创建一个新类并复制到所有并尝试从头开始做它没有用。它很奇怪,因为我的其他java类工作正常,按钮变量被识别并以蓝色突出显示。有谁遇到过这个?

MainActivity.class

package tp.mp2014.dotmatrix;

    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v7.app.ActionBarActivity;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;


    public class MainActivity extends ActionBarActivity {

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

            Button aboutBtn, startBtn;

            aboutBtn = (Button) findViewById(R.id.aboutBtn);
            startBtn = (Button) findViewById(R.id.startBtn);

            aboutBtn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent about = new Intent(MainActivity.this, aboutPage.class);
                    startActivity(about);

                }
            });

           startBtn.setOnClickListener(new View.OnClickListener() {


                @Override
                public void onClick(View v) {
                    Intent start = new Intent(MainActivity.this, startPage.class);
                    startActivity(start);

                }
            });
            if (savedInstanceState == null) {
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.container, new PlaceholderFragment())
                        .commit();
            }
        }


        @Override
        public boolean onCreateOptionsMenu(Menu menu) {

            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

        /**
         * A placeholder fragment containing a simple view.
         */
        public static class PlaceholderFragment extends Fragment {

            public PlaceholderFragment() {
            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment_main, container, false);
                return rootView;
            }
        }

    }

startPage.class

package tp.mp2014.dotmatrix;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;

public class startPage extends Activity {

    Button backButton, nextButton;
    RadioGroup modechoice1, modechoice2, modechoice3, sizechoice1, sizechoice2;

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

        nextButton = (Button) findViewById(R.id.nextselection);
        backButton = (Button) findViewById(R.id.backselection);
        modechoice1 = (RadioGroup) findViewById(R.id.textmode);
        modechoice2 = (RadioGroup) findViewById(R.id.animationmode);
        modechoice3 = (RadioGroup) findViewById(R.id.imagemode);
        sizechoice1 = (RadioGroup) findViewById(R.id.normal);
        sizechoice2 = (RadioGroup) findViewById(R.id.extended);

        /*Text Mode and 32X32 Display*/
        int choice1mode = modechoice1.getCheckedRadioButtonId();
        int choice1size = sizechoice1.getCheckedRadioButtonId();

        /*Animation Mode and 32x32 Display*/
        int choice2mode = modechoice2.getCheckedRadioButtonId();
        choice1size = sizechoice1.getCheckedRadioButtonId();

        if(choice1mode == 1 && choice1size == 1){

            nextButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent nextSelection = new Intent(startPage.this, textmode32by32.class);
                    startActivity(nextSelection);

                }
            });

        }else if(choice2mode == 1 && choice1size == 1){

            nextButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent nextSelection1 = new Intent(startPage.this, animationmode32by32.class);
                    startActivity(nextSelection1);

                }
            });

        }

        backButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent backSelection = new Intent(startPage.this, MainActivity.class);
                startActivity(backSelection);

            }
        });




    }

}

logcat的

    04-29 10:19:57.820: E/AndroidRuntime(27444): FATAL EXCEPTION: main
04-29 10:19:57.820: E/AndroidRuntime(27444): java.lang.RuntimeException: Unable to start activity ComponentInfo{tp.mp2014.dotmatrix/tp.mp2014.dotmatrix.startPage}: java.lang.ClassCastException: android.widget.RadioButton cannot be cast to android.widget.RadioGroup
04-29 10:19:57.820: E/AndroidRuntime(27444):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2129)
04-29 10:19:57.820: E/AndroidRuntime(27444):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2154)
04-29 10:19:57.820: E/AndroidRuntime(27444):    at android.app.ActivityThread.access$700(ActivityThread.java:146)
04-29 10:19:57.820: E/AndroidRuntime(27444):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1260)
04-29 10:19:57.820: E/AndroidRuntime(27444):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-29 10:19:57.820: E/AndroidRuntime(27444):    at android.os.Looper.loop(Looper.java:137)
04-29 10:19:57.820: E/AndroidRuntime(27444):    at android.app.ActivityThread.main(ActivityThread.java:4949)
04-29 10:19:57.820: E/AndroidRuntime(27444):    at java.lang.reflect.Method.invokeNative(Native Method)
04-29 10:19:57.820: E/AndroidRuntime(27444):    at java.lang.reflect.Method.invoke(Method.java:511)
04-29 10:19:57.820: E/AndroidRuntime(27444):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043)
04-29 10:19:57.820: E/AndroidRuntime(27444):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
04-29 10:19:57.820: E/AndroidRuntime(27444):    at dalvik.system.NativeStart.main(Native Method)
04-29 10:19:57.820: E/AndroidRuntime(27444): Caused by: java.lang.ClassCastException: android.widget.RadioButton cannot be cast to android.widget.RadioGroup
04-29 10:19:57.820: E/AndroidRuntime(27444):    at tp.mp2014.dotmatrix.startPage.onCreate(startPage.java:22)
04-29 10:19:57.820: E/AndroidRuntime(27444):    at android.app.Activity.performCreate(Activity.java:5185)
04-29 10:19:57.820: E/AndroidRuntime(27444):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
04-29 10:19:57.820: E/AndroidRuntime(27444):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
04-29 10:19:57.820: E/AndroidRuntime(27444):    ... 11 more

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

    <LinearLayout
        android:id="@+id/LinearLayout1H"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textmodeselection"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/textmodeselection"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/textsizeselection"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp"
            android:text="@string/textsizeselection"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </LinearLayout>

    <RadioGroup
        android:id="@+id/textsizeselection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/modeselection"
        android:layout_toRightOf="@+id/modeselection" >

        <RadioButton
            android:id="@+id/normal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/sizechoice1" />

        <RadioButton
            android:id="@+id/extended"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/sizechoice2" />
    </RadioGroup>

    <RadioGroup
        android:id="@+id/modeselection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/LinearLayout1H" >

        <RadioButton
            android:id="@+id/textmode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/modechoice1" />

        <RadioButton
            android:id="@+id/animationmode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/modechoice2" />

        <RadioButton
            android:id="@+id/imagemode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/modechoice3" />
    </RadioGroup>

    <Button
        android:id="@+id/backselection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="@string/back" />

    <Button
        android:id="@+id/nextselection"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="@string/next" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

您的错误是由这些行引起的

modechoice1 = (RadioGroup) findViewById(R.id.textmode);
modechoice2 = (RadioGroup) findViewById(R.id.animationmode);
modechoice3 = (RadioGroup) findViewById(R.id.imagemode);
sizechoice1 = (RadioGroup) findViewById(R.id.normal);
sizechoice2 = (RadioGroup) findViewById(R.id.extended);

textmodeanimationmode等都是在xml布局中定义的RadioButton。因此,当您执行(RadioGroup) findViewById(R.id.textmode);时,这不起作用,因为您无法将RadioButton转换为RadioGroup

而是获取RadioGroup并在其上调用getCheckedRadioButtonId

RadioGroup modechoice = (RadioGroup) findViewById(R.id.modeselection);
RadioGroup sizechoice = (RadioGroup) findViewById(R.id.textsizeselection);

int modeChoiceId = modechoice.getCheckedRadioButtonId();
int sizeChoiceId = sizechoice.getCheckedRadioButtonId();

// An example, not sure about your logic.
if (modeChoiceId == R.id.textmode && sizeChoiceId == R.id.normal){

}

另请注意,getCheckedRadioButtonId会返回所选RadioButton的ID,而不是索引。


变量的语法突出显示不表示它们是否有效,或者它们是否被识别。

我相信您指的是本地实例变量的语法突出显示之间的差异。

enter image description here

由于您将aboutBtnstartBtn定义为局部变量,因此它们不会以蓝色突出显示,就像上图中名为local的变量一样。

如果您将它们定义为上图中的变量field,它们将以蓝色突出显示,表示实例变量。

所有这些假设您知道两者之间的区别。

上面的图片是语法着色偏好,可以在以下位置找到:
窗口 - &gt;偏好 - Java - &gt;编辑 - &gt;语法着色