2013-08-01 6 views
5

मैं एंड्रॉइड विकास के लिए नया हूं और मैं एक छोटा, सरल एप्लिकेशन लिखने की कोशिश कर रहा हूं। इस एप्लिकेशन को 2 संपादन टेक्स्ट से टेक्स्ट पढ़ने से और कुछ नहीं करना चाहिए और जब उपयोगकर्ता "भेजें" बटन दबाता है तो उसे टेक्स्ट को 2 टेक्स्ट व्यू पर लिखना चाहिए। इनमें से प्रत्येक टेक्स्ट व्यू एक टुकड़े में है।बटन खोजने के लिए फ्रैगमेंट में findViewById का कॉल शून्य

लेकिन मैं बटन पर ऑनक्लिकवेन्ट जोड़ नहीं सकता, क्योंकि findViewById (R.id.sendTextButton) हमेशा शून्य देता है। मैंने इस ऐप को काम करने की कोशिश की है लेकिन अब तक मुझे कोई समाधान नहीं मिला।

टुकड़ा की onCreateView समारोह, कि पहला इनपुट संभाल चाहिए (HeadlineFragment.java):

Button sendButton = null; 
View inflatedView = null; 

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

    this.inflatedView = inflater.inflate(R.layout.headline_view, container, false); 

    sendButton = (Button) inflatedView.findViewById(R.id.sendTextButton); 

    if(sendButton == null) 
    { 
     Log.d("debugCheck", "HeadFrag: sendButton is null"); 
     return inflatedView; 
    } 
    sendButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String headline = ((EditText) v.findViewById(R.id.enterHeadlineText)).getText().toString(); 
      ((TextView) v.findViewById(R.layout.headline_view)).setText(headline); 
     } 
    }); 
    return inflatedView; 
} 

लेआउट के साथ xml फ़ाइल (activity_main.xml

यहाँ प्रासंगिक codeblocks हैं):

<LinearLayout 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" 
     android:orientation="vertical"> 

     <EditText 
      android:id="@+id/enterHeadlineText" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:hint="@string/enterHeadlineTextHint" 
      android:maxLines="1" 
      android:inputType="textCapSentences" 
     /> 

     <EditText 
      android:id="@+id/enterMessageText" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:hint="@string/enterMessageTextHint" 
      android:maxLines="1" 
      android:inputType="textCapSentences" 
      /> 

     <Button 
      android:id="@+id/sendTextButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      android:text="@string/sendTextButton" 
      /> 



     <fragment android:name="com.example.mysecondapplication.HeadlineFragment" 
      android:id="@+id/headlineFragment" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="25dp" 
      /> 

     <fragment android:name="com.example.mysecondapplication.MessageFragment" 
      android:id="@+id/messageFragment" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dp" 
      /> 



</LinearLayout> 

और TextView साथ xml फ़ाइल, कि पाठ (headline_view.xml) शामिल करना चाहिए:

<?xml version="1.0" encoding="utf-8"?> 

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/headline_view" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="#FFFFFF" 
    android:text="@string/hello_world" 
/> 

उत्तर

7
this.inflatedView = inflater.inflate(R.layout.headline_view, container, false); 

sendButton = (Button) inflatedView.findViewById(R.id.sendTextButton); 

आप headline_view.xml अंदर आईडी sendTextButton के साथ एक बटन के लिए देख रहे हैं। लेकिन आपके द्वारा पोस्ट किए गए लेआउट से आईडी प्रेषण टेक्स्ट बटन के साथ कोई बटन नहीं है। कि `जिस तरह से आप अशक्त

+0

क्या मेरे Fragment के अंदर से गतिविधि_main.xml देखने का कोई तरीका है या क्या मुझे किसी अन्य फ़ाइल से बटन तक पहुंचना है? –

+0

आप क्या हासिल करना चाहते हैं। टुकड़े के अंदर गतिविधि लेआउट के अंदर आपको घोषित बटन की आवश्यकता क्यों है? यदि यह खंड से लौटाए गए दृश्य से संबंधित है तो आपको इसे headline_view.xm – Blackbelt

+0

के अंदर घोषित करना चाहिए, मैं बटन ईवेंट को सेट करना चाहता था, जो टुकड़ों के अंदर फ्रेजमेंट की सामग्री को बदलता था क्योंकि मैंने सोचा था कि यह किसी प्रकार की अच्छी शैली होगी। लेकिन अब मैं इसे किसी अन्य तरीके से करने की कोशिश करूंगा, धन्यवाद! –

1

आपका बटन activity_main.xml अंदर है मिलता है, लेकिन आप R.layout.headline_view बढ़ा-चढ़ाकर कर रहे हैं:

this.inflatedView = inflater.inflate(R.layout.headline_view, container, false); 
+1

@ अहमद संपादन के लिए धन्यवाद :) –

1

समस्या

sendButton.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     String headline = ((EditText) v.findViewById(R.id.enterHeadlineText)).getText().toString(); 
     ((TextView) v.findViewById(R.layout.headline_view)).setText(headline); 
    } 
}); 

वी देखें दृश्य क्लिक किया जाता है, इसलिए है कि coure v.findViewById() काम नहीं करता ..
अपने लक्ष्य को प्राप्त करने के लिए आप एडिटटेक्स्ट और टेक्स्टव्यू ग्लोबल को घोषित कर सकते हैं, क्रिएट() में इन्हें तुरंत चालू करने के लिए inflater का उपयोग करें और क्लिक करें जिस पर आप उनका उपयोग कर सकते हैं!

0

आपको उस गतिविधि के अंदर onClickListener सेट करना है, जिसमें से आप टुकड़े का प्रबंधन कर रहे हैं, न कि टुकड़े के अंदर, यदि बटन उस टुकड़े में नहीं है जो आपका टुकड़ा बढ़ रहा है।

संबंधित मुद्दे