我是Kotlin的新手,我正在尝试在我的Android项目中使用它.我有这个代码:
public var oneTouchTimer: CountDownTimer = CountDownTimer(500, 100) {
override fun onTick(l: Long) {
}
override fun onFinish() {
}
}
Run Code Online (Sandbox Code Playgroud)
它抛出了错误:
Cannot create an instance of an abstract class.
Run Code Online (Sandbox Code Playgroud)
基本上我正在尝试创建CountDownTimer的实例,并且无法弄清楚如何将其转换为Kotlin.
这是Java中的代码:
CountDownTimer oneTouchTimer = new CountDownTimer(500, 100) {
@Override
public void onTick(long l) {
}
@Override
public void onFinish() {
}
};
Run Code Online (Sandbox Code Playgroud) 由于某种原因在棒棒糖,我得到这个错误,因为棒棒糖是如此新的我无法找到为什么在任何地方,我不知道如何弄明白.在此先感谢Logcat错误:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.bent.MissionaryTracker/com.bent.MissionaryTracker.MainActivity}:
java.lang.RuntimeException: Font asset not found helvetica.ttf
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.RuntimeException: Font asset not found helvetica.ttf
at android.graphics.Typeface.createFromAsset(Typeface.java:190)
at com.bent.MissionaryTracker.MainActivity.onCreate(MainActivity.java:57)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
... 10 more
Run Code Online (Sandbox Code Playgroud)
编辑:这是导致错误的代码
title = (TextView) findViewById(R.id.title);
Typeface font = Typeface.createFromAsset(getAssets(), "helvetica.ttf");
title.setTypeface(font);
Run Code Online (Sandbox Code Playgroud)
我的项目文件夹中的assets文件夹中有helvetica.ttf.
编辑:此应用程序适用于所有设备,直到5.0,所以由于某种原因5.0 5.0无法识别我的资产文件夹中的文件.
我试图在我的资源文件夹中发布它的截图,但我没有足够的声誉来发布图像.
我正试图在我的Android应用程序中实现Facebook"Like Button".在我使用Facebook SDK v3之前,您将在其中设置LikeView,然后在onActivityResult()中调用likeView.handleOnActivityResult(context,requestCode,resultCode,data); 这将改变按钮,以便在页面被"喜欢"之后它将显示"喜欢"以及也喜欢该页面的人数.
现在,我正在使用Facebook SDK v4,因为现在不推荐使用v3.在这个版本中,我没有看到任何文档或者无论如何都没有为"喜欢"按钮提供相同类型的功能.它不再具有v3具有的likeView.handlePnActivityResult方法.现在,当用户点击"喜欢"按钮并喜欢该页面时,它不会改变按钮的状态.
有谁知道如何解决这个问题,以便它将具有与Facebook SDK v3中的LikeView相同的功能?
这是我正在做的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initialize FaceBook SDK
FacebookSdk.sdkInitialize(this);
setContentView(R.layout.activity_about);
// Set up ActionBar
actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
terms = (LinearLayout) findViewById(R.id.terms_holder);
privacyPolicy = (LinearLayout) findViewById(R.id.privacy_policy_holder);
share = (LinearLayout) findViewById(R.id.social_media_holder);
environmentButton = (Button) findViewById(R.id.environment_change);
likeView = (LikeView) findViewById(R.id.like_view);
likeView.setObjectIdAndType("##############", LikeView.ObjectType.PAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// In the old Facebook SDK this is where it would …Run Code Online (Sandbox Code Playgroud) 我试图直接访问片段中的按钮(android.support.v4),这就是它抛出的内容:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at
com.....fragments.HomeFragment._$_findCachedViewById(HomeFragment.kt)
at
com.....fragments.HomeFragment.onCreateView(HomeFragment.kt:25)
Run Code Online (Sandbox Code Playgroud)
这是我试图在fragment_home.xml中访问的按钮:
<Button
android:id="@+id/batteryInstallationButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/battery_installation"
android:layout_weight="1"
android:textSize="12sp"
android:background="@drawable/custom_button"
android:textColor="@color/white"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"/>
Run Code Online (Sandbox Code Playgroud)
这是HomeFragment.kt中的Kotlin代码
import android.content.Context
import android.net.Uri
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import kotlinx.android.synthetic.main.fragment_home.*
class HomeFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val rootView = inflater!!.inflate(R.layout.fragment_home, container, false)
batteryInstallationButton?.setOnClickListener …Run Code Online (Sandbox Code Playgroud)