按钮不起作用。继续强制关闭应用程序[关闭]
Posted
技术标签:
【中文标题】按钮不起作用。继续强制关闭应用程序[关闭]【英文标题】:Button not working. Keeps on forcing app close [closed] 【发布时间】:2015-02-04 08:17:30 【问题描述】:每当我将代码(按钮)插入片段时,应用程序都会强制关闭。我制作了一个 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_
android:layout_>
<LinearLayout
android:layout_
android:layout_
android:layout_gravity="center"
android:orientation="horizontal">
<ListView android:id="@id/android:list"
android:layout_
android:layout_
android:layout_marginTop="125dp"/>
</LinearLayout>
<LinearLayout
android:layout_
android:id="@+id/linearLayout"
android:layout_
android:gravity="center"
android:layout_gravity="bottom">
<Button
android:id="@+id/buttonTest"
android:layout_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)
【问题讨论】:
java.lang.NullPointerException at com.example.ttg.fragment.LinuxCommands.onCreate(LinuxCommands.java:56)
您应该在onCreateView()
方法中膨胀和初始化您的布局,而不是onCreate()
。你想在膨胀的视图上调用findViewById()
。
所以按钮应该在onCreateView()
里面?
【参考方案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;
【讨论】:
@NikkodelValle 你应该接受给定的答案。 @Ankit Sharma 请在发布答案之前做好格式化。下次注意了 我正在投票,但我没有足够的 repu以上是关于按钮不起作用。继续强制关闭应用程序[关闭]的主要内容,如果未能解决你的问题,请参考以下文章
Bootstrap popover 关闭功能在 iphone 中不起作用