2017-12-05 1 views
0

내가 .. 내가 그것에서 값을 가지고 탭 조각 에 전달 활성을 가지고있는 탭 조각에, 나는의 setText이 값을 사용하려는조각을 대체하여 활동에서 Tab Fragment로 값을 전달하는 방법?

문제점이다이 값을하는 방법 탭 조각? 나는 항상이 예외를 얻는다. java.lang.IllegalArgumentException : 조각 Tab1에 대해 ID ....에 대한 뷰를 찾지 못했다. 어떻게 해결할 수 있는가?

나는 안드로이드 스튜디오에서 일반 탭 활동을 사용하고이 방법을 수정

homee.java이 내 활동 number.java

public class number extends AppCompatActivity { 
Button add,save; 
TextView num; 
int n; 
String stringNum; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_number); 

    add = findViewById(R.id.idadd); 
    save = findViewById(R.id.idsave); 
    num = findViewById(R.id.idnum); 

    add.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      //simple method 
      n = Integer.parseInt(num.getText().toString()); 
      num.setText(++n +""); 
     } 
    }); 

    save.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      stringNum= num.getText().toString(); 
// I want to pass this value to Tab1 Fragment by replaceFragment or by any 
//way 
      Bundle bundle = new Bundle(); 
      bundle.putString("key", stringNum); 
      Tab1 myFragment = new Tab1(); 
      myFragment.setArguments(bundle); 


    getSupportFragmentManager().beginTransaction().replace 
    (R.id.container,myFragment).commit(); 
      finish(); 
     } 
    }); 
}} 

입니다

public Fragment getItem(int position) { 
     switch (position){ 
      case 0: 
       Tab1 tab1 =new Tab1(); 
       return tab1; 
      case 1: 
       Tab2 tab2 =new Tab2(); 
       return tab2; 
      case 2: 
       Tab3 tab3 =new Tab3(); 
       return tab3; 
     } 
     return null ; 
    } 

이 탭의 TAB1 자식 활동

public class Tab1 extends Fragment { 

Button get,update; 
TextView txt; 

public Tab1(){} 

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
container, @Nullable Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.tab1, container, false); 

    get = view.findViewById(R.id.idget); 
    update = view.findViewById(R.id.idup); 
    txt = view.findViewById(R.id.textView); 

    get.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent in = new Intent(getContext(), number.class); 
      startActivity(in); 
     } 
    }); 

    update.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String Number= getArguments().getString("key").toString(); 
      //here the problem 
      txt.setText(Number); 
     } 
    }); 

     return view; 
     } 
    } 

activity_homee.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/main_content" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:context="eg.noname.opo.homee"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/appbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingTop="@dimen/appbar_padding_top" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:layout_weight="1" 
     android:background="?attr/colorPrimary" 
     app:layout_scrollFlags="scroll|enterAlways" 
     app:popupTheme="@style/AppTheme.PopupOverlay" 
     app:title="@string/app_name"> 

    </android.support.v7.widget.Toolbar> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <android.support.design.widget.TabItem 
      android:id="@+id/tabItem" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/tab_text_1" /> 

     <android.support.design.widget.TabItem 
      android:id="@+id/tabItem2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/tab_text_2" /> 

     <android.support.design.widget.TabItem 
      android:id="@+id/tabItem3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/tab_text_3" /> 

    </android.support.design.widget.TabLayout> 
</android.support.design.widget.AppBarLayout> 

<android.support.v4.view.ViewPager 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 

</android.support.design.widget.CoordinatorLayout> 

tab1.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" android:layout_height="match_parent" 
android:orientation="vertical" 
android:id="@+id/fra"> 

<TextView 
    android:id="@+id/textView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="111dp" 
    android:text="TextView" 
    android:textAlignment="center" 
    android:textSize="30sp" /> 

<Button 
    android:id="@+id/idget" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_marginTop="88dp" 
    android:text="get" /> 

<Button 
    android:id="@+id/idup" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="update" /> 
</LinearLayout> 

편집 나는이 값을 돌려줍니다 호출 정적 방법으로 그것을 해결하지만, 아직도 내가 값을 업데이트하는 조각에 버튼을 눌러야합니다 새로운 것에, 나는 나의 활동에서 그것을 통과 할 때 가치를 자동적으로 새롭게하는 방법을 발견하는 것을 시도하고있다. 어떤 도움? 활동 number.java에서

+0

당신은 질문의 첫 번째 두 라인을 설명 할 수있다. 뭐라 구요? –

+0

@AnkurKhandelwal 좋아, 내가 편집 했어. 지금은 분명하지? –

답변

0

in onCreate method just call 
    String Number= getArguments().getString("key").toString(); 
     //here the problem 
     txt.setText(Number) 
+0

하지만 여전히 번들에 의해 "Number"값을받을 수 없습니다 .. 항상이 예외가 발생합니다 java.lang.IllegalArgumentException : 조각에 대한보기 ID가 없습니다 .... 조각 Tab1 –

관련 문제