应用程序关闭时,按下片段中定义的按钮(使用Veiwpager)

时间:2017-10-08 15:28:02

标签: java android android-fragments textview

我有一个从其他设备接收消息的项目。

当它收到数据时,会使用setText方法续订第三个片段中的TextView

我尝试了一些方法,但它们没有用。我遇到了一些问题,所以我希望有人可以提供帮助。

我以前直接设置片段的TextView;这就是为什么我现在不能设置它?

问题1:

首先,扫描QR码然后开始另一个活动来处理并立即更新片段3中的 TextView A 。 ((app关闭......但是 应用程序重启后,TextView已“更改”。

我在片段三中定义了一个按钮。按下时,它会在片段3中更新 TextView B 。((再次直接关闭....不确定TextView是否更改。

我希望片段三会在收到后立即更新数据。 我该怎么办?

以下是MainActivity中的代码,用于描述scanActivity处理QR代码后的操作。

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode,resultCode,data);
        if(requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            if(data != null) {
                final Barcode barcode = data.getParcelableExtra("barcode");
                parkingNumtmp = barcode.displayValue;
                Date date = new Date();
                final String time = simpleDateFormat.format(date);
                sharedPreferences.edit().putString("parkingNum", parkingNumtmp).putString("time", time).commit();

    *//I not sure following sentence correct or not.*
    *//I guess TextView.post is the problem , how to revise?
    *//because I want renew data immediately

                textView_parkingNoVal.post(new Runnable() {
                    @Override
                    public void run() {

    *//I define a method in fragment three to set TextView which in fragment three*
                        FragmentThree runParking = new FragmentThree();
                        runParking.setParkingText("No. : " + parkingNumtmp + "\t(" + time +")");
                    }
                });
            }
        }
    }

在MainActivity中,接口中定义的函数位于片段3中 (我猜 TextView.post 是问题,我是对的吗?)

@Override
    public void OnFragmentTwoReady() {

        button_scan = (Button) findViewById(R.id.button_scan);

        button_scan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ScanActivity.class);
                startActivityForResult(intent, REQUEST_CODE);
            }
        }) ;


    }

    @Override
    public void OnFragmentThreeReady() {

        button_find = (Button) findViewById(R.id.button_find);

        sharedPreferences = getSharedPreferences("Data", 0);
        if(sharedPreferences.contains("parkingNum") && sharedPreferences.contains("time")) {
            parkingNumtmp = sharedPreferences.getString("parkingNum", "");
            textView_parkingNoVal.setText("Parking No. : " + parkingNumtmp + "\t(" + sharedPreferences.getString("time", "") + ")");
        }

        Date date = new Date();
        textView_parkingNoVal = (TextView) findViewById(R.id.textView_parkingNoVal);

        textView_parkingNoVal.post(new Runnable() {
            @Override
            public void run() {
                textView_parkingNoVal.setText("Parking No. : " + parkingNumtmp + "\t(" + time +")");
            }
        });

        button_find.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(parkingNumtmp != null) {
                    try {

                    catch {
        });

    }

以下代码在片段三中。

public class FragmentThree extends Fragment {

    private OnFragmentThreeReadyListener callback;

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onAttach(Context context){
        super.onAttach(context);
        try{
            callback = (OnFragmentThreeReadyListener) context;
        }catch (ClassCastException e) {
            throw new ClassCastException(context.toString() + "must implements OnFragmentThreeListener");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container,  Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_three_layout,container,false);


        TextView textView_userIDVal = (TextView) rootView.findViewById(R.id.textView_userIDVal);
        return rootView;
    }

    // I use this method to change content of TextView in MainActivity
    public void setNumText(String str){
        Num.setText("No:"+str);


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }


    public interface OnFragmentThreeReadyListener {
        void OnFragmentThreeReady();
    }
}

问题2:

我有一个困惑,如果我需要在Fragment中更新TextView,我可以在片段中创建一个函数来更改TextView并在MainActivity中新建一个Fragment对象来更新吗?
onFragmentThreeReady() 仍然必须存在?在mainActivity中,它是否仍属于片段?
我应该在哪里放置我的TextView并使用setText。是MainActivity吗?还是碎片?

问题3:
在我的情况下,为什么导致应用程序关闭? 生命周期有什么问题吗?

有Logcat。

10-09 00:02:43.090 551-646/? E/WifiStateMachine: WifiStateMachine CMD_START_SCAN source -2 txSuccessRate=1.95 rxSuccessRate=1.87 targetRoamBSSID=00:00:00:00:00:00 RSSI=-29
10-09 00:03:03.310 551-646/? E/WifiStateMachine: WifiStateMachine CMD_START_SCAN source -2 txSuccessRate=1.05 rxSuccessRate=1.91 targetRoamBSSID=00:00:00:00:00:00 RSSI=-27
10-09 00:03:03.310 551-646/? E/WifiStateMachine: WifiStateMachine starting scan for "我跑很慢"WPA_PSK with 2457
10-09 00:03:22.160 247-6440/? E/IA NVM: ERROR:: NVM: Invalid AF orientation!
10-09 00:03:22.340 247-6440/? E/Arcsoft: BeautyShot SDK=ArcSoft_BeautyShot_3.0.0.1215_B2 Build:2015/01/04
10-09 00:03:22.340 247-6440/? E/Arcsoft: BeautyShot version: ArcSoft_FlawlessFace_3.0.0.1215, 12/26/2014
10-09 00:03:22.340 247-6440/? E/CamHAL_ABS: BS engine.Init(DataTypeVideo) OK
10-09 00:03:22.350 247-6440/? E/Camera_ISP: detachObserver failed!
10-09 00:03:22.350 247-6440/? E/Camera_ISP: detachObserver failed!
10-09 00:03:22.350 247-6440/? E/Camera_ISP: detachObserver failed!
10-09 00:03:22.360 247-6440/? E/Camera_ISP: detachObserver failed!
10-09 00:03:22.360 247-6440/? E/Camera_ISP: detachObserver failed!
10-09 00:03:22.630 247-28892/? E/Camera_V4L2DevBase: error subscribing event 4: Invalid argument
10-09 00:03:23.090 551-646/? E/WifiStateMachine: WifiStateMachine CMD_START_SCAN source -2 txSuccessRate=0.93 rxSuccessRate=0.87 targetRoamBSSID=00:00:00:00:00:00 RSSI=-37
10-09 00:03:23.090 551-646/? E/WifiStateMachine: WifiStateMachine starting scan for "我跑很慢"WPA_PSK with 2457
10-09 00:03:24.320 27285-27285/tw.com.flag.parking22 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: tw.com.flag.parking22, PID: 27285
                                                                       java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                           at tw.com.flag.parking22.FragmentThree.setParkingText(FragmentThree.java:80)
                                                                           at tw.com.flag.parking22.MainActivity$2.run(MainActivity.java:127)
                                                                           at android.os.Handler.handleCallback(Handler.java:739)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:135)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5264)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:372)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
