2013-03-30 54 views
0

我不確定爲什麼,但按鈕在點擊時不起作用。它只是說該活動已停止工作,然後退出程序。它的編譯水平爲8 api,目標水平爲17 api。按鈕點擊時打開頁面

package com.example.button; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 

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


public void open(MainActivity view) { 
    view.loadUrl("http://www.yahoo.org"); 
} 

private void loadUrl(String string) { 
    // TODO Auto-generated method stub 

} 
} 


<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_alignRight="@+id/textView1" 
    android:layout_below="@+id/textView1" 
    android:layout_marginTop="38dp" 
    android:onClick="open" 
    android:text="Button" /> 

+0

你可以發佈這個java代碼的xml嗎? – danielcooperxyz

+1

張貼您的XML,否則沒有人可以建議你修復bug – Sri

+0

好的,我將它添加到原來的副本 –

回答

0
public void open(MainActivity view) { 
    view.loadUrl("http://www.yahoo.org"); 
} 

Activity沒有方法loadUrl。好像你在這裏嘗試使用WebView

-

不過,既然你不叫open任何地方這應該不會崩潰的應用程序。這是你的全部代碼嗎?

+0

沒有我添加了XML文件 –

0

如果您的方法打開用於onClick屬性,請在XML中設置,然後參數不正確。一個onClick方法應採取View view並不是一項活動,像這樣: XML:

<Button onClick="onClick"/> 

的Java:

public void onClick(View view) 
{ 
    //do something when button is clicked 
} 

這種看法傳遞是被點擊的按鈕。 要打開在web視圖特定URL使用以下代碼中的onClick方法:

上web視圖
((WebView)findViewById(android.R.id.WebView)).loadUrl("http://slashdot.org/"); 

的更多信息可以發現here

0

哪裏是按鈕?? webview在哪裏(當你調用一個網頁或網址來查看你的活動時)?分享你的代碼,也檢查Android清單文件..如果你正從一個活動轉移到另一個活動需要提到像這樣:

<activity 
      android:name=".main" 
      android:label="@string/title_activity_main" 
      android:screenOrientation="landscape" 
      android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 
      > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <activity android:name=".SecondPage" 
        android:screenOrientation="landscape" 
      android:theme="@android:style/Theme.NoTitleBar.Fullscreen"> 
     </activity>