I'm trying to make a button class that creates buttons that fire up an intent to the phone's browser when clicked, but I'm receiving java.lang.NullPointerException when launched on my device. I read that it may be caused by findViewById returning null since it was referring to a wrong View. I was looking for a way to pass R from ItemActivity to ButtonInternetAcces but I read that it is not necessary. There is no error in eclipse so I'm confused as to what to fix the problem.
Here's the line that is probably causing the error:
Button goNextBtn = (Button) findViewById(R.id.internet_button);
class 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 class 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");
}
//............
}
layout/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>
Error Log:
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)