我知道这个问题已被多次询问,但其他答案都没有帮助我。当我注释掉setOnClickListener时,我的代码工作正常。
所以,这是我的主要活动......
public class StartingPoint extends ActionBarActivity {
int ctr = 0;
Button add, sub;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
//find views
display = (TextView) findViewById(R.id.tvTotal);
add = (Button)this. findViewById(R.id.bAdd);
sub = (Button)this. findViewById(R.id.bSub);
//click listeners
OnClickListener lis = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ctr++;
display.setText("The total is " + ctr);
}
};
add.setOnClickListener(lis);
/**sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ctr--;
display.setText("The total is " + ctr);
}
});*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {...}
@Override
public boolean onOptionsItemSelected(MenuItem item) {...}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_starting_point,
container, false);
return rootView;
}
}
}
add.setOnClickListener(lis);使应用程序崩溃。
这是fragment_starting_point.xml ...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvTotal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="The total is 0"
android:layout_gravity="top"
android:gravity="center"
android:textSize="30sp"
android:textColor="@color/red"
/>
<Button
android:id="@+id/bAdd"
android:layout_width="150sp"
android:layout_height="wrap_content"
android:text="Add 1"
android:textSize="20sp"
android:layout_gravity="center"
android:gravity="center"
/>
<Button
android:id="@+id/bSub"
android:layout_width="150sp"
android:layout_height="wrap_content"
android:text="Subtract 1"
android:textSize="20sp"
android:layout_gravity="center"
android:gravity="center"
/>
这就是logCat所说的......
05-19 23:23:09.700: D/CLIPBOARD(16417): Hide Clipboard dialog at Starting input: finished by someone else... !
05-19 23:23:34.085: I/dalvikvm(16518): Could not find method android.widget.LinearLayout$LayoutParams.<init>, referenced from method android.support.v7.internal.view.menu.ActionMenuView$LayoutParams.<init>
05-19 23:23:34.085: W/dalvikvm(16518): VFY: unable to resolve direct method 8208: Landroid/widget/LinearLayout$LayoutParams;.<init> (Landroid/widget/LinearLayout$LayoutParams;)V
05-19 23:23:34.085: D/dalvikvm(16518): VFY: replacing opcode 0x70 at 0x0000
05-19 23:23:34.115: D/CLIPBOARD(16518): Hide Clipboard dialog at Starting input: finished by someone else... !
05-19 23:23:37.760: D/CLIPBOARD(16518): Hide Clipboard dialog at Starting input: finished by someone else... !
05-19 23:28:52.000: D/AndroidRuntime(16834): Shutting down VM
05-19 23:28:52.000: W/dalvikvm(16834): threadid=1: thread exiting with uncaught exception (group=0x40192760)
05-19 23:28:52.000: E/AndroidRuntime(16834): FATAL EXCEPTION: main
05-19 23:28:52.000: E/AndroidRuntime(16834): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example2.mysecondapp/com.example2.mysecondapp.StartingPoint}: java.lang.NullPointerException
05-19 23:28:52.000: E/AndroidRuntime(16834): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
05-19 23:28:52.000: E/AndroidRuntime(16834): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
05-19 23:28:52.000: E/AndroidRuntime(16834): at android.app.ActivityThread.access$500(ActivityThread.java:122)
05-19 23:28:52.000: E/AndroidRuntime(16834): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
05-19 23:28:52.000: E/AndroidRuntime(16834): at android.os.Handler.dispatchMessage(Handler.java:99)
05-19 23:28:52.000: E/AndroidRuntime(16834): at android.os.Looper.loop(Looper.java:132)
05-19 23:28:52.000: E/AndroidRuntime(16834): at android.app.ActivityThread.main(ActivityThread.java:4123)
05-19 23:28:52.000: E/AndroidRuntime(16834): at java.lang.reflect.Method.invokeNative(Native Method)
05-19 23:28:52.000: E/AndroidRuntime(16834): at java.lang.reflect.Method.invoke(Method.java:491)
05-19 23:28:52.000: E/AndroidRuntime(16834): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
05-19 23:28:52.000: E/AndroidRuntime(16834): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
05-19 23:28:52.000: E/AndroidRuntime(16834): at dalvik.system.NativeStart.main(Native Method)
05-19 23:28:52.000: E/AndroidRuntime(16834): Caused by: java.lang.NullPointerException
05-19 23:28:52.000: E/AndroidRuntime(16834): at com.example2.mysecondapp.StartingPoint.onCreate(StartingPoint.java:47)
05-19 23:28:52.000: E/AndroidRuntime(16834): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
05-19 23:28:52.000: E/AndroidRuntime(16834): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
05-19 23:28:52.000: E/AndroidRuntime(16834): ... 11 more
05-19 23:28:59.720: I/Process(16834): Sending signal. PID: 16834 SIG: 9
我不知道到底需要什么,所以我发布了我认为相关的所有内容,我还是新手。我真的很感激任何帮助。
答案 0 :(得分:1)
Fragment
的布局包含Button
,其内容为R.id.bAdd
,Activity
的布局不包含add = (Button)this.findViewById(R.id.bAdd);
。这是您的错误来源,这一行:
Button
在Activity
的布局中找不到OnClickListener
,因此它返回null。之后,当您尝试设置NullPointerException
时,您会获得Buttons
。
您需要做的是将与Fragment
等的所有互动转移到它不属于Activity
的{{1}}中,因为它不属于Activity
onCreateView()
的布局。覆盖Fragment
中的Views
方法并在那里夸大您的布局,然后像这样查找@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_starting_point, container, false);
this.add = (Button) view.findViewById(R.id.bAdd);
return view;
}
:
OnClickListener
然后,您可以在onViewCreated()
中添加@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
this.add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
...
}
});
}
:
{{1}}
答案 1 :(得分:0)
这是因为findViewById()
在activity_main
布局中搜索,而按钮位于片段的布局fragment_main
中。
在片段的onCreateView()
方法中移动该段代码:
//...
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button buttonClick = (Button)rootView.findViewById(R.id.button);
buttonClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onButtonClick((Button) view);
}
});
请注意,现在您可以通过rootView view
访问它:
Button buttonClick = (Button)rootView.findViewById(R.id.button);
否则你会再次NullPointerException
。