2015-10-23 69 views
0

我正在嘗試構建一個應用程序,該按鈕被按下時顯示第二個活動。它還根據在第一個活動的EditText(id:ApplesInput)中輸入的文本更改第二個活動(id:baconTextId)中TextView的文本。基本意圖問題?

活動1:Apples.java 活動2:Bacon.java

在XML中兩個按鈕,我把

android:onClick = "Thismethodiscalledonclick" 

所以我並不需要添加的聽衆。

我按照這個tutorial的意圖,但有一個錯誤。 Android Studio顯示沒有錯誤,但是當我按下「ApplesInput」w/id「ApplesInput」按鈕時,我的手機說,「不幸Intent示例已停止。」

enter image description here

按下這個按鈕後,我應該得到的畫面像下面這樣:

enter image description here

第一項活動:Apples.java

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.content.Intent; 
import android.view.*; 
import android.widget.*; 
public class Apples extends AppCompatActivity { 

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

} 
    public void Thismethodiscalledonclick(View v){ 
     Intent i = new Intent(this, Bacon.class); 


     final EditText ApplesInput = (EditText) findViewById(R.id.ApplesInput); //Named var and id same for simplicity 

     String usersmessage = ApplesInput.getText().toString(); //Whatever user types 

     i.putExtra("applesMessageKey", usersmessage); //Parameters == ("What do you want to call this", What piece of information?) 



     startActivity(i); //Starts intent stuff 

    } 



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

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
} 

第二項活動:Bacon.java

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.*; 

public class Bacon extends AppCompatActivity { 


final TextView BaconText = (TextView) findViewById(R.id.BaconTextid); 


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

    Bundle applesData = getIntent().getExtras(); //Call intent and store the extras in applesdata 

    if(applesData == null){ 
     BaconText.setText("Must type something, sending you back..."); 
     Thismethodiscalledonclick(null); //Take them back to first screen 

     return; 
    } 

    String applesDataReceived = applesData.getString("appleMessageKey"); 
    BaconText.setText(applesDataReceived); 
} 

public void Thismethodiscalledonclick(View v){ 
     Intent i = new Intent(this, Apples.class); 
     startActivity(i); 

    } 


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

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
} 

一些logcat的錯誤過濾:

android.provider.Settings$SettingNotFoundException: accessibility_enabled 
     at   android.provider.Settings$Secure.getIntForUser(Settings.java:3163) 
     at android.provider.Settings$Secure.getInt(Settings.java:3148) 
     at com.android.systemui.power.PowerUI$1.onReceive(PowerUI.java:386) 
     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:774) 
     at android.os.Handler.handleCallback(Handler.java:733) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:136) 
     at android.app.ActivityThread.main(ActivityThread.java:5272) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:515) 
     at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:883) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 
     at dalvik.system.NativeStart.main(Native Method) 

隨意問我要了代碼或資源,如果你認爲它是需要在解決問題的過程。

+0

什麼是logcat輸出? – Shmuel

+0

@Shmuel感謝您的回覆,我將用logcat輸出更新我的問題。 :) –

回答

1

試試這個:在的onCreate

private TextView BaconText; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_bacon); 
    BaconText = (TextView) findViewById(R.id.baconText); 

    Bundle applesData = getIntent().getExtras(); //Call intent and store the extras in applesdata 

    if(applesData == null){ 
     BaconText.setText("Must type something, sending you back..."); 
     Thismethodiscalledonclick(null); //Take them back to first screen 

     return; 
    } 

    String applesDataReceived = applesData.getString("applesMessageKey"); 
    BaconText.setText(applesDataReceived); 
} 

public void Thismethodiscalledonclick(View v){ 
    Intent i = new Intent(this, MainActivity.class); 
    startActivity(i); 

} 

調用findViewById()()。 ApplesActivity中的名稱putExtra是「applesMessageKey」,BaconActivity中的名稱getString是「appleMessageKey」。它們不一樣:D

+0

哇!非常感謝!你介意解釋爲什麼我需要將findViewById()放在onCreate()方法中,爲什麼我需要將BaconText聲明爲private而不是final? –

+0

另外,我想讓用戶回到第一個活動,如果他們不在BaconText中輸入任何內容。現在,這是行不通的 - 這可能是這條線的問題:Thismethodiscalledonclick(null); //將它們帶回第一個屏幕。你介意解釋我如何解決這個問題? –

+0

順便說一句:我標記你的答案是最好的答案,我打你的答案。 :) –