按钮无法使用。保持强制关闭应用程序
问题描述:
每当我将我的代码(按钮)插入片段时,应用程序强制关闭。我制作了一个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)
答
在片段你应该使用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;
}
'显示java.lang.NullPointerException在com.example.ttg.fragment.LinuxCommands.onCreate(LinuxCommands.java:56)' – csmckelvey 2014-12-06 03:57:27
你应该膨胀和'onCreateView()'方法初始化布局,不'onCreate()'。并且您想在充值的View上调用'findViewById()'。 – 2014-12-06 03:57:33
所以这个按钮应该在'onCreateView()'里面? – 2014-12-06 04:05:21