0

여기에 몇 가지 비슷한 질문이 있지만 그 중 누구도 내 문제를 설명하지 못했습니다. 나는 단순히 그것의 ID에 의해 TextView를 참조하려하지만, 아무 것도하지 않으면 널 포인터 예외가 계속 발생한다. setContentView()가 호출되고 프래그먼트가 비정상적으로 시작된 후 내 과제를 확실히 작성했습니다. 다른 문제가 발생하지 않았는지 확인하기 위해 코드의 다른 부분을 제거하고 할당 후 null을 테스트했습니다. 이것에 상관없이 여전히 null로 표시됩니다.setContentView 이후 TextView가 null입니다.

여기 내 코드입니다 :

public class MainActivity extends Activity 
{ 
    //MEMBER VARIABLES 
    TextView mQuestionText; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     if (savedInstanceState == null) 
     { 
      getFragmentManager().beginTransaction() 
        .add(R.id.container, new PlaceholderFragment()).commit(); 
     } 

     mQuestionText = (TextView) findViewById(R.id.questionTextView); 
     if (mQuestionText == null) 
      System.out.println("debug:: " + "null"); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) 
    { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) 
     { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    /** 
    * A placeholder fragment containing a simple view. 
    */ 
    public static class PlaceholderFragment extends Fragment 
    { 

     public PlaceholderFragment() 
     { 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) 
     { 
      View rootView = inflater.inflate(R.layout.fragment_main, container, 
        false); 
      return rootView; 
     } 
    } 
} 

을 그리고 여기 fragment_main.xml이다 : 당신의 도움에 대한

<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" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="edu.wichita.eecs.cs697ac.w387u669.assignment3.MainActivity$PlaceholderFragment" > 

    <TextView 
     android:id="@+id/questionTextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="41dp" 
     android:text="Question" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/button1" 
     android:layout_toRightOf="@+id/questionTextView" 
     android:text="False" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/questionTextView" 
     android:layout_marginTop="35dp" 
     android:layout_toLeftOf="@+id/questionTextView" 
     android:text="True" /> 

</RelativeLayout> 

감사합니다!

편집 : 다른 연구를 수행 한 후 onStart() 메서드를 재정의하고 뷰 (심지어 조각의 코드 포함)에 액세스 할 수 있으므로 코드를 거기에서 이동할 수 있음을 알게되었습니다. 그러나 이것은 실제로 사용되기 위해 파편을 실제로 사용하지 않는 바이 패스입니다. 사람

답변

0

questionTextView 텍스트 뷰를하는 데 도움이

protected void onStart() 
    { 
     super.onStart(); 
     TextView test = (TextView) findViewById(R.id.questionTextView); 
     test.setText("hello"); 
    } 

희망은 PlaceholderFragmentfragment_main.xml 레이아웃, 그래서 같은 텍스트 뷰를 초기화하는 조각의 onCreateView를 사용 : 당신이에서를 참조 갈까요 당신의 조각을 사용하는 경우

View rootView = inflater.inflate(R.layout.fragment_main, container, 
        false); 
TextView mQuestionText = (TextView)rootView.findViewById(R.id.questionTextView); 
+0

일 즉 그! 문제일지도 모른다고 생각했지만 작동하도록 모든 것을 정적으로 선언해야하는 것에 대해 걱정했습니다. 문제는 끝나지 않았습니다. 다시 한 번 감사드립니다. – George

0

프래그먼트 OnCreateView 메서드

공용 클래스 MainActivity가 활동 당신은 setContentView(R.layout.activity_main) 설정 한 {

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    if (savedInstanceState == null) 
    { 
     getFragmentManager().beginTransaction() 
       .add(R.id.container, new PlaceholderFragment()).commit(); 
    } 


} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) 
{ 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) 
{ 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) 
    { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

/** 
* A placeholder fragment containing a simple view. 
*/ 
public static class PlaceholderFragment extends Fragment 
{ 

    public PlaceholderFragment() 
    { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) 
    { 
     View rootView = inflater.inflate(R.layout.fragment_main, container, 
       false); 
    TextView QuestionText = (TextView) rootView.findViewById(R.id.questionTextView);  
    if (mQuestionText == null) 
     System.out.println("debug:: " + "null"); 
     return rootView; 
    } 
} } 
+0

답변 해 주셔서 감사합니다. 이것은 결국 문제가되었지만, ρяσѕρєя K가 먼저 대답하여 내가 그들에게주었습니다 – George

+0

probs가 없습니다, 당신은 환영합니다. –

0

확장하지만 XML 이름 fragment_main.xml입니다 사용 된 setContentView에서이 XML은 setContentView(R.layout.fragment_main)