按钮不起作用。继续强迫app关闭

时间:2014-12-06 03:54:26

标签: java android eclipse button

每当我将代码(按钮)插入片段时,app强制关闭。我创建了一个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>

FRAGMENT 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)

1 个答案:

答案 0 :(得分:1)

在Fragment中,您应该使用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;
    }