2012-06-07 62 views
1

大家好,感谢您事先的任何答案。故障投诉次活动

的具有1M的问题是,我所谓的“Calander.class”第二个活动是不是从我的主要活动被称为所谓的“ShiftSelection”。林新编程和Android一般,我只是想学习的基本知识,但这个问题真的难倒我。 我已经加入了活动的清单文件,并觉得我正确地调用意图,但运行我的按钮的应用程序的onClick应该调用第二个活动不会做任何事情的时候。

我在做什么错?非常抱歉,如果这是一个简单的答案,但我向你保证,我已经尝试过许多个小时尝试不同的事情并失败了。

我的第一个活动,清单文件的代码如下。

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 

public class ShiftSelection extends Activity{ 

    Button openButton; 
    RadioGroup shiftSelection; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.shiftselection);  

    shiftSelection = new RadioGroup(this); 

    RadioButton shiftPattern1 = new RadioButton(this); //shiftPattern1 = 4 shift 
    shiftPattern1.setText("4 shift"); 
    shiftPattern1.setChecked(true); 
    shiftPattern1.setId(01); 

    RadioButton shiftPattern2 = new RadioButton(this); //shiftPattern2 = 6  shift   
    shiftPattern2.setText("4 shift"); 
    shiftPattern2.setId(02);   

    shiftSelection.addView(shiftPattern1); 
    shiftSelection.addView(shiftPattern2);   

    Button openButton = new Button(this); 
    openButton.setText("open"); 
    openButton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(getApplicationContext(), Calander.class); 
       int genderID = shiftSelection.getCheckedRadioButtonId(); 

      switch(genderID){ 

      case 01: 
      intent.putExtra("shiftPattern", "1"); 
      break; 

      case 02: 
      intent.putExtra("shiftPattern", "2"); 
      break; 
      } 
      startActivity(intent); 
      } 

      }); 

    } 

} 

而且清单文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.examples" 
    android:versionCode="1" 
    android:versionName="1.0"> 
<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <activity android:name=".ShiftSelection" 
       android:label="@string/app_name"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
     </activity> 
     <activity android:name=".Calander"> 
     </activity>    
</application> 
</manifest> 

三江源的任何帮助。

回答

1

错误收到您的按钮。 通过这样做,您可以将onClick侦听器分配给永远不可见的按钮。

而是通过做

Button openButton = (Button)findViewById(R.id.openButton); 

Button openButton = new Button(this); 

你应该试图找到你的现有布局的按钮,你在你的R.layout.shiftselection布局文件

分配您的按钮取ID替换 R.id.openButton

你正在做的,现在要创建一个新的按钮,然后永远不会将其连接到一个布局,因此无线网络的方式永远不会显示

+0

嗨dymmeh,非常感谢您的时间和答案是现货!我改变了我的代码,并解决了这个问题,我很高兴这是一个简单的修复。我已经接受你的回答并标上了勾号,不确定是否还有其他事情可以投票给你? –

3

在你的意图,而不是getApplicationContext(),尝试使用这个

Intent intent = new Intent (this, Calendar.class); 

也许这可以帮助你

+0

嗨,谢谢你看看我选择“Dophie's”答案的问题,但都非常感谢。 –

0

另一个想法,可以帮助:使用您的按钮查看onclick属性。您可以在应用程序范围内将其设置为名称任意方法,但在与该视图关联的活动中使用方法最为常见。该方法的签名是

公共无效方法名(视图V)

+0

嗨,感谢您看看我选择“Dophie's”答案的问题,但都非常感谢。 –

0

尝试你在活动需要2清单


Intent intent = new Intent(ShiftSelection.this, Calander.class); 
+0

嗨,谢谢你看看。但接受了Dophh的答案。 –

0

我有三个变化和1在ShiftSelection中替换此代码。的java活动

Button openButton = new Button(this); 

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

和改变这一行(相同的活性的onClick方法)

Intent intent = new Intent(getApplicationContext(), Calander.class); 

Intent intent = new Intent(ShiftSelection.this, Calander.class); 

那么你shiftselection.xml应该有这样的

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <Button android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button Open"></Button> 
</LinearLayout> 

最后你需要输入两个活动AndroidManifest.xml中文件中像这样最重要的一个按钮。

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.examples" 
     android:versionCode="1" 
     android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="8" /> 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 

     <activity android:name=".ShiftSelection" 
        android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <activity android:name=".Calander" 
        android:label="@string/app_name"> 
      <intent-filter>     
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

    </application> 
</manifest> 

让我知道如果你仍然有问题要做到这一点。

+0

嗨RDC,非常感谢你的努力,我会看看我的清单文件,因为你的例子是再次包括“意图过滤器”的第二个活动,因为我的缺乏,但一分钟它的工作正常感谢“Dophie”。 –

+0

....只是想知道,但是有没有任何理由为什么它会工作得很好atm没有那些额外的过滤器,那些额外的过滤器是否良好的编码实践?我会投票给你,但仍然是一个新手抱歉。 –

+0

@ user1442782 ..没有问题的伴侣,在某些日子之前,我在你的地方:),顺便说一句[这个](http://developer.android.com/guide/topics/intents/intents-filters.html)和[this ](http://www.vogella.com/articles/AndroidIntent/article.html)链接肯定清除你对intents和意图过滤器的怀疑。 – swiftBoy