我正在搜索如何制作图像按钮,当您点击它时会打开一个新的活动。我做得对,因为eclipse没有显示任何错误但是当我尝试在我的xperia z上运行它并且模拟器崩溃时,任何人都有了想法?
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
ImageButton ImageButton = (ImageButton) findViewById(R.id.search);
ImageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick (View v) {
Intent intent = new Intent(MainActivity.this, SearchActivity.class);
startActivity(intent);
}
});
}
}
public static class PlaceholderFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
}
}
logcat的:
05-18 15:07:10.389: I/ActivityManager(847): Start proc com.example.skatelogic for activity com.example.skatelogic/.MainActivity: pid=18106 uid=10189 gids={50189, 1028}
05-18 15:07:10.499: D/skia(18106): new locale en-Latn-US
05-18 15:07:10.529: D/AndroidRuntime(18106): Shutting down VM
05-18 15:07:10.529: W/dalvikvm(18106): threadid=1: thread exiting with uncaught exception (group=0x415af8b0)
05-18 15:07:10.529: E/AndroidRuntime(18106): FATAL EXCEPTION: main
05-18 15:07:10.529: E/AndroidRuntime(18106): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.skatelogic/com.example.skatelogic.MainActivity}: java.lang.NullPointerException
05-18 15:07:10.529: E/AndroidRuntime(18106): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
05-18 15:07:10.529: E/AndroidRuntime(18106): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
05-18 15:07:10.529: E/AndroidRuntime(18106): at android.app.ActivityThread.access$600(ActivityThread.java:150)
05-18 15:07:10.529: E/AndroidRuntime(18106): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
05-18 15:07:10.529: E/AndroidRuntime(18106): at android.os.Handler.dispatchMessage(Handler.java:99)
05-18 15:07:10.529: E/AndroidRuntime(18106): at android.os.Looper.loop(Looper.java:213)
05-18 15:07:10.529: E/AndroidRuntime(18106): at android.app.ActivityThread.main(ActivityThread.java:5225)
05-18 15:07:10.529: E/AndroidRuntime(18106): at java.lang.reflect.Method.invokeNative(Native Method)
05-18 15:07:10.529: E/AndroidRuntime(18106): at java.lang.reflect.Method.invoke(Method.java:525)
05-18 15:07:10.529: E/AndroidRuntime(18106): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
05-18 15:07:10.529: E/AndroidRuntime(18106): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
05-18 15:07:10.529: E/AndroidRuntime(18106): at dalvik.system.NativeStart.main(Native Method)
05-18 15:07:10.529: E/AndroidRuntime(18106): Caused by: java.lang.NullPointerException
05-18 15:07:10.529: E/AndroidRuntime(18106): at com.example.skatelogic.MainActivity.onCreate(MainActivity.java:28)
05-18 15:07:10.529: E/AndroidRuntime(18106): at android.app.Activity.performCreate(Activity.java:5133)
05-18 15:07:10.529: E/AndroidRuntime(18106): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-18 15:07:10.529: E/AndroidRuntime(18106): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2230)
05-18 15:07:10.529: E/AndroidRuntime(18106): ... 11 more
05-18 15:07:10.529: W/ActivityManager(847): Force finishing activity com.example.skatelogic/.MainActivity
05-18 15:07:10.829: I/WindowManager(847): Screenshot max retries 4 of Token{41c6c138 ActivityRecord{4209fb70 u0 com.example.skatelogic/.MainActivity}} appWin=Window{41e5f528 u0 Starting com.example.skatelogic} drawState=4
05-18 15:07:10.829: W/WindowManager(847): Screenshot failure taking screenshot for (1080x1920) to layer 21025
05-18 15:07:10.920: I/CrashMonitor(14341): CrashMonitorServiceBroadcastReceiver: Forwarding intent: android.intent.action.DROPBOX_ENTRY_ADDED tag: data_app_crash
05-18 15:07:10.930: I/CrashMonitor(14341): CrashMonitorService: Service started...
答案 0 :(得分:0)
正如@Fllo所评论的那样,导致NPE是因为ImageButton
被声明为PlaceHolderFragment
的一部分;在Activity.onCreate()
,Fragment
的根视图尚未附加到活动的视图树中。
如果您保留PlaceHolderFragment
的实例,则可以获取根视图,然后从那里找到搜索图标:
PlaceHolderFragment fragment = new PlaceHolderFragment();
ImageButton button = (ImageButton) fragment.getView().findViewById(R.id.search);
也就是说,您将使用View.findViewById()
,而不是Activity.findViewById()
。
我更喜欢这种方法而不是将OnClickListener移动到Fragment,因为它们只是一组Views的方便容器;除了知道他们住在活动中之外,理想情况下他们不知道其他任何事情(包括他们所在的活动);它们应尽可能自包含,以允许在不同的活动中重用,并通过从片段中启动活动,它表明它对其上下文了解得太多。