package com.example.hello.word;
import android.R;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ToggleButton;
public class OptionsMenu extends Activity{
MediaPlayer ourSong;
Button btn1;
Intent openStartingPoint;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.menuoptions);
final ToggleButton onTog = (ToggleButton) findViewById(R.id.toggleButton1);
onTog.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if (onTog.isChecked()){
ourSong.start();
}else{
ourSong.stop();
}
}
});
I have an error under all the Buttons and its shows me for example: "menuoptions cannot be resolved or is not field".. the same error on togglebutton1
my XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Volume"
android:textSize="40dp"
android:textStyle="bold"
android:layout_marginTop="40dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="121dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="ToggleButton" />
<Button
android:id="#+id/button1"
android:layout_width="178dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="150dp"
android:text="Back" />
</LinearLayout>
I tried to clean the project.. delete the R class and everything! what I can do to fix that Error?
Thanx for helpers..
You are getting that error because your are importing android.R you have to remove this import.
Try removing the line import android.R; from your code, this should fix your problem
When you press control + shift + O to organise your imports in eclipse, it sometimes adds the import for android.R
Here is a similar question
R cannot be resolved - Android error
Related
I am creating an android application that consists of registration form in dialog. Here, I placed a drop down spinner in registration dialog. But,here it was showing type casting error even i did the correct type casting. Please check it and make it solve. I am trying to solve since 2 hours. This is my code:
package com.example.testingsample;
import com.gc.materialdesign.views.ButtonFlat;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity
{
Button checking,spinner_check;
EditText username,password,confirm,seq_answer;
ButtonFlat clear,register;
Spinner spinner;
String[] questions = {
"What is your first name.?",
"What is your first pet name.?",
"What is your school name.?",
"What is your date of birth.?",
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checking = (Button)findViewById(R.id.button1);
checking.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
android.app.Dialog registration_dialog = new android.app.Dialog(MainActivity.this);
registration_dialog.setContentView(R.layout.signup);
registration_dialog.setTitle("MedeQuip Registration");
username = (EditText)registration_dialog.findViewById(R.id.Dialog_edittext_username);
password = (EditText)registration_dialog.findViewById(R.id.Dialog_edittext_password);
confirm = (EditText)registration_dialog.findViewById(R.id.Dialog_edittext_confirmpassword);
spinner = (Spinner)registration_dialog.findViewById(R.id.spinner1);
ArrayAdapter adpter = new ArrayAdapter(MainActivity.this ,android.R.layout.simple_dropdown_item_1line,questions);
spinner.setAdapter(adpter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id)
{
Toast.makeText(MainActivity.this, questions[position], Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
seq_answer = (EditText)registration_dialog.findViewById(R.id.editText1);
clear = (ButtonFlat)registration_dialog.findViewById(R.id.dialog_button_clear);
register = (ButtonFlat)registration_dialog.findViewById(R.id.Dialog_button_register);
//seq_answer = (EditText)registration_dialog.findViewById(R.id.editText1);*/
/*spinner_check.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Toast.makeText(MainActivity.this, "Checking", Toast.LENGTH_LONG).show();
}
});*/
registration_dialog.show();
}
});
}}
This is my dialog xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" >
<EditText
android:id="#+id/Dialog_edittext_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/Dialog_edittext_username"
android:ems="10"
android:hint="Password"
android:inputType="textPassword" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/Dialog_edittext_confirmpassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/Dialog_edittext_password"
android:ems="10"
android:hint="Confirm Password"
android:inputType="textPassword" />
<EditText
android:id="#+id/Dialog_edittext_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="User Name" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="345dp"
android:layout_height="60dp"
android:layout_below="#+id/Dialog_edittext_confirmpassword"
android:layout_centerHorizontal="true" />
<com.gc.materialdesign.views.ButtonFlat
android:id="#+id/Dialog_button_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/dialog_button_clear"
android:text="Register"
android:textColor="#ffffff" >
</com.gc.materialdesign.views.ButtonFlat>
<com.gc.materialdesign.views.ButtonFlat
android:id="#+id/dialog_button_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_marginRight="52dp"
android:layout_marginTop="59dp"
android:layout_toLeftOf="#+id/Dialog_button_register"
android:text="clear"
android:textColor="#ffffff" >
</com.gc.materialdesign.views.ButtonFlat>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/spinner1"
android:layout_marginTop="67dp"
android:ems="10"
android:hint="Enter your answer for security question" />
</RelativeLayout>
Same problem i faced but i solved that problem with following solutions
-> If you are implementing in Eclipse once clean the project then test it.
-> If above also not works once restart your Eclipse.
Hope it will helps you.
error is that, R is underlined red! and com.example.guessthenumber.R is also underlined red!
when hovered it says to create a class R in package com.example.guessthenumber.R
xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/Guess"
android:gravity="start"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Done!" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_centerHorizontal="true"
android:text="Enter your guessed number below"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#191970"
android:textStyle="bold" />
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_centerVertical="true"
android:text="0"
android:inputType="number" />
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/editText1"
android:layout_marginTop="29dp"
android:fontFamily="sans-serif-condensed"
android:text="#string/guess"
android:textColor="#000000"
android:textSize="22sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/game" />
</RelativeLayout>
below one is the java file:
package com.example.guessthenumber;
import android.os.Bundle;
import com.example.guessthenumber.R
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView t1 = (TextView) findViewById(R.id.textView1);
final TextView guessText = (TextView) findViewById(R.id.textView2);
final EditText userGuess = (EditText) findViewById(R.id.editText1);
Button b1 = (Button) findViewById(R.id.button1);
final String[] myNames={"Shashank","Sarthak","Gowda","$hankar"};
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String randText="";
// TODO Auto-generated method stub
int rando = (int) (Math.random()* 6);
int userNumber = Integer.parseInt(userGuess.getText().toString());
if(userNumber == rando){
guessText.setText("You got it right!");
}else{
guessText.setText("Guess again! :(");
}
randText=Integer.toString(rando);
t1.setText(randText);
userGuess.setText("0");
}
});
}
#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;
}
}
Replace:
import android.R;
by
import com.example.guessthenumber.R
OR
remove import.android.R and use ctrl+shift+O, organise imports if you are using eclipse.
Clean, build your project.
Also, pls read more about R:
Android: What is R? Why is it so Cryptic?
Check your imports - at the top of your activity, below your package name. And make sure it has something like this line:
import your_application_package_name.R;
Agreed with gunar's comment above,
In your xml, the "id" has not been defined properly, for your TextView. Please post the xml code, may be the activity's too.
Correct way for "id" in TextView, xml :
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Check that you are importing the R class from your package and not the Android's one. Check also that your resources do not contains error, otherwise the R class will not be generated.
I'm learning Android (I'm a beginner) and making a program for CheckBox.
Everything seems fine, but when I cleaned my project suddenly, R.java was deleted and it's giving an error.
I checked three times, but not getting it back.
Here is my source code:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="IPhone" />
<CheckBox
android:id="#+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android"
android:checked="true"/>
<CheckBox
android:id="#+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Windows Mobile"
android:checked="false"/>
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display" />
</LinearLayout>
MainActivity.java
package com.example.lesson06;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
addListenerOnChkbox();
}
public void addListenerOnChkbox(){
CheckBox chkbx=(CheckBox)findViewById(R.id.checkBox1);
chkbx.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if(((CheckBox) v).isChecked()){
Toast.makeText(MainActivity.this, "Bro..try Android :)", Toast.LENGTH_LONG);
}
}
});
}
public void addListenerOnButton(){
final CheckBox chkbx=(CheckBox)findViewById(R.id.checkBox1);
final CheckBox chkbx2=(CheckBox)findViewById(R.id.checkBox2);
final CheckBox chkbx3=(CheckBox)findViewById(R.id.checkBox3);
final Button btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener(){
public void onClick(View v){
StringBuffer result=new StringBuffer();
result.append("IPhone check: ").append(chkbx.isChecked());
result.append("\nAndroid check: ").append(chkbx2.isChecked());
result.append("\nWindows Mobile Check: ").append(chkbx3.isChecked());
Toast.makeText(MainActivity.this, result.toString(), Toast.LENGTH_LONG).show();
}
});
}
}
Any help would be greatly appreciated.
Your Button Tag not closed into activity_main.xml
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display"
Close that
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display" />
and clean project. Your R.java class will be generated.
Change your button to the following:
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display"/>
It seems you forgot to close the button tag, so AAPT could not generate a new R.class.
Generally R.java is not created when you have errors on one or more xml files. In this case the error is in the Button tag (the IDE should warn you about that error).
In following code shown below
I am trying open a new activity(memo.class to view.class) when click view button.but showing an error "activity not found exception: unable to find explicit activity class".
what is wrong in my code??
please help me
my code:
memo.xml
<?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="match_parent">
<TextView android:text="Titile" android:id="#+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:id="#+id/question" android:layout_width="match_parent" android:layout_height="wrap_content" android:text=""></EditText>
<TextView android:text="Text" android:id="#+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:minLines="6" android:maxLines="10" android:id="#+id/answer" android:layout_width="match_parent" android:layout_height="wrap_content" android:text=""></EditText>
<TableLayout android:layout_height="wrap_content"
android:layout_width="wrap_content" android:layout_gravity="center"
android:stretchColumns="*">
<TableRow>
<Button android:id="#+id/add" android:layout_width="wrap_content" android:text="ADD" android:layout_height="wrap_content"></Button>
<Button android:id="#+id/view" android:layout_width="wrap_content" android:text="VIEW" android:layout_height="wrap_content"></Button>
</TableRow>
</TableLayout>
</LinearLayout>
memo.java
package quesansw.the1;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
public class Memo extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Dialog d1 = new Dialog(this);
Window window = d1.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
d1.setTitle("Register Questions");
d1.setContentView(R.layout.memo);
d1.show();
Button view1 = (Button) d1.findViewById(R.id.view);
Button add = (Button) d1.findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
EditText add = (EditText) d1.findViewById(R.id.question);
EditText view = (EditText) d1.findViewById(R.id.answer);
System.out.println(add.getText());
System.out.println(view.getText());
}
});
view1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Intent intent = new Intent(getBaseContext(), View.class);
startActivity(intent);
}
});
}
}
view.xml
<?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="match_parent">
<EditText android:id="#+id/question" android:layout_width="match_parent" android:layout_height="wrap_content" android:text=""></EditText>
</LinearLayout>
view.java
package quesansw.the1;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class View extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Dialog d1 = new Dialog(this);
Window window = d1.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
d1.setTitle("Login");
d1.setContentView(R.layout.view);
d1.show();
}
}
You should declare the Activity in the manifest:
<activity
android:name="com.yourpackagename.View"/>
Basically, your AndroidManifest.xml file (found in the root package folder) acts as a "settings" or "main controller" for your entire application, and so the system needs to be aware of each activity (essentially a page) that you will transition to or use.
Here are other things that can go within the Activity tag in the Manifest file.
Side Note: as #Tyler M. says, you should use another name than "View" for your Activity.
Rename your Activity subclass to something other than View.
It is generally a bad idea to use names of existing classes - it is a reference disaster waiting to happen.
You most likely forgot to include it in the Manifest.xml
I'm very new with android development. I'm trying to develop an application, I decided to try out the code below but I keep getting errors when I try to open it on the emulator. please can anyone tell me what I'm doing wrong?
package hajara.android.MyRecipes;
import android.app.Activity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MyRecipesActivity extends Activity {
Button btn;
TextView t1, t2;
EditText e;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
t1 = (TextView)findViewById(R.id.text1);
t2 = (TextView)findViewById(R.id.text2);
e = (EditText)findViewById(R.id.edit1);
btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener((OnClickListener) this);
}
}
My main.xml file is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter a string:"
/>
<EditText android:id="#+id/edit1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cursorVisible="true"
android:editable="true"
android:singleLine="true"
/>
<Button android:id="#+id/button1"
android:text="OK"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/text2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
</LinearLayout>
Your onClick listener is done incorrectly. I find it easier to simply create a method such as:
public void buttonOnClick(View v) {
// Do something
}
and within your XML layout file (ie. main.xml) invoke the onClick attribute:
<Button
...
android:onClick="buttonOnClick"
...
/>
I think you haven't Declared button Properly.
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Your Code Goes here
}
});
And Hope you have Declared your Activity in Manifest File.