【问题标题】:Android Google Analytics (R.id) Renaming ButtonsAndroid Google Analytics (R.id) 重命名按钮
【发布时间】:2011-11-15 13:49:04
【问题描述】:
Button createEventButton = (Button)findViewById(R.id.NewEventButton);

Button createPageButton = (Button)findViewById(R.id.NewPageButton);

你好 我已经搜遍了,但我不明白。我应该将“NewEventButton”更改为“button01”之类的东西吗? 问题是,如果我更改它,按钮将停止工作,正常单击按钮时,它会打开手机浏览器并导航到网站。

同样的情况也发生在“NewPageButton”上,例如 Eclipse 要求也更改为“button01”。

如果我让它们保持原样,我会出现“不能成为解析器或不是字段”之类的错误

提前致谢

更新 这是主要的xml文件,

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
android:background="@drawable/aevbackground"
android:id="@+id/main"
    >

    <Button android:text="Other" android:id="@+id/button9" android:layout_width="wrap_content" android:layout_height="33dp" 
android:onClick="myonclick1" android:layout_gravity="bottom|right"></Button>

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:layout_gravity="center"
   android:orientation="vertical">
   <TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello1"
    />

<Button android:text="Home" android:id="@+id/button01" android:layout_width="230dp" android:layout_height="34dp" 
android:onClick="openBrowser1"></Button>
<Button android:text="Forum" android:id="@+id/button2" android:layout_width="210dp" android:layout_height="34dp" 
android:onClick="openBrowser2"></Button>
<Button android:text="FOC" android:id="@+id/button3" android:layout_width="190dp" android:layout_height="34dp" 
android:onClick="openBrowser3"></Button>
<Button android:text="ON TFC" android:id="@+id/button4" android:layout_width="170dp" android:layout_height="34dp" 
android:onClick="openBrowser4"></Button>
<Button android:text="At Facebook" android:id="@+id/button5" android:layout_width="150dp" android:layout_height="34dp" 
android:onClick="openBrowser5"></Button>
<Button android:text="At Twitter" android:id="@+id/button6" android:layout_width="130dp" android:layout_height="34dp" 
android:onClick="openBrowser6"></Button>
<Button android:text="At Google+" android:id="@+id/button7" android:layout_width="110dp" android:layout_height="34dp" 
android:onClick="openBrowser7"></Button>     
<Button android:text="@string/linkEmail" android:id="@+id/button8" android:layout_width="90dp" android:layout_height="34dp" 
android:onClick="linkEmailClicked"></Button>

</LinearLayout>

</FrameLayout>

这是带有 Google 分析的主要 java 文件

package com.av0001;


