views:

355

answers:

3

Hi, I am pretty new to programming and just to try Andorid. I am trying to create an simple app that ask after a string from the user and after that I want my program to go out on the internet with the web-browser.

But I get 2 things wrong, one thing is my way to go on the internet but that I know I chose the wrong method, but the other thing is that findViewById(R.id. ) don't work, and I cant understand why..

My code (from http://pastebay.com/77559):

package team.ice.kth;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;

public class KTH extends Activity {
 private Button mButton;
 private EditText mClassInfo;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  mClassInfo = (EditText) findViewById(R.id.ClassInfo);

  final Button mButton = (Button) this.findViewById(R.id.OK_Button);
  mButton.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
    onGoTo();
   }
   private void onGoTo() {
    createUrl();
   }
   protected void createUrl() {
    String Class = mClassInfo.getText().toString();

    WebView webview = new WebView(this);
    setContentView(webview);
    webview.loadUrl("http://schema.sys.kth.se/4DACTION/WebShowSearch/2/1-0?wv_type=5&wv_category=0&wv_ts=20091220T015342X7921&wv_search=" + Class + "&wv_startWeek=935&wv_stopWeek=951&wv_first=0&wv_addObj=&wv_delObj=&wv_obj1=19498000&wv_graphic=Grafiskt+format");
   }
  });
 }
}

and my XML (from http://pastebay.com/77560):

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/widget0"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent" >
 <EditText
  android:id="@+id/class"
  android:layout_width="96px"
  android:layout_height="wrap_content"
  android:textSize="18sp"
  android:layout_x="120px"
  android:layout_y="43px" />
 <TextView
  android:id="@+id/ClassInfo"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Class:"
  android:layout_x="62px"
  android:layout_y="58px" />
 <Button
  android:id="@+id/OK_Button"
  android:layout_width="47px"
  android:layout_height="39px"
  android:text="Sök"
  android:layout_x="237px"
  android:layout_y="52px" />
</AbsoluteLayout>

Would appreciate so I cant get this ghost out of my head, my knowledge cant get me any further.

A: 

As for directing the user to the internet, what is it that you want here? A webview is good for showing a piece of web content within an app, but if you just want to open the default browser from your app and point it to a URL you could just use this:

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com/")));

As for the findViewById, they look about right, what error message are you getting, or if none, what is it that doesn't work with it?

If the id's you're after doesn't exist in R.id, there might be some error in your main.xml (actually, i think errors elsewhere could interrupt R from being properly generated as well), so you might want to check eclipse for compiler errors.

Please describe the findViewById problem in a bit more detail, and you could also show us some of your layout xml code.

David Hedlund
Eclipse message to me is R.id cannot be resolved, has posted the XML file on comment, Thanks for helping me!
Stefan
When I paste your XML code into eclipse, and hit save, it shows an error message saying *invalid symbol 'class'*. turns out *class* is a reserved keyword that cannot be used for an id here. replacing that with anything else (i used *class1*) solved the problem. the fact that the xml file was, then, not valid, would cause your `R` class to not be generated properly, and that is most likely the casue of your error.
David Hedlund
Thanks for the help, got it to work, but now I have some problem with the button that the debug.. But BIG THANKS for the help!
Stefan
A: 

Your problem with the button (in the comments rather than the main question above) might have to do with how you declare the button twice. Using the numbers from PasteBay, you first decalre the button on line 12, then again on 26 when you run the findViewById. Try getting rid of the first one. If that doesn't help, more detail about the issue might let people more experienced than me (I'm a beginner too :p) find the issue.

Steve H
Has tried to do that, the pastebay code is following an example from google, but don't work, but thanks for trying!
Stefan
+1  A: 

The problem with findViewById() you're trying to read the input from the wrong View. According to the XML your EditText would be refered to as R.id.class, not R.id.ClassInfo (which is your TextView). You'd need to modify your code to something like:

public class KTH extends Activity {
 private Button   mButton;
 private EditText mClass;      // this will be a handle to the editable text area
 private TextView mClassInfo;  // this will be a handle to the static text area which reads "Class:"

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  mClassInfo = (TextView) findViewById(R.id.ClassInfo);  // 'ClassInfo' is a TextView in the XML
  mClass     = (EditText) findViewById(R.id.class    );  // 'class' is an EditText in the XML
  mButton    = (Button  ) findViewById(R.id.OK_Button);

  mButton.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
    onGoTo();
   }
   private void onGoTo() {
    createUrl();
   }
   protected void createUrl() {
    String Class = mClass.getText().toString();

    WebView webview = new WebView(this);
    setContentView(webview);
    webview.loadUrl("http://schema.sys.kth.se/4DACTION/WebShowSearch/2/1-0?wv_type=5&amp;wv_category=0&amp;wv_ts=20091220T015342X7921&amp;wv_search=" + Class + "&wv_startWeek=935&wv_stopWeek=951&wv_first=0&wv_addObj=&wv_delObj=&wv_obj1=19498000&wv_graphic=Grafiskt+format");
   }
  });
 }
}

As David noted above, you should really find something else (anything else) to name that EditText to other than "class", but it sounds like you probably have already.

With regard to the web site not loading, could you tell us how it's failing? What's going wrong?

fiXedd
Thanks! I have done like david told me to do, but now I have problem with the button, eciplse dont tell me something is wrong but I cant run the program.When I run it, it tells me "Your project contains error(s), please fix them before running your application." and runns out on the consol "Project has no target set. Edit the project properties to set one."
Stefan