मैं एंड्रॉइड विकास के लिए नया हूं और मैं एक छोटा, सरल एप्लिकेशन लिखने की कोशिश कर रहा हूं। इस एप्लिकेशन को 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"
/>
क्या मेरे Fragment के अंदर से गतिविधि_main.xml देखने का कोई तरीका है या क्या मुझे किसी अन्य फ़ाइल से बटन तक पहुंचना है? –
आप क्या हासिल करना चाहते हैं। टुकड़े के अंदर गतिविधि लेआउट के अंदर आपको घोषित बटन की आवश्यकता क्यों है? यदि यह खंड से लौटाए गए दृश्य से संबंधित है तो आपको इसे headline_view.xm – Blackbelt
के अंदर घोषित करना चाहिए, मैं बटन ईवेंट को सेट करना चाहता था, जो टुकड़ों के अंदर फ्रेजमेंट की सामग्री को बदलता था क्योंकि मैंने सोचा था कि यह किसी प्रकार की अच्छी शैली होगी। लेकिन अब मैं इसे किसी अन्य तरीके से करने की कोशिश करूंगा, धन्यवाद! –