2013-10-16 123 views
-1

爲什麼不能正常工作?我在setContentView和findViewById上發生錯誤。ViewPager和片段錯誤

方法對於Utesteder類型是未定義的。

如果您需要更多信息,請詢問!

我在應用程序開發方面很新穎。

package hib.bergen2; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

public class Utesteder extends Fragment { 


    TextView exodusTekst; 
    Button exodusKnapp; 

    TextView fotballpubenTekst; 
    Button fotballpubenKnapp; 

    TextView ovenpoTekst; 
    Button ovenpoKnapp; 

    TextView kvarteretTekst; 
    Button kvarteretKnapp; 

    TextView hulenTekst; 
    Button hulenKnapp; 

    TextView sjobodenTekst; 
    Button sjobodenKnapp; 

    TextView downstairsTekst; 
    Button downstairsKnapp; 


    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_utesteder); 



     exodusTekst = (TextView) findViewById(R.id.exodusTekst); 

     exodusKnapp= (Button) findViewById(R.id.exodusKnapp); 
     exodusKnapp.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       exodusTekst.setVisibility((exodusTekst.getVisibility() == View.VISIBLE)? View.GONE : View.VISIBLE); 
       fotballpubenTekst.setVisibility(View.GONE); 
       ovenpoTekst.setVisibility(View.GONE); 
       kvarteretTekst.setVisibility(View.GONE); 
       hulenTekst.setVisibility(View.GONE); 
       sjobodenTekst.setVisibility(View.GONE); 
       downstairsTekst.setVisibility(View.GONE); 
      } 

     }); 

     fotballpubenTekst = (TextView) findViewById(R.id.fotballpubenTekst); 

     fotballpubenKnapp= (Button) findViewById(R.id.fotballpubenKnapp); 
     fotballpubenKnapp.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       fotballpubenTekst.setVisibility((fotballpubenTekst.getVisibility() == View.VISIBLE)? View.GONE : View.VISIBLE); 
       exodusTekst.setVisibility(View.GONE); 
       ovenpoTekst.setVisibility(View.GONE); 
       kvarteretTekst.setVisibility(View.GONE); 
       hulenTekst.setVisibility(View.GONE); 
       sjobodenTekst.setVisibility(View.GONE); 
       downstairsTekst.setVisibility(View.GONE); 
      } 

     }); 

     ovenpoTekst = (TextView) findViewById(R.id.ovenpoTekst); 

     ovenpoKnapp= (Button) findViewById(R.id.ovenpoKnapp); 
     ovenpoKnapp.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       ovenpoTekst.setVisibility((ovenpoTekst.getVisibility() == View.VISIBLE)? View.GONE : View.VISIBLE); 
       exodusTekst.setVisibility(View.GONE); 
       fotballpubenTekst.setVisibility(View.GONE); 
       kvarteretTekst.setVisibility(View.GONE); 
       hulenTekst.setVisibility(View.GONE); 
       sjobodenTekst.setVisibility(View.GONE); 
       downstairsTekst.setVisibility(View.GONE); 
      } 

     }); 

     kvarteretTekst = (TextView) findViewById(R.id.kvarteretTekst); 

     kvarteretKnapp= (Button) findViewById(R.id.kvarteretKnapp); 
     kvarteretKnapp.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       kvarteretTekst.setVisibility((kvarteretTekst.getVisibility() == View.VISIBLE)? View.GONE : View.VISIBLE); 
       exodusTekst.setVisibility(View.GONE); 
       fotballpubenTekst.setVisibility(View.GONE); 
       ovenpoTekst.setVisibility(View.GONE); 
       hulenTekst.setVisibility(View.GONE); 
       sjobodenTekst.setVisibility(View.GONE); 
       downstairsTekst.setVisibility(View.GONE); 
      } 

     }); 

     hulenTekst = (TextView) findViewById(R.id.hulenTekst); 

     hulenKnapp= (Button) findViewById(R.id.hulenKnapp); 
     hulenKnapp.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       hulenTekst.setVisibility((hulenTekst.getVisibility() == View.VISIBLE)? View.GONE : View.VISIBLE); 
       exodusTekst.setVisibility(View.GONE); 
       fotballpubenTekst.setVisibility(View.GONE); 
       ovenpoTekst.setVisibility(View.GONE); 
       kvarteretTekst.setVisibility(View.GONE); 
       sjobodenTekst.setVisibility(View.GONE); 
       downstairsTekst.setVisibility(View.GONE); 
      } 

     }); 

     sjobodenTekst = (TextView) findViewById(R.id.sjobodenTekst); 

     sjobodenKnapp= (Button) findViewById(R.id.sjobodenKnapp); 
     sjobodenKnapp.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       sjobodenTekst.setVisibility((sjobodenTekst.getVisibility() == View.VISIBLE)? View.GONE : View.VISIBLE); 
       exodusTekst.setVisibility(View.GONE); 
       fotballpubenTekst.setVisibility(View.GONE); 
       ovenpoTekst.setVisibility(View.GONE); 
       kvarteretTekst.setVisibility(View.GONE); 
       hulenTekst.setVisibility(View.GONE); 
       downstairsTekst.setVisibility(View.GONE); 
      } 

     }); 

     downstairsTekst = (TextView) findViewById(R.id.downstairsTekst); 

     downstairsKnapp= (Button) findViewById(R.id.downstairsKnapp); 
     downstairsKnapp.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       downstairsTekst.setVisibility((downstairsTekst.getVisibility() == View.VISIBLE)? View.GONE : View.VISIBLE); 
       exodusTekst.setVisibility(View.GONE); 
       fotballpubenTekst.setVisibility(View.GONE); 
       ovenpoTekst.setVisibility(View.GONE); 
       kvarteretTekst.setVisibility(View.GONE); 
       hulenTekst.setVisibility(View.GONE); 
       sjobodenTekst.setVisibility(View.GONE); 
      } 

     }); 
    } 

} 




    package hib.bergen2; 

