【问题标题】:Tab menu to start an activity using tabactivity and tab group on api16选项卡菜单使用 api16 上的选项卡活动和选项卡组启动活动
【发布时间】:2012-11-14 01:15:14
【问题描述】:

我正在创建一个用于读取和写入 nfc 标签的应用程序,为此我创建了一个带有 4 个选项卡的选项卡菜单。 这个应用程序有一个自定义标题栏和背景图像。
我试图在 api 16 中运行这个应用程序。
当按下 tab3 时,一切都会正确显示,我的自定义标题栏、我的背景和下面的 4 个选项卡。在此选项卡中,我有一个文本框和一个按钮,单击按钮时,文本框中的文本应传递给 nfc 标签,如果不是,则应读取标签数据。
发生的情况是,当我单击按钮而不是写入标签时,它正在读取,当它读取标签时,启动的活动是标签活动,但布局错误,它只出现定义的活动标题在清单中,黑色背景以及文本框和按钮。然后,如果我单击按钮写入标签,它会起作用,并且如果未单击按钮,则会读取标签。
现在我创建了第四个选项卡,里面有一个按钮,如果单击该按钮,它会启动一个新的活动,这个标签活动效果很好。 当这个类在 api13 之后被弃用时,问题是否与在 api16 中使用 tagsactivity 有关? 或者是我缺少的另一件事。
这是tabs2

 import android.app.LocalActivityManager;
   import android.content.Intent;
 import android.os.Bundle;
 import android.view.KeyEvent;
 import android.view.View;
 import android.view.View.OnKeyListener;
 import android.view.Window;
 import android.view.View.OnClickListener;
 import android.widget.Button;
 import android.widget.TabHost;

 public class Tabs2 extends android.app.TabActivity{
public TabHost tabHost;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.activity_tabs);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
        // Get the tabHost
    this.tabHost = getTabHost();


    Button btentrar= (Button) findViewById(R.id.titlebarRefreshBtn);

       btentrar.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                setResult(2);
                finish();
            }
        }); 

    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch the first Activity for the tab (to be reused)
    intent = new Intent().setClass(this, FirstGroup.class);

    // Initialize a TabSpec for the first tab and add it to the TabHost
    spec = tabHost.newTabSpec("Opcções").setIndicator("FirstGroup",
            getResources().getDrawable(R.drawable.settingsicon)) // Replace null with R.drawable.your_icon to set tab icon
                    .setContent(intent);
    tabHost.addTab(spec);

        // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, SecondActivityGroup.class);

    // Initialize a TabSpec for the second tab and add it to the TabHost
    spec = tabHost.newTabSpec("Tags passadas").setIndicator("Tags Passadas",
            getResources().getDrawable(R.drawable.writedocumenticon)) // Replace null with R.drawable.your_icon to set tab icon
                    .setContent(intent);
    tabHost.addTab(spec);

     // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, ThirdActivityGroup.class);

        // Initialize a TabSpec for the second tab and add it to the TabHost
        spec = tabHost.newTabSpec("Read and write").setIndicator("Ler e escrever",
                getResources().getDrawable(R.drawable.checkiconm)) // Replace null with R.drawable.your_icon to set tab icon
                        .setContent(intent);
        tabHost.addTab(spec);


        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, FourthActivityGroup.class);

        // Initialize a TabSpec for the second tab and add it to the TabHost
        spec = tabHost.newTabSpec("Read and write v2").setIndicator("Ler e escrever v2",
                getResources().getDrawable(R.drawable.checkiconm)) // Replace null with R.drawable.your_icon to set tab icon
                        .setContent(intent);
        tabHost.addTab(spec);

    tabHost.setCurrentTab(0);
}
}

这里是扩展活动组的第三个活动组

