2016-02-19 17 views
0

我試圖做一個按鈕類,創建按鈕,點擊時觸發了手機瀏覽器的意圖,但我在設備上啓動時收到java.lang.NullPointerException。我讀到它可能是由於findViewById返回null而引起的,因爲它指的是錯誤的View。我正在尋找一種方法來將ItemActivity中的R傳遞給ButtonInternetAcces,但我讀到它並不是必需的。 eclipse沒有錯誤,所以我很困惑要解決什麼問題。創建按鈕的類,但得到NullException錯誤

這裏有可能是導致該錯誤的行:

Button goNextBtn = (Button) findViewById(R.id.internet_button);

ButtonInternetAccess

public class ButtonInternetAccess extends Activity{ 

    public Button main(final String url){ 

     Button goNextBtn = (Button) findViewById(R.id.internet_button); 

     OnClickListener goToNet = new OnClickListener(){ 

     @Override 
     public void onClick(View v){ 
      Intent net = new Intent(Intent.ACTION_VIEW); 
      net.setData(Uri.parse(url)); 
      startActivity(net); 
     } 

     }; 

     goNextBtn.setOnClickListener(goToNet); 

     return goNextBtn; 
    } 
} 

Activity類ItemActivity

public class ItemActivity extends ActionBarActivity{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_item);  
     TextView tvResponse = (TextView) findViewById(R.id.response); 
     Intent i = getIntent(); 
     Bundle b = i.getExtras(); 
     String mid = b.getString("mid"); 
     tvResponse.setText(mid); 

     ButtonInternetAccess bia = new ButtonInternetAccess(); 

     Button goNextBtn = bia.main("http://www.google.com"); 

    } 
    //............ 

} 

佈局/ activity_item:

<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="com.app.ItemActivity" > 

    <TextView 
     android:id="@+id/response" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/second.welcome" /> 

    <Button 
     android:id="@+id/internet_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/response" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="70dp" 
     android:text="Button" /> 

</RelativeLayout> 

錯誤日誌:

02-19 08:24:06.735: E/AndroidRuntime(9802): FATAL EXCEPTION: main 
02-19 08:24:06.735: E/AndroidRuntime(9802): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app/com.app.ItemActivity}: java.lang.NullPointerException 
02-19 08:24:06.735: E/AndroidRuntime(9802):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 
02-19 08:24:06.735: E/AndroidRuntime(9802):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
02-19 08:24:06.735: E/AndroidRuntime(9802):  at android.app.ActivityThread.access$600(ActivityThread.java:130) 
02-19 08:24:06.735: E/AndroidRuntime(9802):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
02-19 08:24:06.735: E/AndroidRuntime(9802):  at android.os.Handler.dispatchMessage(Handler.java:99) 
02-19 08:24:06.735: E/AndroidRuntime(9802):  at android.os.Looper.loop(Looper.java:137) 
02-19 08:24:06.735: E/AndroidRuntime(9802):  at android.app.ActivityThread.main(ActivityThread.java:4745) 
02-19 08:24:06.735: E/AndroidRuntime(9802):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-19 08:24:06.735: E/AndroidRuntime(9802):  at java.lang.reflect.Method.invoke(Method.java:511) 
02-19 08:24:06.735: E/AndroidRuntime(9802):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
02-19 08:24:06.735: E/AndroidRuntime(9802):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
02-19 08:24:06.735: E/AndroidRuntime(9802):  at dalvik.system.NativeStart.main(Native Method) 
02-19 08:24:06.735: E/AndroidRuntime(9802): Caused by: java.lang.NullPointerException 
02-19 08:24:06.735: E/AndroidRuntime(9802):  at android.app.Activity.findViewById(Activity.java:1825) 
02-19 08:24:06.735: E/AndroidRuntime(9802):  at com.component.ButtonInternetAccess.main(ButtonInternetAccess.java:16) 
02-19 08:24:06.735: E/AndroidRuntime(9802):  at com.app.ItemActivity.onCreate(ItemActivity.java:30) 
02-19 08:24:06.735: E/AndroidRuntime(9802):  at android.app.Activity.performCreate(Activity.java:5008) 
02-19 08:24:06.735: E/AndroidRuntime(9802):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
02-19 08:24:06.735: E/AndroidRuntime(9802):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
+0

您是否在ItemActivity中初始化了goNextBtn?不知道這是否是問題,因爲我不明白爲您的按鈕製作不同類的要點。 –

回答

1

假設是佈局/ activity_item內所有的代碼,問題就在這裏:

setContentView(R.layout.activity_item);  
TextView tvResponse = (TextView) findViewById(R.id.response); 

第一行是什麼賦予你的佈局/ activity_item.xml作爲ItemActivity的佈局。第二個查看該佈局文件以查找ID爲「響應」的視圖。但是沒有,因爲該佈局中的唯一視圖是ID爲「internet_button」的Button。

我也很好奇,是否有一個原因,你想讓一個類,使按鈕?你可以做一個方法來做同樣的事情並返回Button。

更新:

@Override 
protected void onCreate(Bundle savedInstanceState) { //put the code you had in main() here, after these two lines 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_item); //this should be the layout for this class; in this layout you should have the internet button 
    ... 
} 

但是,如果重複使用該按鈕是你想要的,那麼也許你應該讓你的類擴展按鈕,而不是活動。這樣,您可以在需要時簡單實例化該按鈕。查看this的帖子,看看如何做到這一點:

+0

我打算爲所有活動製作這樣的課程。這是一個合適的方法嗎? – RedGiant

+0

對不起,我剛剛在原始文章中複製了部分佈局。其實在佈局中有一個textview。但錯誤似乎來自'ButtonInternetAccess'類。我已更新信息 – RedGiant

+0

您是否在ButtonInternetAccess中實現了onCreate方法?如果你沒有,那麼做那個,然後把你在main()方法中的代碼移到那裏。這看起來應該與ItemActivity中的類似。我更新了我的帖子以向你展示。 – Dave

1

你需要在你所有的活動中實現這個監聽器。

你可以寫你的應用程序(/res/layout/header.xml)自定義標題佈局,在其中您有「BIA」按鈕,點擊收聽集(指向一個onBIAClicked法):

機器人:的onClick = 「onBIAClicked」

然後,你包括此標頭到每個活動佈局:

包括機器人:ID = 「@ + ID /報頭」 佈局= 「@佈局/報頭」

你也可以創建一個包含onBIAClic的接口ked方法聲明,並且實現該接口的所有活動都需要定義onBIaclicked方法的主體。