2016-03-26 171 views
0

UPDATE無法解析方法maketext

當我使用此代碼Toast.makeText(Lesson111.this,rb.getText(),Toast.LENGTH_SHORT).show();我收到錯誤消息說不能解決方法maketext。然後我看到這個(The method makeText in the type Toast is not applicable for the arguments)我應用了代碼Toast.makeText(Lesson111.this.getActivity(),rb.getText(),Toast.LENGTH_SHORT).show();而不是Toast.makeText(Lesson111.this,rb.getText(),Toast.LENGTH_SHORT).show(); 。這個錯誤不知何故就消失了。但是,當我嘗試運行該應用程序並單擊Lesson111時。它迫使應用程序關閉。我錯過了什麼嗎?

package com.android.pet.view; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.View; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.Toast; 
import android.view.LayoutInflater; 
import android.view.ViewGroup; 

import com.doepiccoding.navigationdrawer.R; 

public class Lesson111 extends Fragment { 
    private RadioGroup radioGroup; 


    public View onCreateView(LayoutInflater Inflater, ViewGroup container,Bundle savedInstanceState) { 
     View rootView = Inflater.inflate(R.layout.twopointthree, null); 

    /* Initialize Radio Group and attach click handler */ 
     radioGroup = (RadioGroup) rootView.findViewById(R.id.radioGroup); 
     radioGroup.clearCheck(); 

    /* Attach CheckedChangeListener to radio group */ 
     radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(RadioGroup group, int checkedId) { 
       RadioButton rb = (RadioButton) group.findViewById(checkedId); 
       if(null!=rb && checkedId > -1){ 
        Toast.makeText(Lesson111.this.getActivity(), rb.getText(), Toast.LENGTH_SHORT).show(); 
       } 

      } 
     }); 
     return rootView; 
    } 

    public void onClear(View v) { 
    /* Clears all selected radio buttons to default */ 
     radioGroup.clearCheck(); 
    } 

    public void onSubmit(View v) { 
     RadioButton rb = (RadioButton) radioGroup.findViewById(radioGroup.getCheckedRadioButtonId()); 
     Toast.makeText(Lesson111.this.getActivity(), rb.getText(), Toast.LENGTH_SHORT).show(); 
    } 
} 

這裏顯示的內容在logcat中,進出口使用藍疊作爲模擬器

6月3日至27日:04:19.703 7458-7458 /? E/AndroidRuntime:致命例外:main 進程:com.doepiccoding.navigationdrawer,PID:7458 java.lang.NullPointerException at com.android.pet.view.Lesson111.onCreateView(Lesson111.java:22) at android。 support.v4.app.Fragment.performCreateView(Fragment.java:1478) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927) at android.support.v4.app.FragmentManagerImpl.moveToState( FragmentManager.java:1104) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460) android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:440) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java: 95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5021) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:515) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:827) 在com.android.internal.os.ZygoteInit .main(ZygoteInit.java:643) at dalvik.system.NativeStart.main(Native Method)

+0

你能張貼logcat的PLZ一RadioGroup中?所以我們可以看到當您嘗試顯示Toast時拋出了什麼異常。我想Lesson111.this.getActivity()可能會返回null,但很難確認沒有日誌 –

+1

@KevinLEGOFF我已經更新了帖子,此外,Im使用bluestacks作爲模擬器 – Dreamer

回答

1

它似乎從線22

radioGroup.clearCheck();

來的錯誤,您可以檢查佈局twopointthree有id爲 radioGroup中

+0

是的,你是對的。兩個三方都沒有RadioGroup。我添加了radiogroup並且它已經在工作,輸出結果已經顯示在我的導航抽屜中。非常感謝! – Dreamer