2013-10-30 28 views
0
  • 將呈現一個預定義的問題
  • ,同時讓問題在屏幕上顯示的按鈕,這將導致該問題的答案的區域
  • 一個區域將提供答案
  • 一個按鈕,將導致過渡到與此格式相同的屏幕,並顯示下一個問題
  • 將導致應用程序結束的按鈕(a過渡到(3))

只試圖從屏幕1到屏幕2的轉換,在按下鼠標左鍵的同時使用不同的問題/答案對。如果無論如何要做到這一點,而不是切換錯誤的屏幕和活動,請讓我知道。不幸的是(項目名稱),已停止按鈕工作單擊

package com.example.androidassignment2; 

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

public class AndroidAssignment2 extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_android_assignment2_1); 

     Button next = (Button) findViewById(R.id.QButton); 
     next.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Intent intent = new Intent(); 
       setResult(RESULT_OK, intent); 
       finish(); 
      } 
     }); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.android_assignment2_1, menu); 
     return true; 
    } 

} 

佈局文件

<LinearLayout 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:orientation="vertical" > 

<TextView android:id="@+id/Questions" 
    android:layout_weight="1" 
    android:layout_width="wrap_content" 
    android:layout_height="0dip" 
    android:text="@string/Q2" /> 

<Button android:id="@+id/QButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/button_question" /> 

<Button android:id="@+id/AButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/button_send" /> 

<TextView android:id="@+id/Answers" 
    android:layout_weight="1" 
    android:layout_width="wrap_content" 
    android:layout_height="0dip" 
    android:hint="@string/A2" /> 

<Button android:id="@+id/QuitButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/button_quit" /> 


</LinearLayout> 

活動1文件(櫃面需要)

package com.example.androidassignment2; 

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

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


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

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 
} 
+3

請隨時張貼logcat的堆棧跟蹤。如果你不知道如何,問。 – Simon

回答

0
Intent myIntent = new Intent(view.getContext(), AndroidAssignment2_1.class); 
startActivityForResult(myIntent, 0); 

您正在開始一個新的活動,最可能的原因是,這是沒有已在清單中聲明。

在你AndroidManifest.xml`,你將需要添加這樣的事情:

<activity 
    android:name="your.package.name.AndroidAssignment2_1" /> 

這將駐留在<application>標籤內。

+1

我在包名的第一個單詞前面有一個點。 >:| – Learning2Code

0

您正在使用startActivityForResult(),但在第一個Activity中未覆蓋onActivityForResult()方法。您可以將其更改爲僅使用startActivity()並從您的第二個Activity中刪除setResult()

Button next = (Button) findViewById(R.id.QButton); 
    next.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      Intent myIntent = new Intent(view.getContext(), AndroidAssignment2_1.class); 
      startActivity(myIntent); 
     } 
    }); 

如果這不是你的問題,那麼請張貼的logcat(總是當你有一個崩潰),並確保Activity被decalred。

至於你的其他查詢

如果反正做的比切換屏幕和活動,是這種錯誤等,請讓我知道。

如果你只是改變了文本,然後你可以留在同一個Activity,改變你TextView S的文本。您可以將問題和答案存儲在Array或類似的數據庫中,如果您願意,可以將其保存在您所在的位置,然後每次點擊更改文本。

另一種選擇,它可以與如果你想第一個走,是使用ViewPager

相關問題