2014-01-08 123 views
0

我想添加一個按鈕到一個片段視圖,並通過單擊該按鈕它應該把我帶到另一個視圖,但現在我的應用程序只是崩潰,因爲我創建的變量,以獲得viewId的按鈕不斷返回null ...我不知道爲什麼!請幫忙!Android按鈕setonclick返回null

package com.example.app; 

import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 

public class MainActivity extends FragmentActivity { 

    Button nextPage; 

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



     if (savedInstanceState == null) { 
      getSupportFragmentManager().beginTransaction() 
        .add(R.id.container, new PlaceholderFragment()) 
        .commit(); 
     } 

     nextPage = (Button) findViewById(R.id.nextpage); 
    // nextPage.setOnClickListener(buttonNextPage); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.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(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    /** 
    * A placeholder fragment containing a simple view. 
    */ 
    public static class PlaceholderFragment extends Fragment { 

     public PlaceholderFragment() { 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
      return rootView; 
     } 
    } 


    /* private View.OnClickListener buttonNextPage = new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      getFragmentManager().beginTransaction() 
        .replace(R.id.container, new FragmentViewOne()) 
        .commit(); 
     } 
    };*/ 

} 

這就是我MainActivity以上:

package com.example.app; 

import android.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

/** 
* Created by Jerry on 2014/1/4. 
*/ 
public class FragmentViewOne extends Fragment { 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
    } 

     @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     View myFragmentView = inflater.inflate(R.layout.fragmentview_one, container, false); 

     return myFragmentView; 
    } 

} 

我的第二片段:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.app.MainActivity" 
    tools:ignore="MergeRootFrame" /> 

容器XML。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context="com.example.app.MainActivity$PlaceholderFragment"> 

    <TextView 
     android:text="@string/hello_world" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Next Page" 
     android:id="@+id/nextpage" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

</RelativeLayout> 

first fragment xml。

<?xml version="1.0" encoding="utf-8"?> 

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

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Fragment View" 
     android:id="@+id/textView" /> 
</LinearLayout> 

第二個片段。

01-07 21:57:20.470 25549-25549/? E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.example.app, PID: 25549 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.MainActivity}: java.lang.NullPointerException 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
      at android.app.ActivityThread.access$800(ActivityThread.java:135) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:136) 
      at android.app.ActivityThread.main(ActivityThread.java:5017) 
      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:779) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.NullPointerException 
      at com.example.app.MainActivity.setupButton(MainActivity.java:37) 
      at com.example.app.MainActivity.onCreate(MainActivity.java:21) 
      at android.app.Activity.performCreate(Activity.java:5231) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
            at android.app.ActivityThread.access$800(ActivityThread.java:135) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:136) 
            at android.app.ActivityThread.main(ActivityThread.java:5017) 
            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:779) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
            at dalvik.system.NativeStart.main(Native Method) 

logcat的

+2

1. setonclick不會退出。 2. setOnClickListener返回void – Blackbelt

+0

,你在哪裏初始化'buttonNextPage'? –

+0

buttonNextPage < - 我已經評論這些了 –

回答

0

R.id.nextpage是在該片段中沒有活動,您不能在活動直接找到它的XML佈局。

最好的方法是找到這個按鈕裏面自我片段。並且僅在片段內附加onClickListener

現在創建的接口是這樣的:

interface FragmentCallback{ 
    void nextButtonClicked(); 
} 

讓您的活動實現,而且編寫代碼來改變內部活動中nextButtonClicked功能視圖。

內部片段和 FragmentCallback類型的實例變量

現在:

FragmentCallback callback; 

,併爲其編寫一個setter:

public void setFragmentCallback(FragmentCallback callback){ 
    this.callback=callback; 
} 
現在

在當你創建片段通話setFragmentCallback您的活動。

<FragmentObject>.setFragmentCallback(this); 

內片段Next按鈕的的onClick只要致電:

callback.nextButtonClicked(); 

片段:

public class FragmentViewOne extends Fragment implements OnClickListener{ 
    FragmentCallback callback; 
    public void setFragmentCallback(FragmentCallback callback){ 
     this.callback=callback; 
    } 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
    } 

     @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     View myFragmentView = inflater.inflate(R.layout.fragmentview_one, container, false); 
     myFragmentView.findViewById(R.id.nextpage).setOnClickListener(this); 
     return myFragmentView; 
    } 
    @Override 
public void onClick(View v) { 
    callback.nextButtonClicked(); 
    } 
} 

活動:

public class MainActivity extends FragmentActivity implements FragmentCallback{ 

Button nextPage; 

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



    if (savedInstanceState == null) { 
     PlaceholderFragment fragment= new PlaceholderFragment(); 
     getSupportFragmentManager().beginTransaction() 
       .add(R.id.container, fragment) 
       .commit(); 
     fragment.setFragmentCallback(this); 
    } 

    void nextButtonClicked(){ 
     //code to change view 
    } 
//rest of the code