この問題は、さまざまなサイトや質問でスタックオーバーフローについて調査しましたが、私の問題を解決した解決策が見つかりませんでした。私はしばらくの間この問題に苦しんでいますが、解決できません。findViewByIdを使用してFragmentクラスのTextviewのバンドルからテキストを設定するとき
私は2つのアクティビティと1つの断片を持っています。 最初のアクティビティ(概要)では、tのonCreateにFragmentを追加し、別のテキストとデフォルトのものを追加する必要があります。 2番目のアクティビティ(AddCity)は、このテキストデータをインテントとバンドルで概要に送信します。概要では、データが利用可能であり、私はmyfragment.setArguments(bundle)
でフラグメントに送信し、私はBundle bundle = getArguments()
とonCreateViewでのTextViewにアクセスしようとすると、私は次のエラーを取得する:
FATAL EXCEPTION: main
Process: com.myapp.www, PID: 19690
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
例外がStatusFragmentに次の行で発生します:
TextView cityText = (TextView) getView().findViewById(R.id.city_name);
は、私はすでにそのための独自のコンストラクタを使用してのアプローチを試してみましたが、私の知る限りでは、あなたがフラグメントで空のデフォルトコンストラクタ以外のカスタムコンストラクタを避ける必要があり、それはどちらか動作しませんでした。
私のクラスは、次のとおりです。
概要:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_overview);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setIcon(R.drawable.myicon);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
if(getIntent().getExtras() != null){
Bundle extras = getIntent().getExtras();
StatusFragment newFragment = new StatusFragment();
newFragment.setArguments(extras);
mSectionsPagerAdapter.addFragment(newFragment, extras.getString("city"));
}else{
Log.w("Overview-Bundle", "No Bundle Data");
}
mViewPager.setAdapter(mSectionsPagerAdapter);
}
AddCity:
このクラスは、JSON文字列を受け取り、私はに送信する必要のあるデータを得るためにそれを解析する方法を使用しています断片。これはうまくいくので、私はインテントをまとめた関連コードのみを与えます。 objはJSONオブジェクトです。 (あなたはより多くのコードが必要な場合は私に知らせて)
Intent i = new Intent(AddCity.this, Overview.class);
Bundle bundle = new Bundle();
bundle.putString("city", obj.getString("user_city"));
bundle.putString("country", obj.getString("user_ country"));
i.putExtras(bundle);
startActivity(i);
StatusFragment:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_for_overview, null);
setUI(view);
return view;
}
public void setUI(View view){
if(getArguments() != null) {
Bundle bundle = getArguments();
String city = bundle.getString("city");
String country = bundle.getString("country");
TextView cityText = (TextView) getView().findViewById(R.id.city_name);
TextView countryText = (TextView) getView().findViewById(R.id.country_name);
cityText.setText(city);
countryText.setText(country);
}else{
Log.w("Arguments", "no arguments");
}
}
私はすべての答えのために感謝されます。もっとコードを投稿する必要があるかどうかを教えてください。
をありがとうこれは質問がの重複として閉じた理由しかし、私は理解していない:)助けた「とは何NullPointerExceptionがある、と私はそれをどのように修正するのですか?」私はNullPointerExceptionが何であるか、なぜ発生したのかを知っています.- ...しかし、この特定のケースでは、フラグメント内のfindviewbyIdを使用していませんでした。なぜなら、私は決してフラグメントを使用していなかったからです。 – boehmP
@boehmP私は知っています。 NPEに関連しているため、その投稿に人を送る一般的なコンセンサスです。しかし、私は、NPEについての多くの良い質問にも同じ扱いがあることを見てきました。私はいつかメタ上で問題を提起するかもしれない。さらに、初心者のAndroid Studioユーザーであっても投稿が本当に役に立たないと私は同意します。 – Shaishav