2009-12-21 51 views
0

我很新的編程,只是嘗試Andorid。 我試圖創建一個簡單的應用程序,詢問用戶的字符串之後,然後我希望我的程序通過網絡瀏覽器在互聯網上出去。如何使用用戶提供的URL調用Web瀏覽器?

但是我弄錯了兩件事,一件事是我上網的方式,但我知道我選擇了錯誤的方法,但另一件事是findViewById(R.id。)不起作用,並且我無法理解爲什麼..

我的代碼(從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"); 
      } 
     }); 
    } 
} 

和我的XML(從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> 

將不勝感激這麼我不能把這個鬼魂從我的腦海中解救出來,我的知識不能再讓我進一步了。

+1

嗨!你可以發佈你的main.xml佈局文件嗎? – Ramps 2009-12-21 09:18:07

+0

嗨,XML文件http://pastebay.com/77560 – Stefan 2009-12-21 11:52:28

回答

0

至於引導用戶上網,你想要什麼?網頁視圖是很好的顯示應用程序內的一塊網頁內容,但如果你只是想從您的應用程序打開默認的瀏覽器,並指向你可以只使用這樣的URL:

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

對於findViewById,它們看起來是正確的,你得到了什麼錯誤信息,或者如果沒有,它不起作用的是什麼?

如果R.id不存在的id你後,有可能對你main.xml(實際上,我認爲錯誤在其他地方可能會中斷被正確生成以及R)一定的誤差,所以你可能要檢查eclipse發現編譯器錯誤。

請詳細描述findViewById問題,並且您還可以向我們展示一些您的佈局xml代碼。

+0

Eclipse消息給我的是R.id無法解析,已發佈XML文件發表評論,感謝幫助我! – Stefan 2009-12-21 11:53:49

+1

當我將XML代碼粘貼到eclipse中並點擊保存時,它會顯示一條錯誤消息,指出*無效符號'class'*。結果* class *是一個保留關鍵字,不能用於這裏的id。用其他任何東西替換(我用* class1 *)解決了這個問題。 xml文件無效的事實將導致您的R類無法正確生成,這很可能是您錯誤的遺留問題。 – 2009-12-21 12:17:06

+0

感謝您的幫助,得到它的工作,但現在我有一些問題與調試按鈕..但非常感謝幫助! – Stefan 2009-12-21 12:33:36

0

按鈕(在註釋中而不是上面的主要問題中)的問題可能與您如何聲明按鈕兩次有關。使用PasteBay中的數字,首先在12行上解除按鈕,然後再在26上運行findViewById。嘗試擺脫第一個。如果這沒有幫助,關於這個問題的更多細節可能讓人們比我更有經驗(我也是一個初學者:p)找到問題。

+0

已經嘗試做到這一點,pastebay代碼是從谷歌的例子,但不工作,但謝謝你的嘗試! – Stefan 2009-12-21 19:29:09

1

findViewById()的問題您嘗試讀取來自錯誤的View的輸入。根據XML,您的EditText將被稱爲R.id.class,而不是R.id.ClassInfo(這是您的TextView)。你需要修改代碼以類似:

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

正如大衛上面提到的,你應該找點別的事情(別的)來命名EditText比「類」等,但它聽起來像是你可能已經。

關於網站未加載,你能告訴我們它是如何失敗?出了什麼問題?

+0

謝謝!我已經做了像大衛告訴我做的,但現在我有按鈕的問題,eciplse不告訴我有什麼不對,但我不能運行該程序。 當我運行它時,它告訴我「您的項目包含錯誤,請在運行您的應用程序之前修復它們。」並在控制檯上運行「項目沒有目標設置,編輯項目屬性設置一個」。 – Stefan 2009-12-21 20:54:48

0

爲您的r.id.如果您有任何疑問,請點擊以下鏈接:

這可能對您有所幫助。

Herehere