2016-11-12 22 views
0

文字列をフラグメントJavaクラスに渡す際に問題があります。私はそれが宣言され、textviewに表示されるように設定されても、null値を取得し続けました。私は私のonCreateView日付を文字列として文字列に渡す

 date = (TextView) rootView.findViewById(R.id.textDate); 

にこの行を挿入した

TextView date; 
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()); 
    date.setText(mydate); 

はここで参照のためのコードの残りの部分です:

public class Notifications extends Fragment { 
    TextView servingqueue; 
    TextView date; 

DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference(); 
DatabaseReference mServingQueue = mRootRef.child("ServingQueue"); 


public Notifications() { 
    // Required empty public constructor 
} 

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

     String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime()); 
     date.setText(mydate); 

    mServingQueue.addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      Double number = dataSnapshot.getValue(Double.class); 
      servingqueue.setText(String.valueOf(number)); 

     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 

     } 
    }); 
} 

@Override 
public void onAttach(Context context) { 
    super.onAttach(context); 

} 

@Override 
public void onDetach() { 
    super.onDetach(); 

} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_notifications, container, false); 
    // Inflate the layout for this fragmentid.queueServing); 
    servingqueue = (TextView) rootView.findViewById(R.id.queueServing); 
     date = (TextView) rootView.findViewById(R.id.textDate); 

    return rootView; 
} 

} 

は、ここに私のログに表示エラーです:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference 
                      at rogue.queue.Notifications.onCreateView(Notifications.java:74) 

ここには、 MENTレイアウト:しようとする

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="rogue.queue.Notifications" 
android:id="@+id/notification_relative_layout"> 

<!-- TODO: Update blank fragment layout --> 

<TextView 
    android:text="Your Number:" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="72dp" 
    android:id="@+id/textView3" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" /> 

<TextView 
    android:text="0000" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView3" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="40dp" 
    android:id="@+id/myqueue" 
    android:textSize="40sp" /> 

<TextView 
    android:text="Current Number:" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/textView9" 
    android:layout_marginTop="62dp" 
    android:layout_below="@+id/myqueue" 
    android:layout_toStartOf="@+id/myqueue" /> 

<TextView 
    android:text="PLEASE BE SEATED" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="78dp" 
    android:id="@+id/textView15" 
    android:layout_below="@+id/queueServing" 
    android:layout_centerHorizontal="true" /> 

<TextView 
    android:text="-" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/queueServing" 
    android:layout_alignBaseline="@+id/textView9" 
    android:layout_toEndOf="@+id/myqueue" /> 

<TextView 
    android:text="YOU WILL BE SERVED SHORTLY" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="19dp" 
    android:id="@+id/textView14" 
    android:layout_below="@+id/textView15" 
    android:layout_centerHorizontal="true" /> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:text="Welcome to Sunway Education Group" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentStart="true" /> 

<TextView 
    android:text="TextView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="68dp" 
    android:id="@+id/textDate" 
    tools:text="00 - JAN - 0000" 
    android:layout_below="@+id/textView14" 
    android:layout_alignEnd="@+id/textView9" /> 

<Button 
    android:text="CANCEL QUEUE" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="21dp" 
    android:id="@+id/buttonQueueCancel" 
    android:background="@android:color/holo_red_light" 
    /> 

+1

アンドロイドのドキュメントでフラグメントのライフサイクルを確認してください。 – Raghunandan

+0

'onCreateView'は' onCreate'の後に呼び出されるので、 'date'はあなたがそれを使っているときに設定されていません。代わりに 'onViewCreated'メソッドの中から' date'のテキストを見つけて設定してみてください。 – clownba0t

+0

@ clownba0t同じエラー、私はまだ取得仮想メソッドnullオブジェクト参照エラーを呼び出すしようとします。 –

答えて

0

私がコメントで示唆したように、テキストビューを見つけて、onViewCreatedメソッドでその値を設定してみてください。これは既に使用しているonCreateViewメソッドとは異なります。ここではいくつかのサンプルコードです:また

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
    date = (TextView) view.findViewById(R.id.textDate); 
    date.setText(DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime())); 
} 

、デバッグを支援するために、dateメンバ変数を設定するか、少なくとも今のところ、クラスのどこにも使用されていないことを確認してください。

+0

ありがとうございます。私はこれを挿入し、エラーはなかった。しかし文字列はその値をtextviewに渡さなかったので、textviewは空のままです。 –

+0

さて、まあ、それはうまくいけば正しい方向への一歩です。残りの問題は、いくつかの事柄のいずれかによって引き起こされる可能性があります。さらなる支援が必要な場合は、レイアウトファイル( 'R.layout.fragment_notifications')を投稿して、あなたの質問を' Notifications'クラスの現在のコードで更新してください。 – clownba0t

+0

編集を完了しました。 –

0

まず最初は、あなたのTextViewが正常に見つけることができるかどうかを確認、です。

date = (TextView) rootView.findViewById(R.id.textDate); 
if (date == null) 
    Log.e("x", "NOT FOUND"); 

このログ行が表示されるかどうかを確認してください。はいの場合、R.id.textDateTextViewではなく、rootViewは間違った親です。

あなたのTextViewが見つかった場合は、eventcallback 1で日付参照を設定し、callback2で何かを割り当てる代わりに、メソッドを作成することをお勧めします。

private void applyTextValues(Date date, float queueValue) { 
    TextView 
     date = (TextView) rootView.findViewById(R.id.textDate), 
     servingqueue = (TextView) rootView.findViewById(R.id.queueServing); 

     if (date != null) 
      date.setText(...); // Format this via SimpleDateFormat class 

     if (servingQueue != null) 
      servingQueue.setText(...); // Format your number. 

} 

そしてあなたがのonCreateで、どこがそれを必要とする、これまで、変更イベントでapplyTextValuesを呼び出します。このよう

。このようにして、ライフサイクル、Android側からの割り当て解除、アクティビティ関連アクションから独立しています。このメソッドを呼び出すと、最新の参照がここにあります。が完了しています。

+0

私はあなたのログメソッドを試しましたが、そのフラグメントをクリックするとエラーが発生しました。エラーはdate.setText(mydate)で発生します。 NULLオブジェクト参照を指しています。 –

+0

- 質問は既に解決されているので削除 - 申し訳ありませんでした - – Grisgram

関連する問題