当我单击按钮调用第二个活动时,为什么我的应用程序停止工作?

时间:2013-10-06 05:09:54

标签: android android-intent

我有一个简单的应用程序,在主选项卡上,我有一个按钮。单击按钮时,它会启动另一个选项卡。但是当我点击按钮时,屏幕变为空白,并显示:

  

不幸的是,appname已停止

我哪里出错了?

代码段包括:

  1. MainActivity.Java

       public class MainActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    }
    
    @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;
    }
    
    public void serveMenu(View view) {
      // Do something in response to button
    Intent intent = new Intent(this, ServeMenuActivity.class);
    startActivity(intent);
    
      }
        }
    
  2. ServeMenuActivity.Java

     import android.app.Activity;
     import android.content.Intent;
     import android.os.Bundle;
     import android.view.Menu;
    
       public class ServeMenuActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_serve_menu);
    Intent intent = getIntent();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.serve_menu, menu);
    return true;
     }
    
       }
    
  3. AdriodManifest.xml

                              

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.mywinsystems.tkrctennistips.ServeMenuActivity"
        android:label="@string/title_activity_serve_menu" >
    
    </activity>
    

2 个答案:

答案 0 :(得分:0)

检查是否在main.xml按钮上添加了onClick serveMenu,即android:onClick="serveMenu"

答案 1 :(得分:0)

10-06 17:30:41.249: D/OpenGLRenderer(18009): Enabling debug mode 0 10-06 17:31:46.774: D/AndroidRuntime(18009): Shutting down VM 10-06 17:31:46.774: W/dalvikvm(18009): threadid=1: thread exiting with uncaught exception (group=0x4108f2a0) 10-06 17:31:46.784: E/AndroidRuntime(18009): FATAL EXCEPTION: main 10-06 17:31:46.784: E/AndroidRuntime(18009): java.lang.IllegalStateException: Could not find a method ServeMenu(View) in the activity class com.mywinsystems.tkrctennistips.MainActivity for onClick handler on view class android.widget.Button with id 'button1' 

根据你上面的locgcate 尝试重命名

public void serveMenu(View view) {
  // Do something in response to button
Intent intent = new Intent(this, ServeMenuActivity.class);
startActivity(intent);

  }

public void ServeMenu(View view) {
  // Do something in response to button
Intent intent = new Intent(this, ServeMenuActivity.class);
startActivity(intent);

  }