Reputation: 11
As you can see, usually in Eclipse when buttons are working and such it'll have the blue highlighted text when you declare something to a variable.
The problem I'm having is after I declare Button startBtn, aboutBtn;, the variable names will always turn blue indicating that it is working. But right now, for my MainActivity.class, those variables aren't in blue at all. I've tried creating a new class and copying all over and tried doing from scratch and it did not worked. Its odd because my other java class is working fine and button variables are recognized and are highlighted in blue. Anyone has came across this?
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>
Upvotes: 0
Views: 197
Reputation: 5122
Your errors are caused by these lines
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
, etc. are all RadioButton
s defined in your xml layout. So when you are doing (RadioGroup) findViewById(R.id.textmode);
, that does not work because you cannot convert a RadioButton
to a RadioGroup
.
Instead get the RadioGroup
and call getCheckedRadioButtonId
on that:
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){
}
Also note that getCheckedRadioButtonId
returns an ID of the selected RadioButton
, not an index.
The syntax highlighting of variables do not denote whether they work or not, or whether they are recognized or not.
I believe you're referring to the differences between the syntax highlighting of a local or instance variable.
Since you defined aboutBtn
and startBtn
as local variables, they are not highlighted in blue, just like the variable called local
in the image above.
If you define them like the variable field
in the image above, they will be highlighted in blue denoting an instance variable.
All this assuming you know the difference between the two.
The image above is the syntax coloring preferences which can be found in:
Window -> Preferences - Java -> Editor -> Syntax Coloring
Upvotes: 1