-1

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>
Lucifer
  • 29,392
  • 25
  • 90
  • 143
  • 1
    Please upload the error log as well – Lucifer May 06 '14 at 07:29
  • Where is your `R.id.editText1` in xml ? – Lucifer May 06 '14 at 07:30
  • Related: http://stackoverflow.com/questions/4153517/how-exactly-does-the-androidonclick-xml-attribute-differ-from-setonclicklistene, and some explanation from [Developers Guide](http://developer.android.com/training/basics/firstapp/starting-activity.html#RespondToButton) – Andrew T. May 06 '14 at 07:34
  • you can call edittext ID in TextView ... TextView selection = (TextView) findViewById(R.id.textView1)...use this – Amardeepvijay May 06 '14 at 07:35

2 Answers2

1
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

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

thats true dear blackbelt,

The method called onClick should be following features

  1. public method and
  2. the method should contain View parameter
tizbn
  • 1,907
  • 11
  • 16