0

I have a project that receives messages from other devices.

When it receives data, it then uses the setText method to renew the TextView in the third fragment.

I tried some methods but they didn't work. I'm having some problems, so I hope someone can help.

I previously directly set TextView of fragment; is this why I cannot set it now?

Question 1:

At first,scan the QR code then start another activity to deal with and immediately renew TextView A in fragment Three. ((app shut down...But after app restart , TextView has "Changed".

I defined a button in fragment three. When pressed, it expected renew TextView B in fragment three.((directly shut down again....not sure TextView changed or not.

I want fragment three would immediately renew data as soon as received. What should I do ?

Here is code in MainActivity that describe action after scanActivity deal with QR code .

@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 +")");
                    }
                });
            }
        }
    }

In MainActivity ,there is the function defined in interface which is in fragment three.
(I guess TextView.post is the problem ,am I right? )

@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 {
        });

    }

Following code is in Fragment Three.

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();
    }
}

Question 2:

I have a confusion that if I need to renew TextView in Fragment ,can I create a function in fragment to change TextView and new a Fragment object in MainActivity to renew it?
onFragmentThreeReady( ) still have to exist? In mainActivity ,is it still belong to fragment?
And where should I put my TextView and use setText. Is MainActivity ? Or Fragment?

Question 3:
In such of my case,why cause app shut down?
Is there any problem about lifecycle?

There is 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)

If there is need to post code,I will post it as soon as I can. Thank you!

Roddy of the Frozen Peas
  • 14,380
  • 9
  • 49
  • 99

0 Answers0