First simple Android app crashed by click on a button - android

my fist very simple android app is crashed, if I press my button. It's just a test. If the button click is working, I want to put an sql query in myClick function.
package com.example.connectdroid;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
void myClick(){
//here is my problem:
LinearLayout lView = (LinearLayout)findViewById(R.id.mylinear);
TextView selection = (TextView) findViewById(R.id.editText1);
String mytext = "apple";
selection.setText(mytext);
}
}
The xml:
<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: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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:text="Button"
android:onClick="myClick" />
<LinearLayout
android:id="#+id/mylinear"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button1"
android:layout_marginLeft="17dp"
android:layout_marginTop="57dp" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/mylinear"
android:layout_alignLeft="#+id/button1"
android:layout_marginBottom="22dp"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
</RelativeLayout>

void myClick(){
should be
public void myClick(View view){
Your app is crashing because you declared android:onClick="myClick" but the signature in the java file of myClick is wrong

thats true dear blackbelt,
The method called onClick should be following features
public method and
the method should contain View parameter

Related

i found out that my R.java contains nothing, can someone tell me what to be written in R.java

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.

How to add a ShowcaseView that pops up only for the first time to show how to use our app?

I wanted to do some thing like this as shown in image below,Is there any widget to do this or we need to add a view & make it transparent.
Thanks .
you must be create a custom view for it and make it transparent..
Main Activity:
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.Dialog;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initPopup();
}
#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;
}
public void initPopup() {
final Dialog view = new Dialog(this);
view.requestWindowFeature(Window.FEATURE_NO_TITLE);
view.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
view.setContentView(R.layout.transparent_layout);
view.setCanceledOnTouchOutside(true);
view.show();
RelativeLayout rl_chooseone_menu_main = (RelativeLayout) view
.findViewById(R.id.rl_chooseone_menu_main);
rl_chooseone_menu_main.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
view.dismiss();
}
});
/*Handler handler = null;
handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
view.cancel();
view.dismiss();
}
}, 3000);*/
}
}
activity_main.xml:
<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: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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
This is the Transparent View name is transparent_layout.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"
android:background="#android:color/transparent" >
<RelativeLayout
android:id="#+id/rl_chooseone_menu_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/imageView1"
android:layout_marginRight="30dp"
android:text="Tap to view all" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="26dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/imageView2"
android:layout_alignRight="#+id/imageView2"
android:text="Tap to place your order" />
</RelativeLayout>
</RelativeLayout>
Enjoy Brother,i have used dimmy images,replace it with your own images..
You could make a tutorial Activity with a semi-transparent background, containing ImageViews and/or TextViews as shown in your example.
Tutorial activity's layout
<LinearLayout ...
android:background="#color/semitransparent"
... >
...
colors.xml
...
<color name="semitransparent">#80000000</color>
...
Then just start it on top of your usual Activity if you notice this is the first launch of your app by the user.

Android Button Doing Opposite of Command

So I just started using the Android Development kit and learned how to create buttons and get them to work. To test what I know I made a simple app that displays "correct" if you click the button on the right and "idiot" if you click the one on the left. Everything is working other than the fact that it's displaying the opposite message for the button clicked.
Here is my xml code:
<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: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" >
<TextView
android:id="#+id/textv1"
android:layout_width="149dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp"
android:text="Click the Right Button" />
<Button
android:id="#+id/left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textv1"
android:layout_marginTop="29dp"
android:layout_toLeftOf="#+id/textv1" />
<Button
android:id="#+id/right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/left"
android:layout_alignBottom="#+id/left"
android:layout_toRightOf="#+id/textv1" />
Here is my Java code:
package com.clicktherightbutton;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button rightb;
Button leftb;
TextView Display;
String yes = "Correct!!!";
String no = "Idiot!!!";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rightb=(Button)findViewById(R.id.right);
leftb=(Button)findViewById(R.id.left);
Display=(TextView)findViewById(R.id.textv1);
rightb.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
if(rightb.isPressed())
Display.setText(yes);
rightb.setEnabled(false);
leftb.setEnabled(false);
}
});
leftb.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
if(leftb.isPressed())
Display.setText(no);
rightb.setEnabled(false);
leftb.setEnabled(false);
}
});
}
#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;
}
}
I understand if I switch "left" and "right" the buttons will do as I want but I want to know why it's doing the opposite of what I coded it to do? Is something wrong with my code?
Thank you
Change your layout to:
<TextView
android:id="#+id/textv1"
android:layout_width="149dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp"
android:text="Click the Right Button" />
<Button
android:id="#+id/left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textv1"
android:layout_marginTop="29dp"
android:layout_toLeftOf="#id/textv1" />
<Button
android:id="#+id/right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/left"
android:layout_alignBottom="#id/left"
android:layout_toRightOf="#id/textv1" />
You can only declare #+id once - it creates a variable. Later use #id to reference the same variable.

