[help] OnClick Button Errors

Reneg4d3

Member
Joined
May 12, 2010
Messages
50
Reaction score
0
Location
Kentucky
Hello guys, I have been teaching my self to "code" in the android language and java over the past few day. So I am new to this so please be patient.

Basically, my problem is I am trying to get a button on my form to open a web page.

Here is my java file.
Code:
package com.error33.test;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
public class test extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
         Button loginBtn = (Button) findViewById(R.id.login_Button);
    }
} 
loginBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View pV) {
        LoginActivity.this.login();
    }
});
private void login() {
    WebView.loadUrl("http://www.google.com")
}
I am getting errors on the following lines in order:
Line 15
Code:
  Button loginBtn = (Button) findViewById(R.id.login_Button)
- Button cannot be resolved to a type

Line 16
Code:
}
- Syntax error on token "}", { expected

Line 18
Code:
loginBtn.setOnClickListener(new View.OnClickListener() {
- View cannot be resolved to a type

Line 23
Code:
});
- Syntax error, insert "}" to complete MethodBody and Line breakpoint:WalmartSchedule [line: 23] - onCreate

Line 25
Code:
WebView.loadUrl("http://www.google.com")
- Syntax error, insert ";" to complete BlockStatements and Cannot make a static reference to the non-static method loadUrl(String) from the type WebView

Line 26
Code:
}
- Syntax error, insert "}" to complete ClassBody


Here is my main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
android:id="@+id/login_Button"
android:layout_width="300px"
android:layout_height="wrap_content"
android:text="Goto Login Page"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#ffffff00"
android:layout_x="10px"
android:layout_y="120px"
>
</Button>
<Button
android:id="@+id/schedule_Button"
android:layout_width="300px"
android:layout_height="wrap_content"
android:text="View Schedule"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#ffffff00"
android:layout_x="10px"
android:layout_y="297px"
>
</Button>
<TextView
android:id="@+id/Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wal-Mart Schedule"
android:textSize="25sp"
android:typeface="sans"
android:textStyle="bold"
android:textColor="#ff0033ff"
android:layout_x="40px"
android:layout_y="36px"
>
</TextView>
<EditText
android:id="@+id/monthInput"
android:layout_width="50px"
android:layout_height="wrap_content"
android:text="#"
android:textSize="18sp"
android:layout_x="48px"
android:layout_y="220px"
>
</EditText>
<EditText
android:id="@+id/dayInput"
android:layout_width="50px"
android:layout_height="wrap_content"
android:text="#"
android:textSize="18sp"
android:layout_x="131px"
android:layout_y="220px"
>
</EditText>
<EditText
android:id="@+id/yearInput"
android:layout_width="70px"
android:layout_height="wrap_content"
android:text="#"
android:textSize="18sp"
android:layout_x="210px"
android:layout_y="220px"
>
</EditText>
<TextView
android:id="@+id/monthTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Month"
android:layout_x="50px"
android:layout_y="190px"
>
</TextView>
<TextView
android:id="@+id/dayTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day"
android:layout_x="141px"
android:layout_y="190px"
>
</TextView>
<TextView
android:id="@+id/yearTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Year"
android:layout_x="227px"
android:layout_y="190px"
>
</TextView>
<TextView
android:id="@+id/widget224"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Created by: "
android:layout_x="149px"
android:layout_y="410px"
>
</TextView>
</AbsoluteLayout>
Any help would be greatly appriciated!
 
Your imports is incomplete
Import android.widget.Button

The tutorials suggest Ctrl+Shift+O in Eclipse to automatically add imports.

Also, you can point your cursor at red underlined errors in your code for suggestions to fix the problem.

Sent from my DROID2 GLOBAL using DroidForums App
 
Try something like this for the button.

Code:
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class test extends Activity implements OnClickListener{

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		handler.sendEmptyMessage(0);
	}

	public void OnClickListen() {		
		Button loginBtn =(Button)findViewById(R.id.login_Button);		
		loginBtn.setOnClickListener(this);
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			setContentView(R.layout.main);
			OnClickListen();
			return;
		}
	};

	public void onClick(View view) {
		
		switch (view.getId()) {
		case R.id.login_Button:
			// do your work here
		break;
		}
	}
}
 
jRummy is right. what you were missing is params in your onClick
you NEED to have onClick(View view) {
or
runThis(View view) {

you can't just do onClick() or runThis().

edit----

just realized i may have revived a dead thread. my bad! was trying to help :p
 
jRummy is right. what you were missing is params in your onClick
you NEED to have onClick(View view) {
or
runThis(View view) {

you can't just do onClick() or runThis().

edit----

just realized i may have revived a dead thread. my bad! was trying to help :p
You didn't revive a dead thread, I have been checking this for months trying to find an explanation. Thanks :) I will see what I can do now.

So what do I need to change in my code? I am not just simply running onClick() I have 2 parameters listed in the onClick.
 
Back
Top