import com.google.android.apps.analytics.GoogleAnalyticsTracker;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class Main extends Activity {
    /** Called when the activity is first created. */

    GoogleAnalyticsTracker tracker;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tracker = GoogleAnalyticsTracker.getInstance();
     // Start the tracker in manual dispatch mode...
        tracker.startNewSession("UA-25624837-1", this);
        setContentView(R.layout.main);

        Button createEventButton = (Button)findViewById(R.id.button01);
        createEventButton.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
            tracker.trackEvent(
                "Clicks",  // Category
                "Button",  // Action
                "clicked", // Label
                77);       // Value
          }
        });

        Button createPageButton = (Button)findViewById(R.id.button01);
        createPageButton.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
            // Add a Custom Variable to this pageview, with name of "Medium" and value "MobileApp" and
            // scope of session-level.
            tracker.setCustomVar(1, "Navigation Type", "Button click", 2);
            // Track a page view. This is probably the best way to track which parts of your application
            // are being used.
            // E.g.
            // tracker.trackPageView("/help"); to track someone looking at the help screen.
            // tracker.trackPageView("/level2"); to track someone reaching level 2 in a game.
            // tracker.trackPageView("/uploadScreen"); to track someone using an upload screen.
            tracker.trackPageView("/mainspalsh");
            tracker.trackPageView("/main");
            tracker.trackPageView("/main2");
            tracker.trackPageView("/main3");

          }
        });

        Button quitButton = (Button)findViewById(R.id.button01);
        quitButton.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
            finish();
          }
        });

        Button dispatchButton = (Button)findViewById(R.id.button01);
        dispatchButton.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
            // Manually start a dispatch, not needed if the tracker was started with a dispatch
            // interval.
            tracker.dispatch();
          }
        });
      }
        public void openBrowser1(View view) {
            Intent i = new Intent("android.intent.action.VIEW", Uri.parse(""));
           startActivity(i);
        }
        public void openBrowser2(View view) {
            Intent i = new Intent("android.intent.action.VIEW", Uri.parse(""));
            startActivity(i);
        }
        public void openBrowser3(View view) {
            Intent i = new Intent("android.intent.action.VIEW", Uri.parse(""));
            startActivity(i);
        }
        public void openBrowser4(View view) {
            Intent i = new Intent("android.intent.action.VIEW", Uri.parse(""));
            startActivity(i); 
        }
        public void openBrowser5(View view) {
            Intent i = new Intent("android.intent.action.VIEW", Uri.parse(""));
            startActivity(i);    
        }
        public void openBrowser6(View view) {
            Intent i = new Intent("android.intent.action.VIEW", Uri.parse(""));
            startActivity(i);
        }
        public void openBrowser7(View view) {
            Intent i = new Intent("android.intent.action.VIEW", Uri.parse(""));
            startActivity(i);
        } 
        public void linkEmailClicked(View v) {
            Intent it = new Intent(Intent.ACTION_SEND);
            String[] tos = {getString(R.string.emailAddress)};
            it.putExtra(Intent.EXTRA_EMAIL, tos);
            it.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.emailSubject));
            it.setType("text/plain");
            startActivity(it);
        }
        public void myonclick1(View view) {
            Intent mIntent = new Intent(this,Main2.class);
            startActivity(mIntent);
            Toast.makeText(
                    this,
                    "Thank you for using xxxxxxxxxxxxxxxxxxxxxx application, enjoy browsing :)", Toast.LENGTH_LONG).show();
            finish();  
        }
            @Override
            protected void onStart() {
                super.onStart();
                // The activity is about to become visible.
            }
            @Override
            protected void onResume() {
                super.onResume();
                // The activity has become visible (it is now "resumed").
            }
            @Override
            protected void onPause() {
                super.onPause();
                // Another activity is taking focus (this activity is about to be "paused").
            }
            @Override
            protected void onStop() {
                super.onStop();
                // The activity is no longer visible (it is now "stopped")
            }
            @Override
            protected void onDestroy() {
                super.onDestroy();
                // The activity is about to be destroyed.
             // Stop the tracker when it is no longer needed.
                tracker.stopSession();
            }
        }

【问题讨论】:

  • 显示活动的完整代码。
  • 清理你的代码可能是什么?项目->清理
  • 你应该为你的问题使用一个有意义的标题
  • 并显示您的 XML 布局。
  • 您好,clean 没有奏效。我现在如何发布xml?谢谢

标签: android button renaming


【解决方案1】:

听起来您正在重命名它但没有更改 XML 布局 ID 名称,因此 R.java 没有新名称。如果您也在更改 XML,那么您不会重新编译项目。确保在 Eclipse 环境中选择了自动编译。转到 Project -> Build Automatically 并确保它旁边有一个复选标记。

----备选建议---- 您使用的是什么版本的 eclipse 和 ADB...您可能必须升级到最新的并重新安装 ADB....我遇到了类似的编译错误,只有升级和重新安装才有效。你的代码对我来说编译得很好。不确定您将如何使用其中一些方法,并且您确实有 4 个听众在听同一个按钮 R.id.button01。这是您在我的 Eclipse 中运行的代码:

【讨论】:

  • 对不起,但我不明白,正如我所问的,我应该按原样保留它还是替换为“button01”。这个“自动编译”设置在哪里,我找不到,谢谢。
  • 您几乎可以为您的按钮 id 名称命名任何您想要的名称...只需确保更改您的 layout.xml 代码中的 id="+id/yourname"。然后更改对它的任何引用。
  • 找到设置了,已经是这样了。在你之前的帖子里看到了。谢谢
  • 好的,首先,我在 main.xml 中添加了“android:id="@+id/main”,这应该可以与 java 文件的连接,请您检查一下,我也更新了上面的代码,xml和java。谢谢
  • 好的,我再次重新检查了所有按钮的 id 并检查了 R.java 中的添加,这似乎没问题。但是当我再次单击 button01 时,它就像按钮已死。如果我切换回这个“按钮 createEventButton = (Button)findViewById(R.id.button01);”到“按钮 createEventButton = (Button)findViewById(R.id.NewEventButton);”然后按钮再次正常工作。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多