2013-04-01 72 views
-3

我試圖從一個活動切換到其他使用意圖,第一個活動正常運行,但第二個活動開始時,應用程序停止。我已經附上了這個代碼。不幸的是您的應用程序已停止

清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.sampleapp" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    ... 

    <application 
     ... 


     <activity 
      android:name="com.example.sampleapp.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="com.example.sampleapp.welwithname" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.WELWITHNAME" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 

     ... 

    </application> 

    </manifest> 

這是默認的活動。我嘗試獲取字符串並將其發送到下一個activity.here是xml和活動。 活動代碼:

public class MainActivity extends Activity { 
    public final static String EXTRA_MESSAGE = "com.example.sampleapp.MESSAGE"; 

    String tempname; 
    Button submit; 
    EditText name; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     submit= (Button)findViewById(R.id.button1); 

     submit.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 

       Intent intent = new Intent(MainActivity.this,Welwithname.class); 
       EditText editText = (EditText) findViewById(R.id.getname); 
       String message = editText.getText().toString(); 
       intent.putExtra(EXTRA_MESSAGE, message); 
       startActivity(intent); 
      } 
     }); 

    } 


} 

次活動:

這個活動會從活動1串並打印value..heres的XML和活動 活動代碼:

public class Welwithname extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.welwithname); 
     Intent intent = getIntent(); 
     String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); 
     // Create the text view 
     TextView textView = (TextView)findViewById(R.id.textView1); 
     textView.setTextSize(40); 
     textView.setText("Welcome" + message + "!!!"); 
     Button sq = (Button)findViewById(R.id.sq); 
     Button exit = (Button)findViewById(R.id.exit); 

     sq.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View V) 
      { 
       Intent main = new Intent(Welwithname.this,MainScreen.class); 
       startActivity(main); 
      } 

     }); 

     // Set the text view as the activity layout 
     setContentView(textView); 
    } 

    ... 

} 

我應該在這段代碼中做些什麼改變才能使它運行?

+8

請編輯您的帖子和Caps Lock鍵的轉變。同時發佈異常的logcat堆棧跟蹤。最後,分離你的XML和代碼。不要只將一切格式化爲一個塊。幫助我們閱讀這個問題,以便我們提供幫助。 – Simon

+0

另外,請確保您的實際問題不在「代碼」標籤中,請添加錯誤日誌並嘗試刪除我們不需要的所有代碼。添加你的代碼應該做的,爲什麼你期望它這樣做 – Nanne

+0

看看這個問題,例如http://stackoverflow.com/questions/15740635/why-getview-not-getting-called – Simon

回答

1
<activity 
     android:name="com.example.sampleapp.welwithname" 

是不是你的班級叫做Welwithname

利用在活動名稱的「W」你AndroidManifest.xml


你都不會很長,如果你當你遇到崩潰不讀控制檯/ logcat的輸出開發商。

如果您閱讀了LogCat,最有可能會說「無法找到活動welwithname,您是否在您的Manifest中聲明瞭它?」這將是你的答案和StackOverflow的將是一個更快樂的地方

http://developer.android.com/tools/help/logcat.html

相關問題