0

I am developing a simple Android Application with 2 tabs. I have implemented the Tabs very well.

Tab1 consists of Fragment 1 Tab2 consists of Fragment 2

Now i want to add a button in Fragment 1, and when the user clicks on that button, some activity is displayed.

I have tried the following code, and it is not working.

MainActivity.java

package com.adhish.pagertabstriptutorial;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from activity_main.xml
        setContentView(R.layout.activity_main);

        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setLogo(R.drawable.ic_launcher);
        getSupportActionBar().setDisplayUseLogoEnabled(true);

        // Locate the viewpager in activity_main.xml
        ViewPager viewPager = (ViewPager) findViewById(R.id.pager);

        // Set the ViewPagerAdapter into ViewPager
        viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v4.view.PagerTabStrip
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:paddingBottom="10dp"
        android:paddingTop="10dp"
        android:textColor="#000000" />

</android.support.v4.view.ViewPager>

FragmentTab1.java

package com.adhish.pagertabstriptutorial;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

public class FragmentTab1 extends Fragment implements View.OnClickListener {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Get the view from fragmenttab1.xml

        getActivity().findViewById(R.id.button1).setOnClickListener(this);

        return inflater.inflate(R.layout.fragmenttab1, container, false);


    }

    public void onClick(View view)
    {
        if(view == getView().findViewById(R.id.button1))
        {
            Toast.makeText(getActivity(), "Hello !", Toast.LENGTH_SHORT);
        }
    }

}

fragmenttab1.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/Fragment1" />

        <Button
            android:id="@+id/button1"
            android:text="Click Me"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

</RelativeLayout>

The app crashes on load. As i am very new to Android Dev, PLEASE EXPLAIN IN DETAIL THE PROBLEM THAT IS CAUSING THIS AND THE RIGHT SOLUTION

Thanks.

The Logs are:

