Autore Topic: Chiamare una activity da un fragment  (Letto 696 volte)

Offline aledpa

  • Nuovo arrivato
  • *
  • Post: 32
  • Respect: 0
    • Mostra profilo
Chiamare una activity da un fragment
« il: 02 Dicembre 2013, 13:49:01 CET »
0
Salve a tutti, spero di scrivere nella sezione corretta;
nella mia applicazione uso le swipe views, con una activity con dentro 3 tab come fragment, vorrei chiedere se è possibile fare e se si in modo che premendo su una textview (un bottone, ecc..) dentro la tab si avvii una nuova activity;
io ho questo codice:

Codice (Java): [Seleziona]
package it.provaswipe.provaswipe;

import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends FragmentActivity {
   
    CollectionPagerAdapter mCollectionPagerAdapter;
    ViewPager mViewPager;
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mCollectionPagerAdapter = new CollectionPagerAdapter(getSupportFragmentManager());
    final ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mCollectionPagerAdapter);
    }

    public class CollectionPagerAdapter extends FragmentStatePagerAdapter {
   
        final int NUM_ITEMS = 3; // number of tabs
 
            public CollectionPagerAdapter(FragmentManager fm) {
                super(fm);
            }
         
            @Override
            public Fragment getItem(int i) {
                Fragment fragment = new TabFragment();
                Bundle args = new Bundle();
                args.putInt(TabFragment.ARG_OBJECT, i);
                fragment.setArguments(args);
                return fragment;
            }
         
            @Override
            public int getCount() {
                return NUM_ITEMS;
            }
       
            @Override
            public CharSequence getPageTitle(int position) {
         
                String tabLabel = null;
                switch (position) {
                case 0:
                tabLabel = getString(R.string.label1);
                break;
                case 1:
                tabLabel = getString(R.string.label2);
                break;
                case 2:
                tabLabel = getString(R.string.label3);
                break;
                }
                return tabLabel;
            }
    }
 
    public static class TabFragment extends Fragment {
 
        public static final String ARG_OBJECT = "object";
       
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

                Bundle args = getArguments();
                int position = args.getInt(ARG_OBJECT);
         
                int tabLayout = 0;
                switch (position) {
                case 0:
                tabLayout = R.layout.tab1;
                TextView tw = (TextView) getActivity().findViewById(R.id.textView1);
                tw.setOnClickListener(
                                new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v){
                                                startSubActivity();
                                        }
                                }
                );
                break;
                case 1:
                tabLayout = R.layout.tab2;
                break;
                case 2:
                tabLayout = R.layout.tab3;
                break;
        }
        View rootView = inflater.inflate(tabLayout, container, false);
 
        return rootView;
    }
       
    public void startSubActivity() {
        Intent intent;
        intent = new Intent(getActivity().getBaseContext(), Prova.class);
        startActivity(intent);
    }
  }
}

e il logcat che mi da è:
Codice (Java): [Seleziona]
12-02 06:27:05.660: E/AndroidRuntime(2197): FATAL EXCEPTION: main
12-02 06:27:05.660: E/AndroidRuntime(2197): Process: it.provaswipe.provaswipe, PID: 2197
12-02 06:27:05.660: E/AndroidRuntime(2197): java.lang.NullPointerException
12-02 06:27:05.660: E/AndroidRuntime(2197):     at it.provaswipe.provaswipe.MainActivity$TabFragment.onCreateView(MainActivity.java:120)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.view.View.measure(View.java:16458)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.view.View.measure(View.java:16458)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.view.View.measure(View.java:16458)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2289)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.view.View.measure(View.java:16458)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1914)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1111)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1293)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5582)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.view.Choreographer.doFrame(Choreographer.java:532)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.os.Handler.handleCallback(Handler.java:733)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.os.Handler.dispatchMessage(Handler.java:95)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.os.Looper.loop(Looper.java:137)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at android.app.ActivityThread.main(ActivityThread.java:4998)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at java.lang.reflect.Method.invokeNative(Native Method)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at java.lang.reflect.Method.invoke(Method.java:515)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-02 06:27:05.660: E/AndroidRuntime(2197):     at dalvik.system.NativeStart.main(Native Method)
12-02 06:27:09.340: I/Process(2197): Sending signal. PID: 2197 SIG: 9
12-02 13:43:33.384: D/dalvikvm(2361): GC_FOR_ALLOC freed 74K, 5% free 3201K/3352K, paused 48ms, total 50ms
12-02 13:43:33.404: I/dalvikvm-heap(2361): Grow heap (frag case) to 4.271MB for 1127536-byte allocation
12-02 13:43:33.554: D/dalvikvm(2361): GC_FOR_ALLOC freed 2K, 4% free 4299K/4456K, paused 144ms, total 145ms
12-02 13:43:33.794: D/AndroidRuntime(2361): Shutting down VM
12-02 13:43:33.794: W/dalvikvm(2361): threadid=1: thread exiting with uncaught exception (group=0xb1a93b90)
12-02 13:43:33.814: E/AndroidRuntime(2361): FATAL EXCEPTION: main
12-02 13:43:33.814: E/AndroidRuntime(2361): Process: it.provaswipe.provaswipe, PID: 2361
12-02 13:43:33.814: E/AndroidRuntime(2361): java.lang.NullPointerException
12-02 13:43:33.814: E/AndroidRuntime(2361):     at it.provaswipe.provaswipe.MainActivity$TabFragment.onCreateView(MainActivity.java:120)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.view.View.measure(View.java:16458)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.view.View.measure(View.java:16458)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.view.View.measure(View.java:16458)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2289)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.view.View.measure(View.java:16458)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1914)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1111)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1293)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5582)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.view.Choreographer.doFrame(Choreographer.java:532)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.os.Handler.handleCallback(Handler.java:733)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.os.Handler.dispatchMessage(Handler.java:95)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.os.Looper.loop(Looper.java:137)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at android.app.ActivityThread.main(ActivityThread.java:4998)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at java.lang.reflect.Method.invokeNative(Native Method)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at java.lang.reflect.Method.invoke(Method.java:515)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-02 13:43:33.814: E/AndroidRuntime(2361):     at dalvik.system.NativeStart.main(Native Method)
12-02 13:44:24.974: I/Process(2361): Sending signal. PID: 2361 SIG: 9

