我有多个按钮应该将不同的字符串值传递给单个活动。当我尝试在我的主要活动中包含按钮监听器时,我得到了NullPointer异常,当我尝试stackoverflow中的每个帖子时,这就是杀了我,它应该可以工作!我无法弄清楚我的问题是什么。这是一些代码:
fragment_layout中的按钮定义:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
style="@style/AppBaseTheme"
android:screenOrientation="portrait"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity=""
android:animateLayoutChanges="true"
android:background="#f000"
android:baselineAligned="false"
android:gravity="top"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/LinearLayout01"
style="@style/lineralLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="top"
android:orientation="vertical"
android:weightSum="3" >
<Button
android:id="@+id/button1s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/f0"
android:layout_weight="1"
/>
<Button
android:id="@+id/button2s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/f2"
android:layout_weight="1"
/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/f8"
android:layout_weight="1" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/f40"
android:layout_weight="1" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/dont_know"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
MainActivity的代码:
public class MainActivity extends Activity implements View.OnClickListener {
private Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1s);
button1.setOnClickListener(this);
button2 = (Button) findViewById(R.id.button2s);
button2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String toParse = new String();
switch (v.getId()) {
case R.id.button1s:
toParse = "0";
break;
case R.id.button2s:
toParse = "90";
break;
}
Intent intent = new Intent(MainActivity.this, DuzaKartaActivity.class);
intent.putExtra(TEXT_KEY, toParse);
startActivity(intent);
}
Fragment Class中的代码:
public class StandardFragment extends Fragment {
public StandardFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_layout, container, false);
return rootView;
}
}
logcat输出:
10-12 11:14:28.499: E/AndroidRuntime(3265): FATAL EXCEPTION: main
10-12 11:14:28.499: E/AndroidRuntime(3265): Process: com.ericpol.scrumpoker, PID: 3265
10-12 11:14:28.499: E/AndroidRuntime(3265): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ericpol.scrumpoker/com.ericpol.scrumpoker.MainActivity}: java.lang.NullPointerException
10-12 11:14:28.499: E/AndroidRuntime(3265): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
10-12 11:14:28.499: E/AndroidRuntime(3265): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
10-12 11:14:28.499: E/AndroidRuntime(3265): at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-12 11:14:28.499: E/AndroidRuntime(3265): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-12 11:14:28.499: E/AndroidRuntime(3265): at android.os.Handler.dispatchMessage(Handler.java:102)
10-12 11:14:28.499: E/AndroidRuntime(3265): at android.os.Looper.loop(Looper.java:136)
10-12 11:14:28.499: E/AndroidRuntime(3265): at android.app.ActivityThread.main(ActivityThread.java:5017)
10-12 11:14:28.499: E/AndroidRuntime(3265): at java.lang.reflect.Method.invokeNative(Native Method)
10-12 11:14:28.499: E/AndroidRuntime(3265): at java.lang.reflect.Method.invoke(Method.java:515)
10-12 11:14:28.499: E/AndroidRuntime(3265): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-12 11:14:28.499: E/AndroidRuntime(3265): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-12 11:14:28.499: E/AndroidRuntime(3265): at dalvik.system.NativeStart.main(Native Method)
10-12 11:14:28.499: E/AndroidRuntime(3265): Caused by: java.lang.NullPointerException
10-12 11:14:28.499: E/AndroidRuntime(3265): at com.ericpol.scrumpoker.MainActivity.onCreate(MainActivity.java:80)
10-12 11:14:28.499: E/AndroidRuntime(3265): at android.app.Activity.performCreate(Activity.java:5231)
10-12 11:14:28.499: E/AndroidRuntime(3265): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-12 11:14:28.499: E/AndroidRuntime(3265): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
答案 0 :(得分:2)
你应该改变这个
setContentView(R.layout.activity_main);
到
setContentView(R.layout.fragment_layout);
答案 1 :(得分:1)
您的Button
是在fragment_layout
内定义的,您正试图让活动膨胀,然后find the view
。这不行。您必须inflate
fragment_layout
和致电findViewById
或强>
创建一个扩展Fragment
的类并覆盖onCreateView
方法并使用FragmentManager
将片段添加到活动中。
在覆盖onCreateView
方法中,您可以设置侦听器
答案 2 :(得分:0)
我找不到问题所在,所以我在fragemt_layout xml中设置了onClick方法,并从MainActivity中删除了onClick监听器。