2017-02-28 79 views
-1

我正在編寫一個使用包在活動之間傳遞數據的android應用程序。我做了一個Answers.java類來從包中獲取數據。當我嘗試獲取數據時,我收到錯誤非法自助。我得到非法自我參考

Answers a = a.getQuestion1_1(); 

在這裏的錯誤是我的活動代碼

package www.iup.edu.iupcrimsurvey; 

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

    public class Thank_You_MainActivity extends AppCompatActivity { 

Answers a = a.getQuestion1_1(); 

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

    // Sets an onClickListener to the button and calls the launchNextScreenActivity() method 
    final Button btn = (Button) findViewById(R.id.Next_Screen15); 
    btn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      launchNextScreenActivity(); 
     } 
    }); 

    // Sets an onClickListener to the button and calls the launchNextScreenActivity() method 
    final Button btn2 = (Button) findViewById(R.id.Close); 
    btn.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      launchFinishActivity(); 
     } 
    }); 
} 

// Switches to the StartScreen_Activity screen 
private void launchNextScreenActivity() { 
    Intent intent = new Intent(this, StartScreen_MainActivity.class); 
    startActivity(intent); 
} 

// Closes the app 
private void launchFinishActivity() { 

} 

}

我Answers.java文件

 package www.iup.edu.iupcrimsurvey; 

    public class Answers { 
     private int question1_1 = StartScreen_MainActivity.MyAppsBundle.getInt("Demographics_question1"); 
     private String question1_2 = StartScreen_MainActivity.MyAppsBundle.getString("Demographics_question2"); 
     private String question1_3 = StartScreen_MainActivity.MyAppsBundle.getString("Demographics_question3"); 
     private String question1_4 = StartScreen_MainActivity.MyAppsBundle.getString("Demographics_question4"); 
     private String question1_5 = StartScreen_MainActivity.MyAppsBundle.getString("Demographics_question5"); 
     private String question1_6 = StartScreen_MainActivity.MyAppsBundle.getString("Demographics_question6"); 
     private String question1_7= StartScreen_MainActivity.MyAppsBundle.getString("Demographics_question7"); 

private String question2_1 = StartScreen_MainActivity.MyAppsBundle.getString("housing_question1"); 
private int question2_2 = StartScreen_MainActivity.MyAppsBundle.getInt("housing_question2"); 
private String question2_3 = StartScreen_MainActivity.MyAppsBundle.getString("housing_question3"); 
private String question2_4 = StartScreen_MainActivity.MyAppsBundle.getString("housing_question4"); 

private String question3_1 = StartScreen_MainActivity.MyAppsBundle.getString("Sexuality_question1"); 
private String question3_2 = StartScreen_MainActivity.MyAppsBundle.getString("Sexuality_question2"); 
private String question3_3 = StartScreen_MainActivity.MyAppsBundle.getString("Sexuality_question3"); 
private String question3_4 = StartScreen_MainActivity.MyAppsBundle.getString("Sexuality_question4"); 
private String question3_5 = StartScreen_MainActivity.MyAppsBundle.getString("Sexuality_question5"); 

private String question4_1 = StartScreen_MainActivity.MyAppsBundle.getString("Alcohol_question1"); 
private String question4_2 = StartScreen_MainActivity.MyAppsBundle.getString("Alcohol_question2"); 
private String question4_3 = StartScreen_MainActivity.MyAppsBundle.getString("Alcohol_question3"); 
private String question4_4 = StartScreen_MainActivity.MyAppsBundle.getString("Alcohol_question4"); 
private String question4_5 = StartScreen_MainActivity.MyAppsBundle.getString("Alcohol_question5"); 

private String question5_1 = StartScreen_MainActivity.MyAppsBundle.getString("Travel_question1"); 
private String question5_2 = StartScreen_MainActivity.MyAppsBundle.getString("Travel_question2"); 
private String question5_3 = StartScreen_MainActivity.MyAppsBundle.getString("Travel_question3"); 
private String question5_4 = StartScreen_MainActivity.MyAppsBundle.getString("Travel_question4"); 
private String question5_5 = StartScreen_MainActivity.MyAppsBundle.getString("Travel_question5"); 

private String question6_1 = StartScreen_MainActivity.MyAppsBundle.getString("Work_question1"); 
private String question6_2 = StartScreen_MainActivity.MyAppsBundle.getString("Work_question2"); 
private String question6_3 = StartScreen_MainActivity.MyAppsBundle.getString("Work_question3"); 
private String question6_4 = StartScreen_MainActivity.MyAppsBundle.getString("Work_question4"); 
private String question6_5 = StartScreen_MainActivity.MyAppsBundle.getString("Work_question5"); 



private String question7_1 = StartScreen_MainActivity.MyAppsBundle.getString("Crime_question1"); 

private String question8_1 = StartScreen_MainActivity.MyAppsBundle.getString("Crime_Committed_question1"); 
private String question8_2 = StartScreen_MainActivity.MyAppsBundle.getString("Crime_Committed_question2"); 
private String question10_1 = StartScreen_MainActivity.MyAppsBundle.getString("Incident_question1"); 
private String question10_2 = StartScreen_MainActivity.MyAppsBundle.getString("Incident_question2"); 
private String question10_3 = StartScreen_MainActivity.MyAppsBundle.getString("Incident_question3"); 
private String question10_4 = StartScreen_MainActivity.MyAppsBundle.getString("Incident_question4"); 
private String question10_5 = StartScreen_MainActivity.MyAppsBundle.getString("Incident_question5"); 
private String question10_6 = StartScreen_MainActivity.MyAppsBundle.getString("Incident_question6"); 
private String question10_7 = StartScreen_MainActivity.MyAppsBundle.getString("Incident_question7"); 
private String question10_8 = StartScreen_MainActivity.MyAppsBundle.getString("Incident_question8"); 
private String question10_9 = StartScreen_MainActivity.MyAppsBundle.getString("Incident_question9"); 
private String question10_10 = StartScreen_MainActivity.MyAppsBundle.getString("Incident_question10"); 

public int getQuestion1_1() { 
    return question1_1; 
} 

public void setQuestion1_1(int question1_1) { 
    this.question1_1 = question1_1; 
} 

public String getQuestion1_2() { 
    return question1_2; 
} 

public void setQuestion1_2(String question1_2) { 
    this.question1_2 = question1_2; 
} 

public String getQuestion1_3() { 
    return question1_3; 
} 

public void setQuestion1_3(String question1_3) { 
    this.question1_3 = question1_3; 
} 

public String getQuestion1_4() { 
    return question1_4; 
} 

public void setQuestion1_4(String question1_4) { 
    this.question1_4 = question1_4; 
} 

public String getQuestion1_5() { 
    return question1_5; 
} 

public void setQuestion1_5(String question1_5) { 
    this.question1_5 = question1_5; 
} 

public String getQuestion1_6() { 
    return question1_6; 
} 

public void setQuestion1_6(String question1_6) { 
    this.question1_6 = question1_6; 
} 

public String getQuestion1_7() { 
    return question1_7; 
} 

public void setQuestion1_7(String question1_7) { 
    this.question1_7 = question1_7; 
} 

public String getQuestion2_1() { 
    return question2_1; 
} 

public void setQuestion2_1(String question2_1) { 
    this.question2_1 = question2_1; 
} 

public int getQuestion2_2() { 
    return question2_2; 
} 

public void setQuestion2_2(int question2_2) { 
    this.question2_2 = question2_2; 
} 

public String getQuestion2_3() { 
    return question2_3; 
} 

public void setQuestion2_3(String question2_3) { 
    this.question2_3 = question2_3; 
} 

public String getQuestion2_4() { 
    return question2_4; 
} 

public void setQuestion2_4(String question2_4) { 
    this.question2_4 = question2_4; 
} 

public String getQuestion3_1() { 
    return question3_1; 
} 

public void setQuestion3_1(String question3_1) { 
    this.question3_1 = question3_1; 
} 

public String getQuestion3_2() { 
    return question3_2; 
} 

public void setQuestion3_2(String question3_2) { 
    this.question3_2 = question3_2; 
} 

public String getQuestion3_3() { 
    return question3_3; 
} 

public void setQuestion3_3(String question3_3) { 
    this.question3_3 = question3_3; 
} 

public String getQuestion3_4() { 
    return question3_4; 
} 

public void setQuestion3_4(String question3_4) { 
    this.question3_4 = question3_4; 
} 

public String getQuestion3_5() { 
    return question3_5; 
} 

public void setQuestion3_5(String question3_5) { 
    this.question3_5 = question3_5; 
} 

public String getQuestion4_1() { 
    return question4_1; 
} 

public void setQuestion4_1(String question4_1) { 
    this.question4_1 = question4_1; 
} 

public String getQuestion4_2() { 
    return question4_2; 
} 

public void setQuestion4_2(String question4_2) { 
    this.question4_2 = question4_2; 
} 

public String getQuestion4_3() { 
    return question4_3; 
} 

public void setQuestion4_3(String question4_3) { 
    this.question4_3 = question4_3; 
} 

public String getQuestion4_4() { 
    return question4_4; 
} 

public void setQuestion4_4(String question4_4) { 
    this.question4_4 = question4_4; 
} 

public String getQuestion4_5() { 
    return question4_5; 
} 

public void setQuestion4_5(String question4_5) { 
    this.question4_5 = question4_5; 
} 

public String getQuestion5_1() { 
    return question5_1; 
} 

public void setQuestion5_1(String question5_1) { 
    this.question5_1 = question5_1; 
} 

public String getQuestion5_2() { 
    return question5_2; 
} 

public void setQuestion5_2(String question5_2) { 
    this.question5_2 = question5_2; 
} 

public String getQuestion5_3() { 
    return question5_3; 
} 

public void setQuestion5_3(String question5_3) { 
    this.question5_3 = question5_3; 
} 

public String getQuestion5_4() { 
    return question5_4; 
} 

public void setQuestion5_4(String question5_4) { 
    this.question5_4 = question5_4; 
} 

public String getQuestion5_5() { 
    return question5_5; 
} 

public void setQuestion5_5(String question5_5) { 
    this.question5_5 = question5_5; 
} 

public String getQuestion6_1() { 
    return question6_1; 
} 

public void setQuestion6_1(String question6_1) { 
    this.question6_1 = question6_1; 
} 

public String getQuestion6_2() { 
    return question6_2; 
} 

public void setQuestion6_2(String question6_2) { 
    this.question6_2 = question6_2; 
} 

public String getQuestion6_3() { 
    return question6_3; 
} 

public void setQuestion6_3(String question6_3) { 
    this.question6_3 = question6_3; 
} 

public String getQuestion6_4() { 
    return question6_4; 
} 

public void setQuestion6_4(String question6_4) { 
    this.question6_4 = question6_4; 
} 

public String getQuestion6_5() { 
    return question6_5; 
} 

public void setQuestion6_5(String question6_5) { 
    this.question6_5 = question6_5; 
} 

public String getQuestion7_1() { 
    return question7_1; 
} 

public void setQuestion7_1(String question7_1) { 
    this.question7_1 = question7_1; 
} 

public String getQuestion8_1() { 
    return question8_1; 
} 

public void setQuestion8_1(String question8_1) { 
    this.question8_1 = question8_1; 
} 

public String getQuestion8_2() { 
    return question8_2; 
} 

public void setQuestion8_2(String question8_2) { 
    this.question8_2 = question8_2; 
} 

public String getQuestion10_1() { 
    return question10_1; 
} 

public void setQuestion10_1(String question10_1) { 
    this.question10_1 = question10_1; 
} 

public String getQuestion10_2() { 
    return question10_2; 
} 

public void setQuestion10_2(String question10_2) { 
    this.question10_2 = question10_2; 
} 

public String getQuestion10_3() { 
    return question10_3; 
} 

public void setQuestion10_3(String question10_3) { 
    this.question10_3 = question10_3; 
} 

public String getQuestion10_4() { 
    return question10_4; 
} 

public void setQuestion10_4(String question10_4) { 
    this.question10_4 = question10_4; 
} 

public String getQuestion10_5() { 
    return question10_5; 
} 

public void setQuestion10_5(String question10_5) { 
    this.question10_5 = question10_5; 
} 

public String getQuestion10_6() { 
    return question10_6; 
} 

public void setQuestion10_6(String question10_6) { 
    this.question10_6 = question10_6; 
} 

public String getQuestion10_7() { 
    return question10_7; 
} 

public void setQuestion10_7(String question10_7) { 
    this.question10_7 = question10_7; 
} 

public String getQuestion10_8() { 
    return question10_8; 
} 

public void setQuestion10_8(String question10_8) { 
    this.question10_8 = question10_8; 
} 

public String getQuestion10_9() { 
    return question10_9; 
} 

public void setQuestion10_9(String question10_9) { 
    this.question10_9 = question10_9; 
} 

public String getQuestion10_10() { 
    return question10_10; 
} 

public void setQuestion10_10(String question10_10) { 
    this.question10_10 = question10_10; 
} 

public String getQuestion11_1() { 
    return question11_1; 
} 

public void setQuestion11_1(String question11_1) { 
    this.question11_1 = question11_1; 
} 

public String getQuestion11_2() { 
    return question11_2; 
} 

public void setQuestion11_2(String question11_2) { 
    this.question11_2 = question11_2; 
} 

public String getQuestion11_3() { 
    return question11_3; 
} 

public void setQuestion11_3(String question11_3) { 
    this.question11_3 = question11_3; 
} 

public String getQuestion11_4() { 
    return question11_4; 
} 

public void setQuestion11_4(String question11_4) { 
    this.question11_4 = question11_4; 
} 

public String getQuestion11_5() { 
    return question11_5; 
} 

public void setQuestion11_5(String question11_5) { 
    this.question11_5 = question11_5; 
} 

public String getQuestion11_6() { 
    return question11_6; 
} 

public void setQuestion11_6(String question11_6) { 
    this.question11_6 = question11_6; 
} 

public String getQuestion11_7() { 
    return question11_7; 
} 

public void setQuestion11_7(String question11_7) { 
    this.question11_7 = question11_7; 
} 

public String getQuestion11_8() { 
    return question11_8; 
} 

public void setQuestion11_8(String question11_8) { 
    this.question11_8 = question11_8; 
} 

public String getQuestion11_9() { 
    return question11_9; 
} 

public void setQuestion11_9(String question11_9) { 
    this.question11_9 = question11_9; 
} 

public String getQuestion11_10() { 
    return question11_10; 
} 

public void setQuestion11_10(String question11_10) { 
    this.question11_10 = question11_10; 
} 

private String question11_1 = StartScreen_MainActivity.MyAppsBundle.getString("Relationship_question1"); 
private String question11_2 = StartScreen_MainActivity.MyAppsBundle.getString("Relationship_question2"); 
private String question11_3 = StartScreen_MainActivity.MyAppsBundle.getString("Relationship_question3"); 
private String question11_4 = StartScreen_MainActivity.MyAppsBundle.getString("Relationship_question4"); 
private String question11_5 = StartScreen_MainActivity.MyAppsBundle.getString("Relationship_question5"); 
private String question11_6 = StartScreen_MainActivity.MyAppsBundle.getString("Relationship_question6"); 
private String question11_7 = StartScreen_MainActivity.MyAppsBundle.getString("Relationship_question7"); 
private String question11_8 = StartScreen_MainActivity.MyAppsBundle.getString("Relationship_question8"); 
private String question11_9 = StartScreen_MainActivity.MyAppsBundle.getString("Relationship_question9"); 
private String question11_10 = StartScreen_MainActivity.MyAppsBundle.getString("Relationship_question10"); 

}

+0

你需要的地方_instantiate_了''Answers'對象了'。你打算如何創建這個對象? –

回答

1

你的方式試圖打電話是錯誤的。 請遵循此: 創建應答類

> public class Thank_You_MainActivity extends AppCompatActivity { 

    Answer a;//Create an object 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_thank__you__main); 
     a = new Ans();//define it 
     String aa = a.getGetQuestion1_1(); then use it anywhere 

希望的對象,這將幫助你

+0

謝謝你的幫助。 – hotshot146

+0

對不起。我忘了給你信用。 – hotshot146