10-09 00:03:24.630 551-1248/? E/ActivityManager: Invalid thumbnail dimensions: 479x479
10-09 00:03:26.210 551-637/? E/InputDispatcher: channel '2781942a tw.com.flag.parking22/tw.com.flag.parking22.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
10-09 00:03:26.210 551-637/? E/InputDispatcher: channel '335f0b92 tw.com.flag.parking22/tw.com.flag.parking22.ScanActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
10-09 00:03:26.250 229-1319/? E/BufferQueueProducer: [SurfaceView] queueBuffer: BufferQueue has been abandoned
10-09 00:03:26.250 247-28882/? E/Surface: queueBuffer: error queuing buffer to SurfaceTexture, -19
10-09 00:03:26.250 247-28882/? E/Camera_PreviewThread: Surface::queueBuffer returned error -19
10-09 00:03:26.400 247-28882/? E/IMGSRV: :0: gralloc_module_unlock: Buffer is already unlocked (78859)
10-09 00:03:26.400 229-270/? E/BufferQueueProducer: [SurfaceView] cancelBuffer: BufferQueue has been abandoned
10-09 00:03:26.400 229-630/? E/BufferQueueProducer: [SurfaceView] cancelBuffer: BufferQueue has been abandoned
10-09 00:03:26.400 229-1319/? E/BufferQueueProducer: [SurfaceView] cancelBuffer: BufferQueue has been abandoned
10-09 00:03:26.410 229-268/? E/BufferQueueProducer: [SurfaceView] cancelBuffer: BufferQueue has been abandoned
10-09 00:03:26.410 229-5752/? E/BufferQueueProducer: [SurfaceView] cancelBuffer: BufferQueue has been abandoned
10-09 00:03:26.410 229-270/? E/BufferQueueProducer: [SurfaceView] cancelBuffer: BufferQueue has been abandoned
10-09 00:03:41.190 247-247/? E/IA NVM: ERROR:: NVM: Invalid AF orientation!
10-09 00:03:41.380 247-247/? E/Arcsoft: BeautyShot SDK=ArcSoft_BeautyShot_3.0.0.1215_B2 Build:2015/01/04
10-09 00:03:41.380 247-247/? E/Arcsoft: BeautyShot version: ArcSoft_FlawlessFace_3.0.0.1215, 12/26/2014
10-09 00:03:41.380 247-247/? E/CamHAL_ABS: BS engine.Init(DataTypeVideo) OK
10-09 00:03:41.380 247-247/? E/Camera_ISP: detachObserver failed!
10-09 00:03:41.380 247-247/? E/Camera_ISP: detachObserver failed!
10-09 00:03:41.380 247-247/? E/Camera_ISP: detachObserver failed!
10-09 00:03:41.380 247-247/? E/Camera_ISP: detachObserver failed!
10-09 00:03:41.380 247-247/? E/Camera_ISP: detachObserver failed!
10-09 00:03:41.610 247-29164/? E/Camera_V4L2DevBase: error subscribing event 4: Invalid argument
10-09 00:03:42.200 28976-28976/tw.com.flag.parking22 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: tw.com.flag.parking22, PID: 28976
                                                                       java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                           at tw.com.flag.parking22.FragmentThree.setParkingText(FragmentThree.java:80)
                                                                           at tw.com.flag.parking22.MainActivity$2.run(MainActivity.java:127)
                                                                           at android.os.Handler.handleCallback(Handler.java:739)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:135)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5264)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:372)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
10-09 00:03:42.500 551-1134/? E/ActivityManager: Invalid thumbnail dimensions: 479x479
10-09 00:03:45.190 551-637/? E/InputDispatcher: channel '29556c1c Toast (server)' ~ Channel is unrecoverably broken and will be disposed!
10-09 00:03:45.190 551-637/? E/InputDispatcher: channel '255a144c tw.com.flag.parking22/tw.com.flag.parking22.ScanActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
10-09 00:03:45.250 229-270/? E/BufferQueueProducer: [SurfaceView] queueBuffer: BufferQueue has been abandoned
10-09 00:03:45.250 247-29154/? E/Surface: queueBuffer: error queuing buffer to SurfaceTexture, -19
10-09 00:03:45.250 247-29154/? E/Camera_PreviewThread: Surface::queueBuffer returned error -19
10-09 00:03:45.390 247-29153/? E/Camera_AtomAIQ: Error dequeuing statistics: -11
10-09 00:03:45.390 247-29154/? E/IMGSRV: :0: gralloc_module_unlock: Buffer is already unlocked (80088)

如果需要发布代码,我会尽快发布。谢谢!

0 个答案:

没有答案