I'm getting an API data in a fragment to change the Activity txt. But when I call the function that makes this change the setText gives NullPointerException. If I call the same function on the activity screen it works, but if I call anywhere in the fragment it gives error.
ScanActivity.java
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class ScanActivity extends BaseActivity {
private TextView txtResultado;
private TextView txtInfo;
private TextView txtHorario;
private TextView txtData;
private TextView txtUrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
setupToolbar();
txtResultado = findViewById(R.id.txt_result);
txtInfo = findViewById(R.id.txt_info);
txtHorario = findViewById(R.id.txt_horario);
txtData = findViewById(R.id.txt_data);
txtUrl = findViewById(R.id.txt_url);
setResult("teste 001", "teste URL");
}
public void setResult(String result, String fullURL){
Log.i("RESULTADO", result);
txtResultado.setText(result);
txtUrl.setText(fullURL);
}
}
ReaderFragment.java
import android.app.Fragment;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.zxing.Result;
import com.site.qrcode_demo.MySingleton;
import com.site.qrcode_demo.ScanActivity;
import com.site.qrcode_demo.R;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import me.dm7.barcodescanner.zxing.ZXingScannerView;
public class LeitorFragment extends Fragment implements ZXingScannerView.ResultHandler {
private static final String FLASH_STATE = "FLASH_STATE";
private static final String AUTO_FOCUS_STATE = "AUTO_FOCUS_STATE";
private static final String SELECTED_FORMATS = "SELECTED_FORMATS";
private static final String CAMERA_ID = "CAMERA_ID";
private ZXingScannerView mScannerView;
private boolean mFlash;
private boolean mAutoFocus;
private ArrayList<Integer> mSelectedIndices;
private int mCameraId = -1;
ScanActivity scanActivity;
String url = "https://httpbin.org/get";
private RequestQueue queue;
private String data;
Context context;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle state) {
mScannerView = new ZXingScannerView(getActivity());
if (state != null) {
mFlash = state.getBoolean(FLASH_STATE, false);
mAutoFocus = state.getBoolean(AUTO_FOCUS_STATE, true);
mSelectedIndices = state.getIntegerArrayList(SELECTED_FORMATS);
mCameraId = state.getInt(CAMERA_ID, -1);
} else {
mFlash = false;
mAutoFocus = true;
mSelectedIndices = null;
mCameraId = -1;
}
return mScannerView;
}
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
setHasOptionsMenu(true);
context = getActivity();
}
@Override
public void onResume() {
super.onResume();
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
@Override
public void handleResult(Result result) {
conection(result.getText());
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
mScannerView.resumeCameraPreview(LeitorFragment.this);
}
}, 1000);
}
@Override
public void onPause() {
super.onPause();
mScannerView.startCamera();
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_scan, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.menu_flash:
mFlash = !mFlash;
if (mFlash) {
item.setIcon(R.drawable.ic_flash_on);
} else {
item.setIcon(R.drawable.ic_flash_off);
}
mScannerView.setFlash(mFlash);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void conection(final String result) {
//Log.i("RESPOSTA:", resposta);
final String fullURL = url+"/"+result;
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
data = response.getString("url");
Log.i("DATA", data);
scanActivity = new ScanActivity();
scanActivity.setResult(data, fullURL);
//resposta = data;
} catch (JSONException e) {
Log.i("ERRO 1:", "Deu Erro no try");
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
queue.stop();
}
});
MySingleton.getInstance(context).addRequestque(jsonObjectRequest);
}
}
activity_scan.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.site.qrcode_demo.ScanActivity">
<fragment android:name="com.site.qrcode_demo.Fragments.LeitorFragment"
android:id="@+id/scanner_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_gravity="top"
android:minHeight="?attr/actionBarSize"
android:background="@color/actionbar_opacity"
app:theme="@style/TransparentToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/txt_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:layout_marginTop="80dp"
android:text="1250,50"
android:textAlignment="center"
android:textSize="30dp"
android:layout_marginHorizontal="@dimen/margin"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/txt_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:layout_marginRight="16dp"
android:layout_marginTop="120dp"
android:text="Informações adicionais"
android:textAlignment="center"
android:layout_marginHorizontal="@dimen/margin"
android:textColor="@android:color/white" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
android:layout_marginBottom="100dp"
android:gravity="center"
android:layout_marginHorizontal="@dimen/margin">
<TextView
android:id="@+id/txt_sensor"
android:text="Sensor x366"
android:layout_width="0dp"
android:layout_weight="1"
android:textColor="#ffffff"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textSize="25dp"/>
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="@+id/txt_horario"
android:text="09:04"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:textColor="#ffffff"
android:textAlignment="center"
android:textSize="35dp"/>
<TextView
android:id="@+id/txt_data"
android:text="06/04/2018"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="0dp"
android:textColor="#ffffff"
android:textAlignment="center"
android:textSize="20dp"/>
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/txt_url"
android:layout_gravity="center|bottom"
android:text="htto://URL.com/teste"
android:layout_width="wrap_content"
android:textColor="#ffffff"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"/>
<TextView
android:layout_gravity="center|bottom"
android:text="Place a barcode in the viewfinder rectangle to scan it."
android:layout_width="wrap_content"
android:textColor="#ffffff"
android:layout_height="wrap_content"/>
</FrameLayout>
PS: It used to work that way before. I do not know what change I made to this problem