2015-08-09 55 views
-1

當我按下我的應用程序中的「開始活動」按鈕它將無法正常工作,並被迫停止工作。不幸的應用程序停止工作

這是第一個XML文件(get.xml):

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<TextView 
    android:id="@+id/etget" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:text="Enter Your Gender" /> 

<Button 
    android:id="@+id/bget1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_below="@id/etget" 
    android:text="Start Activity" /> 

<Button 
    android:id="@+id/bget2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/etget" 
    android:layout_toLeftOf="@id/bget1" 
    android:text="Start Activity for Result " /> 

<TextView 
    android:id="@+id/tvget" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/bget2" 
    android:text="TextView" /> 

</RelativeLayout> 

下面是Java文件,Get.java:

package example.katta; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class Get extends Activity implements OnClickListener { 
Button bg1, bg2; 
TextView tv; 
TextView etg; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.get); 
    etg = (TextView) findViewById(R.id.etget); 
    bg1 = (Button) findViewById(R.id.bget1); 
    bg2 = (Button) findViewById(R.id.bget2); 
    tv = (TextView) findViewById(R.id.tvget); 
    bg1.setOnClickListener(this); 
    bg2.setOnClickListener(this); 
} 

@Override 
public void onClick(View arg0) { 
    // TODO Auto-generated method stub 
    switch (arg0.getId()) { 
    case R.id.bget1: 
     String bread = etg.getText().toString(); 
     Bundle basket = new Bundle(); 
     basket.putString("key", bread); 
     Intent a = new Intent(Get.this, Send.class); 
     a.putExtras(basket); 
     startActivity(a); 
     break; 
    case R.id.bget2: 

     break; 
    } 
} 

} 

這裏是第二屆XML文件(send.xml):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/etsend" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 
</TextView> 

<RadioGroup 
    android:id="@+id/rgsend" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <RadioButton 
     android:id="@+id/rb1send" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Male" /> 

    <RadioButton 
     android:id="@+id/rb2send" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Female" /> 
</RadioGroup> 

<TextView 
    android:id="@+id/tvsend" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="TextView" /> 

<Button 
    android:id="@+id/buttonsend" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Submit" /> 

</LinearLayout> 

這裏是Java文件(Send.java):

package example.katta; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.RadioGroup; 
import android.widget.RadioGroup.OnCheckedChangeListener; 
import android.widget.TextView; 

public class Send extends Activity implements OnClickListener, OnCheckedChangeListener { 

Button bts; 
TextView tvs,ets; 
RadioGroup selection; 
String gotbread; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.send); 
    Initialize(); 
    Bundle gotbasket = getIntent().getExtras(); 
    gotbread = gotbasket.getString("key"); 
    tvs.setText(gotbread); 
} 

private void Initialize() { 
    // TODO Auto-generated method stub 
    ets = (TextView) findViewById(R.id.etsend); 
    bts = (Button) findViewById(R.id.buttonsend); 
    tvs = (TextView) findViewById(R.id.tvsend); 
    selection = (RadioGroup) findViewById(R.id.rgsend); 
    bts.setOnClickListener(this); 
    selection.setOnCheckedChangeListener(this); 
} 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 

} 

public void onCheckedChanged(RadioGroup arg0, int arg1) { 
    // TODO Auto-generated method stub 
    switch (arg1) { 
    case R.id.rb1send: 

     break; 
    case R.id.rb2send: 

     break; 
    } 

} 

} 
+0

添加堆棧跟蹤請 –

+0

logcat跟蹤? – Rakesh

+0

是的,裏面有錯誤信息。 –

回答

0

的錯誤是:

android.content.ActivityNotFoundException: Unable to find explicit activity class {example.katta/example.katta.Send}; have you declared this activity in your AndroidManifest.xml? 

的解決方案是增加Send活動在AndroidManifest.xml

<activity 
     android:name=".Send" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
+0

是的,它的工作。再次感謝您.. – Rakesh

+0

多一個問題..是否有任何替代方法,而不是getApplicationContext()。setWallpaper(bmp); ?? – Rakesh

0

你應該declarate每一個活動中您的清單,我也認爲你能創造更美好碼。

我會提供一些建議來幫助你。

1-您可以明確命名其視圖。

<TextView 
android:id="@+id/txt_view_send" 
... > 
</TextView> 

2-使用camelNotation Java代碼

Bundle gotBasket ... 

未來的大項目有很大的幫助!

3-使用match_parent取消您的意見

FILL_PARENT,這意味着視圖想要成爲大如其父(在API級別8或更高改名MATCH_PARENT)。

http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html

4-捆綁使用,只有當你有很多爭論。你可以在你的例子中這樣做。

//你可以添加一個變量獲得EDITTEXT值或直接傳遞到putExtras()的值

String bread = etg.getText().toString(); 
Intent a = new Intent(Get.this, Send.class); 
a.putExtras("key", bread); 
startActivity(a); 

http://developer.android.com/reference/android/content/Intent.html

4驗證可以幫助避免例外。

//example 

if(getIntent().getExtras() != null){ 
    tvs.setText(getIntent().getExtras().getString("key")); 
} 



//your example 
Bundle gotbasket = getIntent().getExtras(); 
if(gotbasket != null){ 
    gotbread = gotbasket.getString("key"); 
if(gotbread != null{ 
    tvs.setText(gotbread); 
} 

它可以產生NullPointerException。

我希望它可以幫助你!

乾杯

編程繼續!

+1

謝謝......它可以幫助我提高編程技能。我對Id很困惑。並且還要照顧其他事物。非常感謝.. – Rakesh

相關問題