gpt4 book ai didi

java - 声明后按钮无法识别

转载 作者:行者123 更新时间:2023-12-01 13:04:11 26 4
gpt4 key购买 nike

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

我遇到的问题是在声明 Button 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>

最佳答案

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

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

textmode , animationmode等等都是RadioButton s 在您的 xml 布局中定义。所以当你在做(RadioGroup) findViewById(R.id.textmode);时,这不起作用,因为您无法转换 RadioButtonRadioGroup .

而是获取 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返回所选的 ID RadioButton ,不是索引。

<小时/>

变量的语法高亮并不表示它们是否有效,或者是否被识别。

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

enter image description here

自从您定义 aboutBtn 以来和startBtn作为局部变量,它们不会以蓝色突出显示,就像名为 local 的变量一样如上图所示。

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

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

上图是语法着色首选项,可以在以下位置找到:
窗口 -> 首选项 - Java -> 编辑器 -> 语法着色

关于java - 声明后按钮无法识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23354109/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com