2013-04-17 104 views
0

我看过活动之间切换的每个示例,我总是得到相同的结果。应用程序炸弹。在按钮点击时打开一个新的活动

至于我可以告诉你,如果有填充布局的内容,然后才能切换到另一个布局,必须“链接”到Java文件这反过来将打开的setContentView(java类R.layout.whatever);

当我尝试这样做时,就像我说我的应用程序炸弹一样。我的代码如下: -

从Java类: -

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

    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 

    setContentView(R.layout.activity_main); 

    Button next = (Button) findViewById(R.id.goesnews); 
    next.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      Intent myIntent = new Intent(view.getContext(), ac2.class); 
      startActivityForResult(myIntent, 0); 
     } 

    }); 

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title); 




} 

到Java文件(AC2)

 public class ac2 extends Activity { 

    /** Called when the activity is first created. */ 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main2); 

    }} 

谁能帮助在这里?

+2

什么是错误登录。你有没有声明清单中的活动? – stinepike

+0

使用startActivity(myIntent);,你可以(应该)使用getApplicationContext();而不是view.getContext();还要确保你已经在androidmanifest.xml中声明了你的活动,如

+0

logcat错误日志将有所帮助 – drunkenRabbit

回答

0

试图从该行删除零:

startActivityForResult(myIntent, 0); 

这样并将其更改为只:

startActivity(myIntent); 

和改变这一行:

Intent myIntent = new Intent(view.getContext(), ac2.class); 

为此:

Intent myIntent = new Intent(firstActivityName.this, ac2.class); 

因为你在这里得到按钮的上下文而不是活动。

0

尝试这种方式

添加的onclick功能,以您的按钮xml文件中 这样

<Button 
     android:id="@+id/previous" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="click" 
     android:onClick="move" /> 

,并在乌拉圭回合的java文件 作出这样

public void move(View v) 
{ 
     Intent myIntent = new Intent(yourclass.this, ac2.class); 
     startActivityForResult(myIntent,0); 
} 
+0

我的错误日志如下所示: - –

+0

VSK - 尝试您的方法时,我无法保存并编译应用程序,因为出现错误 - 方法startActivityForResult(Intent,int)在类型活动不适用于参数 (意图) –

+0

对不起,我忘记把0属性。见编辑 – Senthil

0

的功能举动解决方案很简单,并基于VSK(谢谢)的反馈进行了一些调整。

的ImageButton的要求: - 需要

<Button 
    android:id="@+id/previous" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="click" 
    android:onClick="move" /> 

的Java: -

public void move(View v) 
    { 
    Intent myIntent = new Intent(yourclass.this, ac2.class); 
    startActivityForResult(myIntent, 0); 
    } 

VSK - 请注意startActivityForResult的 '0' 属性

感谢所有

相关问题