import java.util.ArrayList;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class ThirdActivityGroup extends ActivityGroup {

    // Keep this in a static variable to make it accessible for all the nested activities, lets them manipulate the view
public static ThirdActivityGroup group;

    // Need to keep track of the history if you want the back-button to work properly, don't use this if your activities requires a lot of memory.
private ArrayList<View> history;

@Override
protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      this.history = new ArrayList<View>();
      group = this;

          // Start the root activity withing the group and get its view
      View view = getLocalActivityManager().startActivity("TagsActivity", new
                                         Intent(this,TagsActivity.class)
                                          .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                                        .getDecorView();

          // Replace the view of this ActivityGroup
      replaceView(view);

   }

public void replaceView(View v) {
            // Adds the old one to history
    history.add(v);
            // Changes this Groups View to the new View.
    setContentView(v);
}

public void back() {
    if(history.size() > 0) {
        history.remove(history.size()-1);
        setContentView(history.get(history.size()-1));
    }else {
        finish();
    }
}

public void onBackPressed() {
    ThirdActivityGroup.group.back();
    return;
}

}

这里是第四个活动组

import java.util.ArrayList;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
 import android.view.View;

public class FourthActivityGroup extends ActivityGroup {

    // Keep this in a static variable to make it accessible for all the nested activities, lets them manipulate the view
public static FourthActivityGroup group;

    // Need to keep track of the history if you want the back-button to work properly, don't use this if your activities requires a lot of memory.
private ArrayList<View> history;

@Override
protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      this.history = new ArrayList<View>();
      group = this;

          // Start the root activity withing the group and get its view
      View view = getLocalActivityManager().startActivity("Teste", new
                                        Intent(this,Teste.class)
                                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                                        .getDecorView();

          // Replace the view of this ActivityGroup
      replaceView(view);

   }

public void replaceView(View v) {
            // Adds the old one to history
    history.add(v);
            // Changes this Groups View to the new View.
    setContentView(v);
}

public void back() {
    if(history.size() > 0) {
        history.remove(history.size()-1);
        setContentView(history.get(history.size()-1));
    }else {
        finish();
    }
}

public void onBackPressed() {
    FourthActivityGroup.group.back();
    return;
}

}

这里是睾丸

import java.util.ArrayList;

import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;

public class Teste extends Activity {

    // Keep this in a static variable to make it accessible for all the nested activities, lets them manipulate the view

@Override
protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_teste);

        Button btnfc= (Button) findViewById(R.id.button1);

           btnfc.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    startActivity(new Intent(Teste.this, TagsActivity.class));  
                }
            }); 

   }

}

这里是标签活动

public class TagsActivity extends Activity {




private NfcAdapter mNfcAdapter;

private Button mEnableWriteButton;
private EditText mTextField;
private ProgressBar mProgressBar;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tags);
     getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
    mTextField = (EditText) findViewById(R.id.text_field);

    mProgressBar = (ProgressBar) findViewById(R.id.progress_bar);
    mProgressBar.setVisibility(View.GONE);

    mEnableWriteButton = (Button) findViewById(R.id.enable_write_button);
    mEnableWriteButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            setTagWriteReady(!isWriteReady);
            mProgressBar.setVisibility(isWriteReady ? View.VISIBLE : View.GONE);
        }
    });

    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (mNfcAdapter == null) {
        Toast.makeText(this, "Sorry, NFC is not available on this device", Toast.LENGTH_SHORT).show();
        finish();
    }


}

private boolean isWriteReady = false;

/**
 * Enable this activity to write to a tag
 * 
 * @param isWriteReady
 */
public void setTagWriteReady(boolean isWriteReady) {
    this.isWriteReady = isWriteReady;
    if (isWriteReady) {
        IntentFilter[] writeTagFilters = new IntentFilter[] { new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED) };
        mNfcAdapter.enableForegroundDispatch(TagsActivity.this, NfcUtils.getPendingIntent(TagsActivity.this),
                writeTagFilters, null);
    } else {
        // Disable dispatch if not writing tags
        mNfcAdapter.disableForegroundDispatch(TagsActivity.this);
    }
    mProgressBar.setVisibility(isWriteReady ? View.VISIBLE : View.GONE);
}

@Override
public void onNewIntent(Intent intent) {
    // onResume gets called after this to handle the intent
    setIntent(intent);
}