inoltre prima qui
Codice (Java): [Seleziona]
intent = new Intent(getActivity().getBaseContext(), Prova.class);            // errore su getActivity()
startActivity(intent);                                                                                  // errore

prima mi dava l'errore "Cannot make a static reference to the non-static method startActivity(Intent) from the type Fragment"
poi ho levato "static" dal primo rigo della classe e poi rimesso e non me lo ha più dato.

come posso risolverlo?

Post unito: 03 Dicembre 2013, 13:25:25 CET
ho modificato leggermente il codice in questo modo:
Codice (Java): [Seleziona]
import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {

    CollectionPagerAdapter mCollectionPagerAdapter;
   
    ViewPager mViewPager;
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   
    mCollectionPagerAdapter = new CollectionPagerAdapter(getSupportFragmentManager());
   
    final ActionBar actionBar = getActionBar();
   
    actionBar.setDisplayHomeAsUpEnabled(true);
   
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mCollectionPagerAdapter);
    }
   
    public class CollectionPagerAdapter extends FragmentStatePagerAdapter {
   
        final int NUM_ITEMS = 3; // number of tabs
 
            public CollectionPagerAdapter(FragmentManager fm) {
                super(fm);
            }
         
            @Override
            public Fragment getItem(int i) {
                Fragment fragment = new TabFragment();
                Bundle args = new Bundle();
                args.putInt(TabFragment.ARG_OBJECT, i);
                fragment.setArguments(args);
                return fragment;
            }
         
            @Override
            public int getCount() {
                return NUM_ITEMS;
            }
       
            @Override
            public CharSequence getPageTitle(int position) {
         
                String tabLabel = null;
                switch (position) {
                case 0:
                tabLabel = getString(R.string.label1);
                TextView tw = (TextView) findViewById(R.id.textView1);
               
                if(tw != null){    
                        tw.setOnClickListener(
                                new View.OnClickListener() {
                                        @Override
                                        public void onClick(View v){
                                                startSubActivity();
                                        }
                                }
                        );     
                }      
                else
                        System.out.println("ERRORE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                break;
                case 1:
                tabLabel = getString(R.string.label2);
                break;
                case 2:
                tabLabel = getString(R.string.label3);
                break;
                }
                return tabLabel;
            }
           
            public void startSubActivity() {
                Intent intent;
                intent = new Intent(getBaseContext(), Prova.class);
                startActivity(intent);
            }
    }
 
    public static class TabFragment extends Fragment {
 
        public static final String ARG_OBJECT = "object";
       
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

                Bundle args = getArguments();
                int position = args.getInt(ARG_OBJECT);
         
                int tabLayout = 0;
                switch (position) {
                case 0:
                tabLayout = R.layout.tab1;
                break;
                case 1:
                tabLayout = R.layout.tab2;
                break;
                case 2:
                tabLayout = R.layout.tab3;
                break;
        }
        View rootView = inflater.inflate(tabLayout, container, false);
 
        return rootView;
    }
  }
}

ora succede una cosa molto strana; all'avvio sulla console mi stampa il messaggio ERRORE, se premo sulla textview non succede niente, mentre se cambio tab, ritorno indietro e ripremo la tab per magia funziona!
che succede?
« Ultima modifica: 03 Dicembre 2013, 13:25:26 CET da aledpa, Reason: Merged DoublePost »