Dhasneem
Dhasneem

Reputation: 4007

Issue on calling function of Fragment Class from Activity

I am writing an application using ActionBarSherlock and viewPagerIndicator.

My Aim is:

To launch a Dialogue Activity(AddDialogue.java), when I am selecting menu item from MainScrenActivity.java. By Pressing the button in AddDialogue.java, it should check for network connection and display the items in listview.

In AddDialogue.java I am using the updateListview() method, which is defined in testFragment class.

AddDialogue.java:

public class AddDialogue extends SherlockFragmentActivity {
    private static final int THEME = R.style.Theme_Sherlock_Light_Dialog;
    private Button goButton;
    private String strurl = "http://feeds.hindustantimes.com/HT-IndiaSectionPage-Topstories";
    EditText editText;
    TestFragment testFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        setTheme(THEME); // Used for theme switching in samples
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_dialogue);
        editText = (EditText) findViewById(R.id.input);
        this.goButton = (Button) this.findViewById(R.id.go);
        this.goButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                TestFragment testFragment = new TestFragment();
                Toast.makeText(getBaseContext(), "ok is clicked",
                        Toast.LENGTH_SHORT).show();
                Log.d("dialogue", "ok is clicked");
                testFragment.updateListView(strurl);
                finish();
            }
        });
    }
}

TestFragment.java

public final class TestFragment extends Fragment {
    private static final String KEY_CONTENT = "TestFragment:Content";

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
} 
...

public void updateListView(String strurl) {
        Log.d(TAG,"Url in updateListView : " + strurl);
        if (isOnline()) {
            animationLoadingImage.start();
            loadingImage.setVisibility(View.VISIBLE);
            new RequestTask().execute(strurl);
            // openMenu(view);
        } else {
            // openMenu(view);
            Toast.makeText(getActivity(),
                    "There is no network connection" + TAG, Toast.LENGTH_SHORT)
                    .show();
        }
    }
public boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) getActivity()
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        }
        return false;
    }
}

My Problem is:

I am receiving following logs, when I pressed button from activity.

FATAL EXCEPTION: main
E/AndroidRuntime( 2316): java.lang.NullPointerException
E/AndroidRuntime( 2316):    at com.mistral.droidcast.TestFragment.isOnline(TestFragment.java:363)
E/AndroidRuntime( 2316):    at com.mistral.droidcast.TestFragment.updateListView(TestFragment.java:348)
E/AndroidRuntime( 2316):    at com.mistral.droidcast.AddDialogue$1.onClick(AddDialogue.java:45)
E/AndroidRuntime( 2316):    at android.view.View.performClick(View.java:2485)
E/AndroidRuntime( 2316):    at android.view.View$PerformClick.run(View.java:9080)
E/AndroidRuntime( 2316):    at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime( 2316):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 2316):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 2316):    at android.app.ActivityThread.main(ActivityThread.java:3687)
E/AndroidRuntime( 2316):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 2316):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 2316):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
E/AndroidRuntime( 2316):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
E/AndroidRuntime( 2316):    at dalvik.system.NativeStart.main(Native Method)
E/        (  122): Dumpstate > /data/log/dumpstate_app_error
W/ActivityManager(  122):   Force finishing activity com.mistral.droidcast/.AddDialogue

I know that, i couldn't access any view's from TestFragment.java and missing something in my code. But, I don't know what is that.

Please help me to solve it.

Thank u in Advance!!!

Upvotes: 0

Views: 958

Answers (1)

user559730
user559730

Reputation:

Please check what happened at the line TestFragment.isOnline(TestFragment.java:363)

My guess is getActivity() returns null. It means that your fragment hasn't attached to your activity. You will need to add fragment to the activity as descibed here How do I add a Fragment to an Activity with a programmatically created content view

Upvotes: 2

Related Questions