@Override
public void onResume() {
    super.onResume();
    if (isWriteReady && NfcAdapter.ACTION_TAG_DISCOVERED.equals(getIntent().getAction())) {
        processWriteIntent(getIntent());
    } else if (!isWriteReady
            && (NfcAdapter.ACTION_TAG_DISCOVERED.equals(getIntent().getAction()) || NfcAdapter.ACTION_NDEF_DISCOVERED
                    .equals(getIntent().getAction()))) {
        processReadIntent(getIntent());
    }
}

private static final String MIME_TYPE = "application/com.smartcom.onetagv4";

/**
 * Write to an NFC tag; reacting to an intent generated from foreground
 * dispatch requesting a write
 * 
 * @param intent
 */
public void processWriteIntent(Intent intent) {
    if (isWriteReady && NfcAdapter.ACTION_TAG_DISCOVERED.equals(getIntent().getAction())) {

        Tag detectedTag = getIntent().getParcelableExtra(NfcAdapter.EXTRA_TAG);

        String tagWriteMessage = mTextField.getText().toString();
        byte[] payload = new String(tagWriteMessage).getBytes();

        if (detectedTag != null && NfcUtils.writeTag(
                NfcUtils.createMessage(MIME_TYPE, payload), detectedTag)) {

            Toast.makeText(this, "Wrote '" + tagWriteMessage + "' to a tag!", 
                    Toast.LENGTH_LONG).show();
            setTagWriteReady(false);
        } else {
            Toast.makeText(this, "Write failed. Please try again.", Toast.LENGTH_LONG).show();
        }
    }
}

public void processReadIntent(Intent intent) {
    List<NdefMessage> intentMessages = NfcUtils.getMessagesFromIntent(intent);
    List<String> payloadStrings = new ArrayList<String>(intentMessages.size());

    for (NdefMessage message : intentMessages) {
        for (NdefRecord record : message.getRecords()) {
            byte[] payload = record.getPayload();
            String payloadString = new String(payload);

            if (!TextUtils.isEmpty(payloadString))
                payloadStrings.add(payloadString);
        }
    }

    if (!payloadStrings.isEmpty()) {
        String content =  TextUtils.join(",", payloadStrings);
        Toast.makeText(TagsActivity.this, "Read from tag: " + content,
                Toast.LENGTH_LONG).show();

    }
}



}

【问题讨论】:

  • 请清理您的问题,有很多不必要的信息。你为什么认为它失败了,你能把你的日志副本粘贴到你编码失败的地方吗?
  • @ile 我很抱歉,但我不能在 Eclipse 上运行这个 nfc 应用程序,因为它告诉我这个设备不支持 nfc。它可以工作但不是我想要的方式。
  • “在 Eclipse 上运行应用程序”是指不能在 Android 虚拟设备上运行它吗?我很困惑,您能否更清楚地解释您遇到的确切问题是什么?
  • @ile 我可以运行该应用程序,但如果按下 tab3,则会显示一条消息“抱歉,NFC 在此设备上不可用”,所以我将应用程序传递给运行 4.0 版的 android 手机.我可以在选项卡 1,2 和 4.Om 之间切换,再次出现相同的选项卡,如果我单击按钮,它会显示相同的消息。我重做了这个问题。是与 api16 上不推荐使用的 tabactivity 相关的问题。
  • 对不起,显然原因不在nfc中,我想不出解决办法,希望其他人能帮忙

标签: android nfc activitygroup


【解决方案1】:

使用linked 示例重新构建您的应用程序。一个建议:您在使用选项卡时遇到问题,因此作为第一个任务构建用户界面。当它正常工作时,添加您的 NFC 代码。

【讨论】:

  • 嗨,谢谢你的帮助,让我们试试这种方式,你有一个按钮,那个 onclick startactivity 苹果,当这个活动被午餐时,在我的情况下,它只出现文本,而不是标签,即布局应该出现选项卡和文本,但不是。
猜你喜欢
  • 2012-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多