import java.util.Locale; 

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.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

public class Uteliv extends FragmentActivity { 

    /** 
    * The {@link android.support.v4.view.PagerAdapter} that will provide 
    * fragments for each of the sections. We use a 
    * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which 
    * will keep every loaded fragment in memory. If this becomes too memory 
    * intensive, it may be best to switch to a 
    * {@link android.support.v4.app.FragmentStatePagerAdapter}. 
    */ 
    SectionsPagerAdapter mSectionsPagerAdapter; 

    /** 
    * The {@link ViewPager} that will host the section contents. 
    */ 
    ViewPager mViewPager; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_uteliv); 

     // Create the adapter that will return a fragment for each of the three 
     // primary sections of the app. 
     mSectionsPagerAdapter = new SectionsPagerAdapter(
       getSupportFragmentManager()); 

     // Set up the ViewPager with the sections adapter. 
     mViewPager = (ViewPager) findViewById(R.id.pager); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 

    } 

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

    /** 
    * A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
    * one of the sections/tabs/pages. 
    */ 
    public class SectionsPagerAdapter extends FragmentPagerAdapter { 

     public SectionsPagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 



     @Override 
     public Fragment getItem(int page) { 
      switch (page) { 
       case 0: return new Utesteder(); 
       /*case 1: return new MySecondFragment(); 
       case 2: return new MyThirdFragment(); 
       //and so on....*/ 
      } 
      return null; 
     } 


     @Override 
     public int getCount() { 
      // Show 3 total pages. 
      return 3; 
     } 

     @Override 
     public CharSequence getPageTitle(int position) { 
      Locale l = Locale.getDefault(); 
      switch (position) { 
      case 0: 
       return getString(R.string.title_section1).toUpperCase(l); 
      case 1: 
       return getString(R.string.title_section2).toUpperCase(l); 
      case 2: 
       return getString(R.string.title_section3).toUpperCase(l); 
      } 
      return null; 
     } 
    } 

    /** 
    * A dummy fragment representing a section of the app, but that simply 
    * displays dummy text. 
    */ 
    public static class DummySectionFragment extends Fragment { 
     /** 
     * The fragment argument representing the section number for this 
     * fragment. 
     */ 
     public static final String ARG_SECTION_NUMBER = "section_number"; 

     public DummySectionFragment() { 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_uteliv_dummy, 
        container, false); 
      TextView dummyTextView = (TextView) rootView 
        .findViewById(R.id.section_label); 
      dummyTextView.setText(Integer.toString(getArguments().getInt(
        ARG_SECTION_NUMBER))); 
      return rootView; 
     } 
    } 

} 

回答

0
public class Utesteder extends Fragment { 

//... 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    final View v = inflater.inflate(R.layout.activity_utesteder, 
      container, false); 
      exodusTekst = (TextView) v.findViewById(R.id.exodusTekst); 
      //and so on.. 

     } 
} 

注意的觀點:它不只是findViewById,其v.findViewById();

1

setContentViewActivity的不Fragment的方法。 onCreateView必須返回要顯示