2014-12-06 119 views
0

每當我將我的代碼(按鈕)插入片段時,應用程序強制關閉。我製作了一個xml文件,我把它稱爲片段java,但它有一個適配器,在我的xml中,我使用framelayout將按鈕放在頁面的最底部。我也嘗試使用頁腳,但同樣的錯誤,我得到了。任何人都知道如何解決這個問題?謝謝:)按鈕無法使用。保持強制關閉應用程序

XML文件

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/takeQuiz" 
android:orientation="horizontal" 
android:background="@drawable/bg_linux_commands" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

<LinearLayout 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="center" 
android:orientation="horizontal"> 

<ListView android:id="@id/android:list" 
      android:layout_width="300dp" 
      android:layout_height="300dp" 
      android:layout_marginTop="125dp"/> 

</LinearLayout> 


<LinearLayout 
android:layout_width="match_parent" 
android:id="@+id/linearLayout" 
android:layout_height="50dp" 
android:gravity="center" 
android:layout_gravity="bottom"> 

    <Button 
     android:id="@+id/buttonTest" 
     android:layout_width="fill_parent 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="@string/test" > 
    </Button> 

</LinearLayout> 
</FrameLayout> 

片段JAVA

public class LinuxCommands extends ListFragment{ 
String classes[] = { "File Managing System", "Scripting and Text Processing","Process and System Utility", 
     "System Information Commands","Archival and Comparison Commands","Command Shells"}; 

@SuppressLint("InflateParams") 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // ActionBar 
    ActionBar mActionBar = getActivity().getActionBar(); 
    mActionBar.setDisplayShowHomeEnabled(true); 
    mActionBar.setDisplayShowTitleEnabled(false); 
    LayoutInflater mInflater = LayoutInflater.from(getActivity()); 

    View mCustomView = mInflater.inflate(R.layout.actionbar_ttg, null); 
    TextView mTextView = (TextView) mCustomView.findViewById(R.id.actionbar_ttg); 
    mTextView.setText("LINUX COMMANDS"); 
    Typeface custom_font = Typeface.createFromAsset(getActivity().getAssets(), 
      "fonts/BRLNSR.TTF"); 
      mTextView.setTypeface(custom_font); 

    mTextView.setTextSize(18); 
    mTextView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL); 

    mActionBar.setCustomView(mCustomView); 
    mActionBar.setDisplayShowCustomEnabled(true); 

    //Button 
    final Button button = (Button) getView().findViewById(R.id.buttonTest); 
    button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      // Quiz 

      Intent intent = new Intent (getActivity(), Terminal.class); 
      startActivity(intent); 
     } 
    }); 


    ListAdapter adapter = new ArrayAdapter<String>(getActivity(), 
      android.R.layout.simple_list_item_1, 
      classes); 

    // Setting the list adapter for the ListFragment 
    setListAdapter(adapter); 
} 

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

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

} 

@Override 
    public void onListItemClick(ListView l, View v, int position, long id) { 


    if(position == 0) { 
     Intent intent = new Intent (getActivity(), FileManagingSystemList.class); 
     startActivity(intent); 
    } 
    else if (position == 1){ 
     Intent intent = new Intent (getActivity(),ScriptingAndTextProcessingList.class); 
     startActivity(intent); 
    } 
    else if (position == 2){ 
     Intent intent = new Intent (getActivity(),ProcessAndSystemUtilityList.class); 
     startActivity(intent); 
    } 
    else if (position == 3){ 
     Intent intent = new Intent (getActivity(),SystemInformationCmdList.class); 
     startActivity(intent); 
    } 
    else if (position == 4){ 
     Intent intent = new Intent (getActivity(),ArchivalAndCompressionCmdList.class); 
     startActivity(intent); 
    } 
    else{ 
     Intent intent = new Intent (getActivity(),CommandShellsList.class); 
     startActivity(intent); 
    } 


} 

} 

logcat的

12-06 11:50:54.069: E/AndroidRuntime(26672): FATAL EXCEPTION: main 
12-06 11:50:54.069: E/AndroidRuntime(26672): java.lang.NullPointerException 
12-06 11:50:54.069: E/AndroidRuntime(26672): at com.example.ttg.fragment.LinuxCommands.onCreate(LinuxCommands.java:56) 
12-06 11:50:54.069: E/AndroidRuntime(26672): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java) 
12-06 11:50:54.069: E/AndroidRuntime(26672): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java) 
12-06 11:50:54.069: E/AndroidRuntime(26672): at android.app.BackStackRecord.run(BackStackRecord.java) 
12-06 11:50:54.069: E/AndroidRuntime(26672): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java) 
12-06 11:50:54.069: E/AndroidRuntime(26672): at android.app.FragmentManagerImpl$1.run(FragmentManager.java) 
12-06 11:50:54.069: E/AndroidRuntime(26672): at android.os.Handler.handleCallback(Handler.java) 
12-06 11:50:54.069: E/AndroidRuntime(26672): at android.os.Handler.dispatchMessage(Handler.java) 
12-06 11:50:54.069: E/AndroidRuntime(26672): at android.os.Looper.loop(Looper.java) 
12-06 11:50:54.069: E/AndroidRuntime(26672): at android.app.ActivityThread.main(ActivityThread.java) 
12-06 11:50:54.069: E/AndroidRuntime(26672): at java.lang.reflect.Method.invokeNative(Native Method) 
12-06 11:50:54.069: E/AndroidRuntime(26672): at java.lang.reflect.Method.invoke(Method.java:511) 
12-06 11:50:54.069: E/AndroidRuntime(26672): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java) 
12-06 11:50:54.069: E/AndroidRuntime(26672): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java) 
12-06 11:50:54.069: E/AndroidRuntime(26672): at dalvik.system.NativeStart.main(Native Method) 
+0

'顯示java.lang.NullPointerException在com.example.ttg.fragment.LinuxCommands.onCreate(LinuxCommands.java:56)' – csmckelvey 2014-12-06 03:57:27

+0

你應該膨脹和'onCreateView()'方法初始化佈局,不'onCreate()'。並且您想在充值的View上調用'findViewById()'。 – 2014-12-06 03:57:33

+0

所以這個按鈕應該在'onCreateView()'裏面? – 2014-12-06 04:05:21

回答

1

在片段你應該使用onCreateView()方法。

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



root = inflater.inflate(R.layout.schedule_fragment, container, false); 

startButton = (Button)root.findViewById(R.id.button); 


     startButton.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       // your code. 
      } 
     }); 
    return root; 
    } 
+0

@NikkodelValle你應該接受給定的答案。 – Amy 2014-12-06 04:34:20

+0

@Ankit Sharma請在發佈答案之前格式化好。下次照顧 – Amy 2014-12-06 04:35:45

+0

我正在投票,但我沒有足夠的repu – 2014-12-06 08:18:26