片段内部的图像按钮nullPoint Exception

时间:2018-06-02 06:54:50

标签: android android-fragments imageview

我将ImageView放在Fragment中。我还有其他问题,但找不到有用的。 这是我的代码:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        View v = inflater.inflate(R.layout.fragment_fragment_contect,container,false);
        ImageButton imageButton = (ImageButton)getView().findViewById( R.id.ib);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity( new Intent( getActivity(), Enquiry.class ) );
            }
        });

        return v;
    }

2 个答案:

答案 0 :(得分:2)

你需要在 getview()中这样做,你得到空指针异常使用视图。

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
{
    View v = inflater.inflate(R.layout.fragment_fragment_contect,container,false);
   ImageButton imageButton = v.findViewById(R.id.ib);
    imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity( new Intent( getActivity(), Enquiry.class ) );
        }
    });

    return v;
}

答案 1 :(得分:0)

由于使用Ran 1 test in 9.734s OK Traceback (most recent call last): File "F:\eclipse-jee-neon-3-win32\eclipse\plugins\org.python.pydev.core_6.3.3.201805051638\pysrc\runfiles.py", line 275, in <module> main() File "F:\eclipse-jee-neon-3-win32\eclipse\plugins\org.python.pydev.core_6.3.3.201805051638\pysrc\runfiles.py", line 97, in main return pydev_runfiles.main(configuration) # Note: still doesn't return a proper value. File "F:\eclipse-jee-neon-3-win32\eclipse\plugins\org.python.pydev.core_6.3.3.201805051638\pysrc\_pydev_runfiles\pydev_runfiles.py", line 874, in main PydevTestRunner(configuration).run_tests() File "F:\eclipse-jee-neon-3-win32\eclipse\plugins\org.python.pydev.core_6.3.3.201805051638\pysrc\_pydev_runfiles\pydev_runfiles.py", line 773, in run_tests all_tests = self.find_tests_from_modules(file_and_modules_and_module_name) File "F:\eclipse-jee-neon-3-win32\eclipse\plugins\org.python.pydev.core_6.3.3.201805051638\pysrc\_pydev_runfiles\pydev_runfiles.py", line 629, in find_tests_from_modules suite = loader.loadTestsFromModule(m) File "C:\Python27\lib\unittest\loader.py", line 65, in loadTestsFromModule tests.append(self.loadTestsFromTestCase(obj)) File "C:\Python27\lib\unittest\loader.py", line 56, in loadTestsFromTestCase loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames)) TypeError: __init__() takes exactly 3 arguments (2 given)

,您获得了NullPointException
select Employee, count(Employee) as TIMES,Post,c_name,Deli_Date,Note from delivery
group by Employee

解决方案:使用<style> /* http://meyerweb.com/eric/tools/css/reset/ v2.0 | 20110126 License: none (public domain) */ html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video { margin: 0; padding: 0; border: 0; font-size: 100%; font: inherit; vertical-align: baseline; } /* HTML5 display-role reset for older browsers */ article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section { display: block; } body { line-height: 1; } ol, ul { list-style: none; } blockquote, q { quotes: none; } blockquote:before, blockquote:after, q:before, q:after { content: ''; content: none; } table { border-collapse: collapse; border-spacing: 0; } </style> 代替import * as workerPath from 'file-loader?name=[name].js!./worker'; class VideoPlayerWorker { private worker: Worker; constructor({ onMessageCallback }: { onMessageCallback: Function }) { this.worker = new Worker(workerPath); this.initHandleMessage({ onMessageCallback }); } initTimer = (): void => { this.postMessage({ type: 'WORKER_INIT_TIMER' }); }; postMessage = (message: ApeVideo.VideoWorkerEventMessage): void => { this.worker.postMessage(message); }; initHandleMessage = ({ onMessageCallback }: { onMessageCallback: Function }): void => { this.worker.addEventListener('message', ({ data }: { data: ApeVideo.VideoWorkerEventMessage }): void => { onMessageCallback(data.type); }); }; destroy = (): void => { this.worker.terminate(); }; } export default VideoPlayerWorker;

Cannot find module 'file-loader?name=[name].js!./worker' from 'playerWorkerGetaway.ts'

Android Fragment Referance