Android 磨损 setOnClickListener 崩溃
Android Wear setOnClickListener crash
我正在尝试为我的 Sony Smartwatch 3 编写应用程序,并且对 Android 编程有一些初学者的了解。我在我的应用程序中添加了一个按钮,我正在尝试连接所述按钮。唯一的问题是当我添加 setOnClickListener 时,我的应用程序崩溃并且无法加载。当我删除两个箭头之间指示的代码时,应用程序启动,并显示按钮,我可以单击它(当然没有任何反应)。所以我不确定我做错了什么,我搜索了又搜索,不幸的是没有找到答案。如果有任何帮助,我们将不胜感激,android 穿着的提示和技巧将非常棒!
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wear_start);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener()
{
@Override
public void onLayoutInflated(WatchViewStub stub)
{
mTextView = (TextView) stub.findViewById(R.id.text);
}
});
Button startingButton = (Button) findViewById(R.id.starting_button);
--> startingButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(WearStart.this, "You clicked it!", Toast.LENGTH_LONG).show();
}
--> });
activity_wear_start.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.WatchViewStub
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/watch_view_stub"
android:layout_width="match_parent" android:layout_height="match_parent"
app:rectLayout="@layout/rect_activity_wear_start"
app:roundLayout="@layout/round_activity_wear_start" tools:context=".WearStart"
tools:deviceIds="wear"></android.support.wearable.view.WatchViewStub>
rect_activity_wear_start.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical" tools:context=".WearStart"
tools:deviceIds="wear_square">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/start_button"
android:id="@+id/starting_button"
android:layout_gravity="center_horizontal"
android:clickable="true"
android:enabled="true" />
</LinearLayout>
崩溃日志
01-01 22:35:50.587 8740-8740/petryszak.kyle.dash E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: petryszak.kyle.dash, PID: 8740
java.lang.RuntimeException: Unable to start activity ComponentInfo{petryszak.kyle.dash/petryszak.kyle.dash.WearStart}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access0(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.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at petryszak.kyle.dash.WearStart.onCreate(WearStart.java:34)
at android.app.Activity.performCreate(Activity.java:5943)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access0(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)
将您的 button's click event
放入 onLayoutInflated()
方法中作为
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
mTextView = (TextView) stub.findViewById(R.id.text);
imageButton=(ImageButton)stub.findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Toast.makeText(getApplicationContext(),"Haiii",Toast.LENGTH_LONG).show();
Intent toNext=new Intent(Home.this,SecondActivity.class);
startActivity(toNext);
}
});
}
});
}
并完成此 link。这对 Android 可穿戴应用程序开发非常有用。
我正在尝试为我的 Sony Smartwatch 3 编写应用程序,并且对 Android 编程有一些初学者的了解。我在我的应用程序中添加了一个按钮,我正在尝试连接所述按钮。唯一的问题是当我添加 setOnClickListener 时,我的应用程序崩溃并且无法加载。当我删除两个箭头之间指示的代码时,应用程序启动,并显示按钮,我可以单击它(当然没有任何反应)。所以我不确定我做错了什么,我搜索了又搜索,不幸的是没有找到答案。如果有任何帮助,我们将不胜感激,android 穿着的提示和技巧将非常棒!
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wear_start);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener()
{
@Override
public void onLayoutInflated(WatchViewStub stub)
{
mTextView = (TextView) stub.findViewById(R.id.text);
}
});
Button startingButton = (Button) findViewById(R.id.starting_button);
--> startingButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(WearStart.this, "You clicked it!", Toast.LENGTH_LONG).show();
}
--> });
activity_wear_start.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.WatchViewStub
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/watch_view_stub"
android:layout_width="match_parent" android:layout_height="match_parent"
app:rectLayout="@layout/rect_activity_wear_start"
app:roundLayout="@layout/round_activity_wear_start" tools:context=".WearStart"
tools:deviceIds="wear"></android.support.wearable.view.WatchViewStub>
rect_activity_wear_start.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical" tools:context=".WearStart"
tools:deviceIds="wear_square">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/start_button"
android:id="@+id/starting_button"
android:layout_gravity="center_horizontal"
android:clickable="true"
android:enabled="true" />
</LinearLayout>
崩溃日志
01-01 22:35:50.587 8740-8740/petryszak.kyle.dash E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: petryszak.kyle.dash, PID: 8740
java.lang.RuntimeException: Unable to start activity ComponentInfo{petryszak.kyle.dash/petryszak.kyle.dash.WearStart}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access0(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.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at petryszak.kyle.dash.WearStart.onCreate(WearStart.java:34)
at android.app.Activity.performCreate(Activity.java:5943)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access0(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)
将您的 button's click event
放入 onLayoutInflated()
方法中作为
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
mTextView = (TextView) stub.findViewById(R.id.text);
imageButton=(ImageButton)stub.findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Toast.makeText(getApplicationContext(),"Haiii",Toast.LENGTH_LONG).show();
Intent toNext=new Intent(Home.this,SecondActivity.class);
startActivity(toNext);
}
});
}
});
}
并完成此 link。这对 Android 可穿戴应用程序开发非常有用。