我有一个问题是让一个按钮在一个片段中工作,我已经尝试了许多东西并且在论坛上搜索但是无法让它工作任何帮助都会很棒!!!
片段类
package info.androidhive.tabsswipe;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
public class BreakfastFragment extends Fragment {
private Button button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater
.inflate(R.layout.fragment_breakfast, container, false);
String[] tables = { "Where are you?", "Take out", "Table 1", "Table 2",
"Table 3", "Table 4", "Table 5", "Table 6", };
Spinner spinner = (Spinner) v.findViewById(R.id.table_spinner);
ArrayAdapter<String> tableadapter = new ArrayAdapter<String>(
this.getActivity(), android.R.layout.simple_spinner_item,
tables);
String[] bagels = { "Pick your Bagel!", "Plain", "Wholemeal", "Seeded", };
Spinner spinner2 = (Spinner) v.findViewById(R.id.spinner_bageltype);
ArrayAdapter<String> Bageladapter = new ArrayAdapter<String>(
this.getActivity(), android.R.layout.simple_spinner_item,
bagels);
String[] fillings = { "A tasty filling?", "Bacon", "Sausage",
"Bacon & Sausage", "Soft Cheese", "Butter" };
Spinner spinner3 = (Spinner) v.findViewById(R.id.spinner_Filling);
ArrayAdapter<String> fillingadapter = new ArrayAdapter<String>(
this.getActivity(), android.R.layout.simple_spinner_item,
fillings);
String[] sauce = { "Any Sauce?", "Red", "Brown", "Mayo", "None", };
Spinner spinner4 = (Spinner) v.findViewById(R.id.spinner_sauce);
ArrayAdapter<String> sauceadapter = new ArrayAdapter<String>(
this.getActivity(), android.R.layout.simple_spinner_item, sauce);
tableadapter
.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
Bageladapter
.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
fillingadapter
.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
sauceadapter
.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner.setAdapter(tableadapter);
spinner2.setAdapter(Bageladapter);
spinner3.setAdapter(fillingadapter);
spinner4.setAdapter(sauceadapter);
addListenerOnButton();
return v;
}
public void addListenerOnButton() {
//Select a specific button to bundle it with the action you want
button = (Button) getView().findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff8400"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Breakfast Menu"
android:textSize="20dp" />
<Spinner
android:id="@+id/table_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="center"
android:hint="Enter your name" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Ready to order?"
android:textSize="20dp" />
<Spinner
android:id="@+id/spinner_bageltype"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" />
<Spinner
android:id="@+id/spinner_Filling"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" />
<Spinner
android:id="@+id/spinner_sauce"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Set your Order!" />
</LinearLayout>
logcat的
03-02 21:56:59.382: W/asset(32351): Copying FileAsset 0x69c2b938 (zip:/data/app/info.androidhive.tabsswipe-1.apk:/resources.arsc) to buffer size 4012 to make it aligned.
03-02 21:56:59.603: W/dalvikvm(32351): threadid=1: thread exiting with uncaught exception (group=0x41681970)
03-02 21:56:59.613: E/AndroidRuntime(32351): FATAL EXCEPTION: main
03-02 21:56:59.613: E/AndroidRuntime(32351): java.lang.NullPointerException
03-02 21:56:59.613: E/AndroidRuntime(32351): at info.androidhive.tabsswipe.BreakfastFragment.addListenerOnButton(BreakfastFragment.java:74)
03-02 21:56:59.613: E/AndroidRuntime(32351): at info.androidhive.tabsswipe.BreakfastFragment.onCreateView(BreakfastFragment.java:64)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.view.View.measure(View.java:16088)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5061)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.view.View.measure(View.java:16088)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5061)
03-02 21:56:59.613: E/AndroidRuntime(32351): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.view.View.measure(View.java:16088)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5061)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-02 21:56:59.613: E/AndroidRuntime(32351): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2277)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.view.View.measure(View.java:16088)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2109)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1219)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1403)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1119)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5849)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:786)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.view.Choreographer.doCallbacks(Choreographer.java:586)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.view.Choreographer.doFrame(Choreographer.java:546)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:771)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.os.Handler.handleCallback(Handler.java:730)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.os.Handler.dispatchMessage(Handler.java:92)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.os.Looper.loop(Looper.java:158)
03-02 21:56:59.613: E/AndroidRuntime(32351): at android.app.ActivityThread.main(ActivityThread.java:5789)
03-02 21:56:59.613: E/AndroidRuntime(32351): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 21:56:59.613: E/AndroidRuntime(32351): at java.lang.reflect.Method.invoke(Method.java:525)
03-02 21:56:59.613: E/AndroidRuntime(32351): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
03-02 21:56:59.613: E/AndroidRuntime(32351): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:843)
03-02 21:56:59.613: E/AndroidRuntime(32351): at dalvik.system.NativeStart.main(Native Method)
03-02 21:57:01.975: D/Process(32351): killProcess, pid=32351
03-02 21:57:01.975: D/Process(32351): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:123 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690
感谢大家让我疯狂:)
答案 0 :(得分:0)
您在BreakfastFragmentClass
的第64行遇到 NullPointerException 。
at info.androidhive.tabsswipe.BreakfastFragment.onCreateView(BreakfastFragment.java:64)
当您在对象上调用NULL 的方法时会发生此错误。
因此,原因可能是您引用了不在布局文件中的View ID,或者因为在调用Fragments getActivity()
方法之前调用了onAttach(Activity a)
。
在您的情况下,NullPointerException最有可能发生在这一行:
button = (Button) getView().findViewById(R.id.button1);
从View v;
)方法移交onCreaveView(...
,而不是调用getView()
。像这样:
public void addListenerOnButton(View v) {
//Select a specific button to bundle it with the action you want
button = (Button) v.findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
答案 1 :(得分:0)
默认情况下,Spinner将选择其列表中的第一个项目。 所以需要禁用微调器中的第一个项目。