EditText.setText是片段中onActivityResult的空对象引用

时间:2016-06-13 14:31:43

标签: android fragment zxing

我试图使用一个名为Zxing的条形码扫描器,我正面临一个问题,即在onActivityResult上的Edittext里面设置一个值。

片段代码:

public class UnidadeTabFragment extends Fragment {

public EditText editarCodigoBarra;
public static final int DIALOG_FRAGMENT = 1;


@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.tab_fragmento_unidade,container,false);

    EditText editarCodigoBarra = (EditText) v.findViewById(R.id.codigobarraInput);
    ImageButton codigoBarraBotao = (ImageButton) v.findViewById(R.id.codigoBarraBotao);

    Bundle arguments = getArguments();
    Log.i("primeiro" , String.valueOf(arguments));
    if(arguments != null)
    {
        CharSequence codigoBarra = arguments.getCharSequence("codigo");
        Log.i("segundo barra" , String.valueOf(arguments));
        editarCodigoBarra.setText(codigoBarra);
    }

   codigoBarraBotao.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent myIntent = new Intent(getActivity(), BarCodeActivity.class);
            getActivity().startActivityForResult(myIntent , 12345);
        }
    });
    return v;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 12345) {
        Bundle bundle = this.getArguments();
        CharSequence codigoBarra = bundle.getCharSequence("codigo");
        Log.d("Activity Resultado", "value: " + codigoBarra);
        editarCodigoBarra.setText(codigoBarra);


    }
}

BarCode Actitvity:

public class BarCodeActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {

private ZXingScannerView mScannerView;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mScannerView = new ZXingScannerView(this);
  setContentView(mScannerView);
}

@Override
public void onResume() {
    super.onResume();
    mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
    mScannerView.setAutoFocus(true);
    mScannerView.startCamera();          // Start camera on resume
}

@Override
public void onPause() {
    super.onPause();
    mScannerView.stopCamera();           // Stop camera on pause
}

@Override
public void handleResult(Result rawResult) {
    Bundle bundle = new Bundle();
    CharSequence codigoString = rawResult.getText();
    bundle.putCharSequence("codigo", codigoString);
    UnidadeTabFragment unidadeTabFragment = new UnidadeTabFragment();
    unidadeTabFragment.setArguments(bundle);
    unidadeTabFragment.onActivityResult(12345, 12345, this.getIntent());  
   finish();


}

}

在logcat中返回' codigobarra'没关系!但当它试图在EditText上设置任何值时,erro会出现:

void android.widget.EditText.setText(java.lang.CharSequence)'在空对象引用上

我做错了什么?

提前致谢

3 个答案:

答案 0 :(得分:0)

实际上你正在创建一个新的EditText而不是初始化你想要的那个,改变它:

EditText editarCodigoBarra = (EditText) v.findViewById(R.id.codigobarraInput);

editarCodigoBarra = (EditText) v.findViewById(R.id.codigobarraInput);

答案 1 :(得分:0)

我认为你应该尝试onActivityResult

View v = inflater.inflate(R.layout.tab_fragmento_unidade,container,false);
EditText editarCodigoBarra = (EditText) v.findViewById(R.id.codigobarraInput);

创建新的本地引用不要使用类中的全局

答案 2 :(得分:0)

感谢答案的人,我找到了解决问题的方法......在我的TabAdapter上。 关键是引用适配器上的片段的方法。 我做了一些改变。获取我更新的文件。

Fragmento代码:

public class UnidadeTabFragment extends Fragment {

public EditText editarCodigoBarra;


@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.tab_fragmento_unidade,container,false);

    editarCodigoBarra = (EditText) v.findViewById(R.id.codigobarraInput);
    ImageButton codigoBarraBotao = (ImageButton) v.findViewById(R.id.codigoBarraBotao);


   codigoBarraBotao.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Fragment fragmentoCodigo = new CodigoBarraFragment();
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.add(R.id.container_body, fragmentoCodigo);
            fragmentTransaction.commit();
        }
    });

    return v;
}


public void alteraTextView(String texto) {
    editarCodigoBarra.setText(texto);
}

BarCode活动更改为片段,并将相机规则放在android>上6,直接从适配器调用Fragment引用:

public class CodigoBarraFragment extends Fragment implements ZXingScannerView.ResultHandler {

private ZXingScannerView mScannerView;
private static int PERMISSION_REQUEST_CAMERA_SCAN_QR=1;


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

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

    mScannerView = new ZXingScannerView(getActivity());

    if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
        requestPermissions(new String[]{android.Manifest.permission.CAMERA}, PERMISSION_REQUEST_CAMERA_SCAN_QR);
    }

    return mScannerView;
}

@Override
public void onResume() {
    super.onResume();
    mScannerView.setResultHandler(this);
    mScannerView.startCamera();
}

@Override
public void onPause() {
    super.onPause();
    mScannerView.stopCamera();

}

@Override
public void handleResult(Result result) {
    Bundle bundle = new Bundle();
    String codigoString = result.getText();
    bundle.putString("message", codigoString);
    FragmentManager fragmetn= getFragmentManager();
    FragmentTransaction ft = fragmetn.beginTransaction();
    fragmetn.beginTransaction();
    UnidadeTabFragment frag1 = (UnidadeTabFragment) TabsPagerAdapter.getFragment(0);
    frag1.alteraTextView(codigoString);
    fragmetn.popBackStack();
    ft.commit();
    this.onDestroy();

}

}

TabPagerAdapter代码:

public class TabsPagerAdapter extends FragmentStatePagerAdapter {

CharSequence Titles[]; 
int NumbOfTabs; 
protected static Hashtable<Integer, WeakReference<Fragment>> fragmentReferences = new Hashtable<>();


public TabsPagerAdapter(FragmentManager fm, CharSequence mTitles[], int mNumbOfTabsumb) {
    super(fm);
    this.Titles = mTitles;
    this.NumbOfTabs = mNumbOfTabsumb;
}


//This method return the fragment for the every position in the View Pager
@Override
public Fragment getItem(int position) {
    Fragment frag = null;

    if(position == 0) // if the position is 0 we are returning the First tab
    {
        frag = new UnidadeTabFragment();
    }else if(position == 1)             
    {
        frag = new DescricaoTabFragment();
    }else if (position == 2)
    {
        frag = new ConservacaoTabFragment();
    }else
    {
        frag = new ResumoTabFragment();
    }

    fragmentReferences.put(position, new WeakReference<Fragment>(frag));
    return frag;

}

public static Fragment getFragment(int fragmentId) {
    WeakReference<Fragment> ref = fragmentReferences.get(fragmentId);
    return ref == null ? null : ref.get();
}


// This method return the titles for the Tabs in the Tab Strip
@Override
public CharSequence getPageTitle(int position) {
    return Titles[position];
}

// This method return the Number of tabs for the tabs Strip
@Override
public int getCount() {
    return NumbOfTabs;
}

}

多数民众赞成,谢谢!