Defining text view to the corresponding id in android?

Found the solution : Declared textview in the oncreateOptionMenu instead of Oncreate method.
Problem : It generates an Multiple marker error when i define text view for the corresponding id.
TwitterSearchActivity.java
package com.example.twittersearchactivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class TwitterSearchActivity extends Activity {
TextView tweet;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_twitter_search);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.twitter_search, menu);
return true;
}
tweet=(TextView)findViewById(R.id.tweet_txt);
}
activity_twitter_search.xml
<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: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=".TwitterSearchActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world"
/>
<TextView
android:id="#+id/intro_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textStyle="italic"
android:text="#string/intro"
/>
<EditText
android:id="#+id/search_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="#string/hint"
android:layout_below="#+id/intro_txt"
android:padding="10dp"
android:background="#ffff66"
android:layout_margin="5dp"
/>
<Button
android:id="#+id/search_btn"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/search_label"
android:layout_below="#+id/search_edit"
android:onClick="searchTwitter"
android:layout_margin="5dp"
/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/search_btn">
<TextView
android:id="#+id/tweet_txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
android:padding="10dp"
android:background="#330000"
android:textColor="#ffffff"
android:layout_margin="5dp"
android:text="#string/placeholder"
android:freezesText="true"
/>
</ScrollView>
</RelativeLayout>
Error
Multiple markers at this line
- Syntax error on token ".", ... expected
- Syntax error, insert ";" to complete FieldDeclaration
- Syntax error on token "tweet", VariableDeclaratorId expected after this token
- Return type for the method is missing
- Syntax error, insert "}" to complete ClassBody
Put
tweet=(TextView)findViewById(R.id.tweet_txt);
within your onCreate() method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_twitter_search);
tweet=(TextView)findViewById(R.id.tweet_txt);
}
Your Button and Your ScrollView got the same id : search_btn. DonĀ“t know if this is the issue for Your problem, but should be fixed

Source attachment does not contain the source for the file view.class

I am new to android and making a simple application of a temperature converter. i had placed 2 text views and a button on the screen. i just wanted to saw that when i press the button an event comes or not when i run the application no event came after pressing the button so i put it in debug mode then it gives a message
Source Not Found:Source attachment does not contain the source for the file view.class
Here is my code:
Activity_main.Xml
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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#color/red"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
style="#style/AppTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="136dp"
android:layout_marginLeft="94dp"
android:onClick="onclick"
android:text="#string/Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="34dp"
android:layout_marginTop="38dp"
android:text="#string/Large_Text"
android:textStyle="italic"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="42dp"
android:text="#string/Large_Text1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/textView2"
android:ems="10"
android:inputType="numberSigned" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView1"
android:layout_toRightOf="#+id/button1"
android:ems="10"
android:inputType="numberSigned" />
</RelativeLayout>
Strings.Xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Converter</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="Button">Convert</string>
<string name="Large_Text">Deg C</string>
<string name="Large_Text1">Deg F</string>
<color name="red">#D3D3D3</color>
</resources>
Main_activity.java
package com.example.converter;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
}
I have created a classs1.java
package com.example.converter;
import android.app.Activity;
import android.widget.TextView;
public class Classs1 extends Activity{
public void onclick(){
setContentView(R.layout.activity_main);
TextView t = (TextView)findViewById(R.id.textView1);
t.setText(4);
}
}
U did not call event listener to ur view
public class Classs1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
btn = (Button) findViewById(R.id.ur_button_id);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
setContentView(R.layout.activity_main);
TextView t = (TextView)findViewById(R.id.textView1);
t.setText(4);
}
});
}
Edit:
Error : Source Not Found:Source attachment does not contain the source for the file view.class
Did u add classs1.java in your manifest file Plz check that too.

Categories

Resources