2017-06-10 11 views
1

命令一樣findViewById工作,getSharedPreferences沒有內部Fragment 我使用科特林工作,我的代碼如下的getPreferences不片段

fun update (v:View){ 
Val sharedpref = getSharedPreferences("logindata",Context.MODE_PRIVATE)} 

LOG

E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.techno.app, PID: 25691 
java.lang.IllegalStateException: Could not find method update(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284) 
at android.view.View.performClick(View.java:5721) 
at android.widget.TextView.performClick(TextView.java:10936) 
at android.view.View$PerformClick.run(View.java:22620) 
at android.os.Handler.handleCallback(Handler.java:739) 
+0

嘗試使用var而不是val。 –

+0

它是val而不是val。 'Val'不是一種類型,只是用於變量(更準確:值)的聲明。 Kotlin然後使用類型推斷。 –

回答

0

禰概念明智上面的回答(https://stackoverflow.com/a/44469679/3845798)是正確的,但也有它需要在科特林。像getActivity()getView()將訪問像一個屬性。 另外,它的val不是Val

下面是如何在活動中使用findViewById(),getSharedPreferences()的簡單示例。

MainActivity碼 -

class MainActivity : AppCompatActivity() { 

    override fun onCreate(savedInstanceState: Bundle?) { 
     super.onCreate(savedInstanceState) 
     setContentView(R.layout.activity_main) 

     setBaseFragment() 
    } 


    private fun setBaseFragment() { 
     val fragment = MainFragment.newInstance() 
     supportFragmentManager 
       .beginTransaction() 
       .replace(R.id.fragment_container, fragment) 
       .commit() 
    } 
} 

這是我的片段類

class MainFragment : Fragment() { 

    lateinit var show: Button 
    lateinit var save: Button 
    lateinit var text: TextView 

    var prefs: SharedPreferences? = null 

    override fun onCreate(savedInstanceState: Bundle?) { 
     super.onCreate(savedInstanceState) 
    } 

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, 
           savedInstanceState: Bundle?): View? { 
     return inflater!!.inflate(R.layout.fragment_main, container, false) 
    } 

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) { 
     super.onViewCreated(view, savedInstanceState) 

     show = view?.findViewById(R.id.showButton) as Button 
     save = view?.findViewById(R.id.saveButton) as Button 
     text = view?.findViewById(R.id.textResult) as TextView 

     prefs = activity.getSharedPreferences("FUN",MODE_PRIVATE) 


     save.setOnClickListener { 
      val editor = prefs!!.edit() 
      editor.putString("Saving", "This is saveValueFromSharedPref") 
      editor.apply() 
     } 

     show.setOnClickListener { 
      text.setText(prefs?.getString("Saving","NotSaved")) 
     } 
    } 

    companion object { 
     fun newInstance(): MainFragment { 
      val fragment = MainFragment() 
      return fragment 
     } 
    } 
} 

這是一個簡單的例子,用文字和兩個按鈕。

首先你必須保存並顯示。

此外,對於您的應用程序崩潰,您可以檢查this solution

1

要調用Context對象在Fragment中,片段不是Context。因此,將此行更改爲如下所示:

Val sharedpref = getActivity().getSharedPreferences("logindata",Context.MODE_PRIVATE)} 

和使用onCreateViewgetView方法用於使用findViewById,例如:

TextView tv = (TextView) getView().findViewById(R.id.mtTextview); 
+0

我做了你所說的,因此沒有錯誤顯示在工作室,但當我運行應用程序在Android手機停止工作,當我打開該片段 –

+0

在這裏添加崩潰日誌 –

+0

如何獲取崩潰日誌 –