2016-01-24 24 views
-4

我正在嘗試爲大學項目創建一個簡單的應用程序,它變得令人沮喪。每當我糾正一個錯誤6更多彈出。有人可以幫幫我嗎。在android studio中調用一個新的活動

這個盒子是要求更多的信息.​​..所以基本上我想創建4活動,主要,age4to10,age11to15,age15to18。我需要能夠點擊一個按鈕並從Main.class中調用相對的活動。但每次我調試時,我得到的錯誤,我根本不明白如何解決。

每項活動都將託管一個微調器,其中包含用戶選擇的玩具選擇。 (不要讓我開始在spinners)

我會感謝用戶不編輯我的帖子,因爲我認爲這裏的所有信息與我所遇到的問題有關。除此之外......我的Java培訓很少,所以我需要更正您可能需要提供的示例。

謝謝。

package com.example.android.skillsdemoVT; 

import android.content.Intent; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 

    Button button1; 
    Button button2; 
    Button button3; 

    @Override 

    /* button1 */ 

    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     View viewById; 
     viewById = findViewById(R.id.button1); 
     button1.setOnClickListener(this); 
    } 
    @Override 
    public void onClick1(View v) 
    { 
     { 
       button1click1(); 
     } 
    } 
    public void button1click1() 
    { 
     startActivity(new Intent(MainActivity.this, age4to10.class)); 
    } 

    /* Button2 */ 

    public void onClick2(View v) 
    { 
     setContentView(R.layout.activity_main); 
     View viewById; 
     viewById = findViewById(R.id.button2); 
     button2.setOnClickListener(this); 
    } 

    public void button2click1() 
    { 
     { 
      button2click1(); 

     } 

    } 


    public void() 
    { 
     { 
      startActivity(new Intent(MainActivity.this, age11to15.class)); 
     } 
    } 

    /* button3 */ 

    public void onClick3(View v) 
    { 
     setContentView(R.layout.activity_main); 
     View viewById; 
     viewById = findViewById(R.id.button3); 
     button3.setOnClickListener(this); 
    } 
    @Override 
    public void onClick(View v) { 
     { 
      button3click1(); 
     } 

    } 

    public void button3click1() 
    { 
     startActivity(new Intent(MainActivity.this, age15to18.class)); 
    } 

} 



MY XML 

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> 

    <ImageView 
     android:id="@+id/tree1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@drawable/tree1"/> 

    <TextView 
     android:id="@+id/welcome" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="85dp" android:text="Welcome to Santa's workshop." 
     android:textColor="#ffffff" 
     android:textSize="25sp"/> 

    <TextView 
     android:id="@+id/instructions" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Please let Santa know how old you are!" 
     android:textColor="#ffffff" 
     android:textSize="25sp" 
     android:layout_marginTop="43dp" 
     android:layout_alignParentTop="true" 
     android:layout_alignRight="@+id/welcome" 
     android:layout_alignEnd="@+id/welcome" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="120dp" 
     android:layout_height="120dp" 
     android:background="#b12b2b" 
     android:textColor="#ffffff" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_centerVertical="true" 
     android:text="Age 4-10" 
     android:textSize="30sp" 
     android:onClick="age4to10"/> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="120dp" 
     android:layout_height="120dp" 
     android:background="#b12b2b" 
     android:textColor="#ffffff" 
     android:layout_centerVertical="true" 
     android:layout_toRightOf="@+id/button1" 
     android:text="Age 11-14" 
     android:textSize="30sp" 
     android:onClick="age11to15"/> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="120dp" 
     android:layout_height="120dp" 
     android:background="#b12b2b" 
     android:textColor="#ffffff" 
     android:layout_centerVertical="true" 
     android:layout_toRightOf="@+id/button2" 
     android:text="Age 15-17" 
     android:textSize="30sp" 
     android:onClick="age15to18" 

     /> 

    /> 

</RelativeLayout> 
+2

人們downvoting,因爲你的問題不遵循這裏設立的準則:http://stackoverflow.com/help/how-to-ask –

+0

這是確定。 。我沒有意識到有問題的規則..... –

+0

只是有點建議,在你提出一個關於你的代碼不工作的問題之前,試着將它縮小到最小的形式,錯誤仍然存​​在。這經常會將問題本身排除在外。 –

回答

0

嘗試:

Intent intent = new Intent(storedActivity, MainActivity.class); 
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
storedActivity.startActivity(intent); 
this.finish(); 
相關問題