开发者

How can I invoke the web browser with a user-supplied URL?

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.i开发者_如何学JAVAd.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.


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&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");
            }
        });
    }
} 

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?


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.


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.


For your r.id. problem you have follow below links :

It may be helpful to you.

Here or here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