2017-04-18 15 views
-1

이전에 여러 번 대답했음을 말하기 전에이 질문을 한 대부분의 사람들을 잘못된보기 ID 또는 TextView가 위치한 곳과 다른 ContentView를 설정하십시오. 나는 이미 잘 작동하는 다른 곳에서이 함수를 사용했다. TextView.setText()에서 문자열 리터럴을 사용해 보았지만 무의미했습니다.Textview.setText 올바른 contentView에있는보기에도 불구하고 nullpointer를 던지고보기

contactstory.xml 파일이

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<TextView 
    android:id="@+id/textyman" 
    android:layout_width="match_parent" 
    android:layout_height="300dp" 
    android:layout_marginTop="100dp" 
    android:text="TextView" /> 
<Button 
    android:id="@+id/close" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Close" /> 
</LinearLayout> 

public void ShowStory(int pos) 
{ 
    final Dialog dialog = new Dialog(this); 
    dialog.setTitle(srcnames[pos]); 
    dialog.setContentView(R.layout.contactstory); 
    String username=getIntent().getStringExtra("username"); 
    LoginDataBaseAdapter loginDataBaseAdapter=new 
    LoginDataBaseAdapter(this); 
    loginDataBaseAdapter.open(); 
    String story=loginDataBaseAdapter.GetStory(pos,username); 
    TextView t1=(TextView)findViewById(R.id.textyman); 
    Button btnend=(Button)findViewById(R.id.close); 
    if(TextUtils.isEmpty(story)){ 
     t1.setText(username+" hasn't added any story for this song"); 
    } 
    else { 
     t1.setText(story); //Exception is thrown here 
    } 

    btnend.setOnClickListener(new View.OnClickListener(){ 
    public void onClick(View v) { 
     dialog.dismiss(); 
    } 
}); 
    dialog.show(); 

} 

로그 캣

FATAL EXCEPTION: main 

java.lang.NullPointerException 

at com.example.sherry.escuchame.ContactInfo.ShowStory(ContactInfo.java:157) 

at 
com.example.sherry.escuchame.ContactInfo.onContextItemSelected 
(ContactInfo.java:140) 

at android.app.Activity.onMenuItemSelected(Activity.java:2660) 

at android.support.v4.app.FragmentActivity.onMenuItemSelected 
(FragmentActivity.java:408) 

at 
android.support.v7.app.AppCompatActivity.onMenuItemSelected 
(AppCompatActivity.java:195) 

at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected 
(WindowCallbackWrapper.java:113) 
+0

[NullPointerException은 무엇이며 어떻게 수정합니까?] (http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i) -fix-it) –

답변

0

설정하지 마십시오 패스 레이아웃 ID를 직접 R.layout.contactstory 같은 자바 파일의 기능을 대신 사용 아래의 코드와 같은 레이아웃을 팽창시키기위한 인플레이터 레이아웃

View view = LayoutInflater.from(context).inflate(R.layout.contactstory,null); 
dialog.setContentView(view); 
TextView t1=(TextView)view.findViewById(R.id.textyman); 
+0

그게 효과가있다. 고맙습니다. :디 –