03-03 20:43:23.514  15010-15010/com.adhish.pagertabstriptutorial E/dalvikvm﹕ dvmPauseGc(AppLaunch) called - cookie=0xc069 (f=0x1)
03-03 20:43:23.519  15010-15010/com.adhish.pagertabstriptutorial I/PersonaManager﹕ getPersonaService() name persona_policy
03-03 20:43:23.529  15010-15028/com.adhish.pagertabstriptutorial D/skia﹕ GFXPNG PNG bitmap created width:18 height:18 bitmap id is 180
03-03 20:43:23.529  15010-15026/com.adhish.pagertabstriptutorial D/skia﹕ GFXPNG PNG bitmap created width:72 height:72 bitmap id is 181
03-03 20:43:23.529  15010-15029/com.adhish.pagertabstriptutorial D/skia﹕ GFXPNG PNG bitmap created width:18 height:18 bitmap id is 182
03-03 20:43:23.529  15010-15024/com.adhish.pagertabstriptutorial D/skia﹕ GFXPNG PNG bitmap created width:48 height:48 bitmap id is 183
03-03 20:43:23.529  15010-15027/com.adhish.pagertabstriptutorial D/skia﹕ GFXPNG PNG bitmap created width:18 height:18 bitmap id is 184
03-03 20:43:23.529  15010-15025/com.adhish.pagertabstriptutorial D/skia﹕ GFXPNG PNG bitmap created width:42 height:126 bitmap id is 185
03-03 20:43:23.529  15010-15030/com.adhish.pagertabstriptutorial D/skia﹕ GFXPNG PNG bitmap created width:144 height:144 bitmap id is 186
03-03 20:43:23.529  15010-15023/com.adhish.pagertabstriptutorial D/skia﹕ GFXPNG PNG bitmap created width:72 height:72 bitmap id is 187
03-03 20:43:23.564  15010-15010/com.adhish.pagertabstriptutorial I/dalvikvm﹕ Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
03-03 20:43:23.564  15010-15010/com.adhish.pagertabstriptutorial W/dalvikvm﹕ VFY: unable to resolve virtual method 11345: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
03-03 20:43:23.564  15010-15010/com.adhish.pagertabstriptutorial D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
03-03 20:43:23.564  15010-15010/com.adhish.pagertabstriptutorial I/dalvikvm﹕ Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
03-03 20:43:23.564  15010-15010/com.adhish.pagertabstriptutorial W/dalvikvm﹕ VFY: unable to resolve virtual method 11351: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
03-03 20:43:23.564  15010-15010/com.adhish.pagertabstriptutorial D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0000
03-03 20:43:23.564  15010-15010/com.adhish.pagertabstriptutorial I/dalvikvm﹕ Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
03-03 20:43:23.564  15010-15010/com.adhish.pagertabstriptutorial W/dalvikvm﹕ VFY: unable to resolve virtual method 9039: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
03-03 20:43:23.564  15010-15010/com.adhish.pagertabstriptutorial D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x000e
03-03 20:43:23.569  15010-15010/com.adhish.pagertabstriptutorial I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
03-03 20:43:23.569  15010-15010/com.adhish.pagertabstriptutorial W/dalvikvm﹕ VFY: unable to resolve virtual method 364: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
03-03 20:43:23.569  15010-15010/com.adhish.pagertabstriptutorial D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
03-03 20:43:23.569  15010-15010/com.adhish.pagertabstriptutorial I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
03-03 20:43:23.569  15010-15010/com.adhish.pagertabstriptutorial W/dalvikvm﹕ VFY: unable to resolve virtual method 386: Landroid/content/res/TypedArray;.getType (I)I
03-03 20:43:23.569  15010-15010/com.adhish.pagertabstriptutorial D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
03-03 20:43:23.579  15010-15010/com.adhish.pagertabstriptutorial D/skia﹕ GFXPNG PNG bitmap created width:72 height:72 bitmap id is 188
03-03 20:43:23.614  15010-15010/com.adhish.pagertabstriptutorial D/AndroidRuntime﹕ Shutting down VM
03-03 20:43:23.614  15010-15010/com.adhish.pagertabstriptutorial W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41acec08)
03-03 20:43:23.619  15010-15010/com.adhish.pagertabstriptutorial E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.adhish.pagertabstriptutorial, PID: 15010
    java.lang.NullPointerException
            at com.adhish.pagertabstriptutorial.FragmentTab1.onCreateView(FragmentTab1.java:17)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
            at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
            at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
            at android.view.View.measure(View.java:17479)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5364)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:17479)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5364)
            at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
            at android.view.View.measure(View.java:17479)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5364)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:17479)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5364)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
            at android.view.View.measure(View.java:17479)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5364)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2532)
            at android.view.View.measure(View.java:17479)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2262)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1391)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1590)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6585)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
            at android.view.Choreographer.doCallbacks(Choreographer.java:603)
            at android.view.Choreographer.doFrame(Choreographer.java:573)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5586)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
            at dalvik.system.NativeStart.main(Native Method)
03-03 20:43:25.609  15010-15010/com.adhish.pagertabstriptutorial I/Process﹕ Sending signal. PID: 15010 SIG: 9
6
  • could you post the stack trace? Commented Mar 3, 2015 at 15:11
  • button does not belong to activity layout Commented Mar 3, 2015 at 15:14
  • @Raghunandan So what is the solution? Commented Mar 3, 2015 at 15:15
  • @AdhishThite the below post should help Commented Mar 3, 2015 at 15:16
  • @TommyTopas Logs posted Commented Mar 3, 2015 at 15:16

1 Answer 1

3

You have issue in onCreateView method. You have to find button from view which you inflated, change it to :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Get the view from fragmenttab1.xml
    View v = inflater.inflate(R.layout.fragmenttab1, container, false);
    v.findViewById(R.id.button1).setOnClickListener(this);
    return v;
}
Sign up to request clarification or add additional context in comments.

6 Comments

Yes at least the app is not crashing now. But when i click on the button, the Toast is not displayed. :(
Toast.makeText(getActivity(), "Hello !", Toast.LENGTH_SHORT).show();
Yes. I will be able to mark it as an answer in 5 minutes. I will. Thanks for the quick help.
I'm glad I could help you So please accept my solution if it was help for you :)
The app is working fine. But i have a different question now. When i run the Code Analysis, it shows that there's a probable bug : 'Method invocation getView().findViewById(R.id.button1) at line 30 may produce java.lang.NullPointerException' . Is there a workaround for this?
|

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.