【问题标题】:Error when adding "Define the Remote Input" code添加“定义远程输入”代码时出错
【发布时间】:2014-03-23 09:48:24
【问题描述】:

当我尝试添加“定义远程输入”代码以获取语音输入时,它在“reply_label”和“EXTRA_VOICE_REPLY”行中出现错误(试图替换它们,仍然错误):

package com.wear.myapp;

import android.app.Activity;
import android.app.Fragment;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.preview.support.v4.app.NotificationManagerCompat;
import android.preview.support.wearable.notifications.RemoteInput;
import android.support.v4.app.NotificationCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main);

        int notificationId = 001;
        // Build intent for notification content
        Intent viewIntent = new Intent(this, MainActivity.class); 
        viewIntent.putExtra("Helllo", "Hello Wear !");
        PendingIntent viewPendingIntent =
                PendingIntent.getActivity(this, 0, viewIntent, 0);

        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher) 
                .setContentTitle("My App") 
                .setContentText("Hello Wear !")
                .setContentIntent(viewPendingIntent);

        // Get an instance of the NotificationManager service
        NotificationManagerCompat notificationManager =
                NotificationManagerCompat.from(this);

        // Build the notification and issues it with notification manager.
        notificationManager.notify(notificationId, notificationBuilder.build()); 
        if (savedInstanceState == null) { 
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();

         // Key for the string that's delivered in the action's intent
            private static final String EXTRA_VOICE_REPLY = "extra_voice_reply";

            String replyLabel = getResources().getString(R.string.reply_label);

            RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
                    .setLabel(replyLabel)
                    .build();    

        }  
    } 
    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }


}

我再次尝试解决它,但没有解决,任何解决方案?

【问题讨论】:

    标签: java android wear-os


    【解决方案1】:

    private static final String EXTRA_VOICE_REPLY = "extra_voice_reply"; 是constant declaration,不能放在这里.. 把它移到MainActivity extends Activity之后的那一行,像这样:

    public class MainActivity extends Activity {
    
        private static final String EXTRA_VOICE_REPLY = "extra_voice_reply";
    
        // ...
    

    您还应该真正阅读basic java tutorial(用于基本语法)

    【讨论】:

    • 但它仍然给出错误: String replyLabel = getResources().getString(R.string.reply_label); ,“reply_line”下面有一条红线
    • 因为你需要一个带有那个id的android res中的字符串
    • 我添加了字符串,代码可以正常运行,但是磨损中没有显示回复按钮.....
    猜你喜欢
    • 1970-01-01
    • 2018-06-25
    • 2022-09-27
    • 2013-12-01
    • 2023-04-09
    • 1970-01-01
    • 2012-08-11
    • 2014-10-10
    • 1970-01-01
    相关资源
    最近更新 更多