0

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)
1
  • Have you initialized goNextBtn in your ItemActivity? Not sure if this is the problem, cause I don't understand the point of making a different class for your button. Commented Feb 19, 2016 at 1:34

2 Answers 2

1

Assuming that that is all your code within layout/activity_item, the problem is here:

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

The first line is what assigns your layout/activity_item.xml as the layout for the ItemActivity. The second looks inside that layout file to find a view with the id "response". However, there is none because the only view in that layout is a Button with the id "internet_button".

I'm also curious, is there a reason you want to make a class that makes Buttons? You could make a method that does the same thing and returns the Button.

Update:

@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
    ...
}

However, if reusing that Button is all you want then maybe you should make your class extend Button and not Activity. That way you can simply instantiate that Button when you need it. Check out this post to see how to do that:

Sign up to request clarification or add additional context in comments.

5 Comments

I was intending to make a class like that for all activities. Is it an appropriate approach?
sorry , I just copied part of the layout in my original post. Actually there is a textview in the layout. But the error seems to come from the ButtonInternetAccess class. I have updated the post
Did you implement the onCreate method in the ButtonInternetAccess? If you didn't, then do that and move the code you have in the main() method there. This should look similar to the one in ItemActivity. I updated my post to show you.
I updated it again, read the bottom part where I give you my suggestion for an approach.
,thank you for your suggestions, still working on the error.
1

You need to implement that listener in all of your activities.

You could write a custom header layout for your application (/res/layout/header.xml), in which you have the "bia" button with a click listener set (pointing to an onBIAClicked method):

android:onClick="onBIAClicked "

Then you include this header to each activity layout:

include android:id="@+id/header" layout="@layout/header"

You could also create an interface which contains an onBIAClicked method declaration, and all your activities implementing that interface need to define the onBIAClicked method's body.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.