2014-06-13 72 views
0

我想了解何时应使用oncreate方法或oncreateview方法。什么样的代码我必须在OnCreate()和什么时候我必须把它放在OnCreateView()?

我有点困惑。首先,我有一些代码,包括OnCreate()方法中的findViewById()等语句。但它总是用一个空指针Exception来响应,然后有人告诉我,我应该把它放在OnCreateView()方法中。它的工作,但我不明白什么时候,什么代码我应该放在OnCreate()或什么时候,我应该放在OnCreateView()。有人可以向我解释这一点。

+0

这表明了'NullPointerException'可能是因为你试图调用方法'findViewById(..)'布局之前被带入视图使用'setContentView(...)'否则我只是通过调用'findViewById',我看不到'NullPointerException'显示在'onCreate()'中的任何原因。 – shyam

回答

0

在我的代码,方法如下:

活动代码:

@Override 
public void onCreate(Bundle saveInstanceState) 
{ 
    super.onCreate(saveInstanceState); 
    this.setContentView(R.layout.activity_container); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    if (saveInstanceState == null) 
    { 
     getSupportFragmentManager().beginTransaction() 
       .replace(R.id.activity_container_container, new MyFragment()).addToBackStack(null).commit(); 
    } 
    getSupportFragmentManager().addOnBackStackChangedListener(new OnBackStackChangedListener() 
    { 
     public void onBackStackChanged() 
     { 
      int backCount = getSupportFragmentManager().getBackStackEntryCount(); 
      if (backCount == 0) 
      { 
       finish(); 
      } 
     } 
    }); 
} 

@Override 
public void myInterface() 
{ 
    System.out.println("Do stuff;"); 
} 

片段代码:

private int titleId; 
private Button placeholderButton; 

private MyInterface activity_myInterface; 

public MyFragment() 
{ 
    super(); 
    this.titleId = R.string.my_actionbar_title; 
} 

@Override 
public void onAttach(Activity activity) 
{ 
    super.onAttach(activity); 
    try 
    { 
     activity_myInterface = (MyInterface)activity; 
    } 
    catch(ClassCastException e) 
    { 
     Log.e(this.getClass().getSimpleName(), "MyInterface interface needs to be implemented by Activity.", e); 
     throw e; 
    } 
} 

//this is an interface defined by me 
@Override 
public int getActionBarTitleId() 
{ 
    return titleId; 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 
    View view = inflater.inflate(R.layout.fragment_myfragment, container, false); 
    placeholderButton = (Button)view.findViewById(R.id.myfragment_placeholderbtn); 
    placeholderButton.setOnClickListener(this); 
    return view; 
} 

@Override 
public void onClick(View v) 
{ 
    if(v == placeholderButton) 
    { 
     System.out.println("Do more stuff"); 
    } 
} 

其中R.layout.activity_container是:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <FrameLayout 
     android:id="@+id/activity_container_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</RelativeLayout> 

这样Activity就负责包含Fragment(并且这个片段是在onCreate()中创建的)并且Fragment负责显示UI和事件处理。不过,您可以在活动中使用多个片段,这是他们主要为其设计的。

0

如果您使用的是活动,那么您可以远离onCreateView。

只要坚持到默认activity lifecycle

布局(内容)在onCreate方法被充气(创建)。

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

然后,一旦它已经膨胀您准备使用它,操纵属于它的观点。

findViewById()为您返回null,因为布局尚未准备好使用,这是因为它尚未充气。您可以在onCreatesetContentView()之后使用findViewById()

findViewById()是可以相当硬的性能的方法(如果多次调用),所以你应该得到你需要的所有意见的onCreate并将它们保存到像一个实例变量:

的TextView textView1, textView2;

覆盖 保护无效onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_book); textView1 = findViewById(R.id.textview1); textView2 = findViewById(R.id.textview2); ... }

而且从在onStart使用它们:

@Override 
protected void onStart() { 
    super.onStart(); 
    if (textView1 != null) textView1.setText("myText"); 
    if (textView2 != null) textView2.setText("some other text"); 
} 
相关问题