Android Wear setOnClickListener崩溃

Android Wear setOnClickListener崩溃,android,wear-os,sony-smartwatch,Android,Wear Os,Sony Smartwatch,我正在尝试为我的Sony Smartwatch 3编写一个应用程序,并且有一些Android编程的初学者知识。我已经在我的应用程序中添加了一个按钮,我正在尝试连接这个按钮。唯一的问题是,当我添加setOnClickListener时,我的应用程序崩溃并无法加载。当我删除两个箭头之间的代码时,应用程序将启动,并显示按钮,我可以单击它(当然不会发生任何事情)。所以我不确定我做错了什么,我搜索了又搜索,不幸的是没有找到答案。任何帮助都将不胜感激,android wear的提示和技巧将非常棒 super

我正在尝试为我的Sony Smartwatch 3编写一个应用程序,并且有一些Android编程的初学者知识。我已经在我的应用程序中添加了一个按钮,我正在尝试连接这个按钮。唯一的问题是,当我添加setOnClickListener时,我的应用程序崩溃并无法加载。当我删除两个箭头之间的代码时,应用程序将启动,并显示按钮,我可以单击它(当然不会发生任何事情)。所以我不确定我做错了什么,我搜索了又搜索,不幸的是没有找到答案。任何帮助都将不胜感激,android wear的提示和技巧将非常棒

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();
        }
--> });
活动\u wear\u 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>
<?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>

按钮的单击事件
放入
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);

                }
            });


        }
    });
}

这将对Android可穿戴应用程序的开发非常有用。

你确定你的startingButton id是正确的吗?当您试图设置OnClickListener时,是否确定它不是
null
?请粘贴您的布局(活动开始)和崩溃日志。感谢您的帮助和链接!不幸的是,我不能投票支持你的答案,但我在心里支持你@凯尔,你需要15个声望才能投票。谢谢你的评论
@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);

                }
            });


        }
    });
}