Android - 如何打开另一个活动单击按钮时

时间:2015-07-18 13:35:33

标签: android

我正在尝试打开一个Activity,其中包含一些 TextView的 EditText的,我的代码工作得很早,但现在当我点击按钮时打开Activity,它说“不幸......已经停止!”。这是一段代码:

Button address=(Button)findViewById(R.id.setOrChangeAddress);
address.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
            Intent intent = new Intent(MainActivityWithButtons.this, ChangeAddressFromAddressButton.class);
            startActivity(intent);      
            }
        });

请你能帮助我!

修改

LogCat输出:

07-18 15:43:31.470: D/(3072): HostConnection::get() New Host Connection established 0xa580bea0, tid 3072
07-18 15:43:35.020: D/AndroidRuntime(3072): Shutting down VM
07-18 15:43:35.020: E/AndroidRuntime(3072): FATAL EXCEPTION: main
07-18 15:43:35.020: E/AndroidRuntime(3072): Process: com.example.policealert, PID: 3072
07-18 15:43:35.020: E/AndroidRuntime(3072): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.example.policealert.ChangeAddressFromAddressButton }
07-18 15:43:35.020: E/AndroidRuntime(3072):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1781)
07-18 15:43:35.020: E/AndroidRuntime(3072):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1501)
07-18 15:43:35.020: E/AndroidRuntime(3072):     at android.app.Activity.startActivityForResult(Activity.java:3745)
07-18 15:43:35.020: E/AndroidRuntime(3072):     at android.app.Activity.startActivityForResult(Activity.java:3706)
07-18 15:43:35.020: E/AndroidRuntime(3072):     at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:820)
07-18 15:43:35.020: E/AndroidRuntime(3072):     at android.app.Activity.startActivity(Activity.java:4016)
07-18 15:43:35.020: E/AndroidRuntime(3072):     at android.app.Activity.startActivity(Activity.java:3984)
07-18 15:43:35.020: E/AndroidRuntime(3072):     at com.example.policealert.MainActivityWithButtons$1.onClick(MainActivityWithButtons.java:34)
07-18 15:43:35.020: E/AndroidRuntime(3072):     at android.view.View.performClick(View.java:4780)
07-18 15:43:35.020: E/AndroidRuntime(3072):     at android.view.View$PerformClick.run(View.java:19866)
07-18 15:43:35.020: E/AndroidRuntime(3072):     at android.os.Handler.handleCallback(Handler.java:739)
07-18 15:43:35.020: E/AndroidRuntime(3072):     at android.os.Handler.dispatchMessage(Handler.java:95)
07-18 15:43:35.020: E/AndroidRuntime(3072):     at android.os.Looper.loop(Looper.java:135)
07-18 15:43:35.020: E/AndroidRuntime(3072):     at android.app.ActivityThread.main(ActivityThread.java:5257)
07-18 15:43:35.020: E/AndroidRuntime(3072):     at java.lang.reflect.Method.invoke(Native Method)
07-18 15:43:35.020: E/AndroidRuntime(3072):     at java.lang.reflect.Method.invoke(Method.java:372)
07-18 15:43:35.020: E/AndroidRuntime(3072):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
07-18 15:43:35.020: E/AndroidRuntime(3072):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

编辑:

对不起,这是我的清单文件:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivityWithButtons"
            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=".ChangeAddressFromAddressButton"
            android:label="@string/title_activity_change_address_from_address_button" >
        </activity>
        <activity
            android:name=".LocationSelectFromAlertButton"
            android:label="@string/title_activity_location_select_from_alert_button" >
        </activity>
        <activity
            android:name=".LocationSelectFromAlertButtons"
            android:label="@string/title_activity_location_select_from_alert_buttons" >
        </activity>
    </application>

</manifest>

4 个答案:

答案 0 :(得分:0)

看起来你得到的是android.content.ActivityNotFoundException。您的Manifest.xml文件中找不到该活动。

你应该包括

<activity
     android:name="com.example.policealert.ChangeAddressFromAddressButton"   
     android:label="@string/title_activity_change_address_from_address_button">
</activity>
在您的Manifest.xml文件中

并更改

Intent intent = new Intent(MainActivityWithButtons.this, ChangeAddressFromAddressButton.class);
startActivity(intent); 

Intent intent = new Intent("com.example.policealert.ChangeAddressFromAddressButton");
startActivity(intent); 

答案 1 :(得分:0)

请在清单文件中写下此代码

 <activity
        android:name=".ChangeAddressFromAddressButton"
  >
 </activity>

答案 2 :(得分:0)

您在清单文件中未添加 ChangeAddressFromAddressButton 活动 android.content.ActivityNotFoundException:未找到任何活动异常

<activity
    android:name="com.your.package.name.ChangeAddressFromAddressButton"
    android:screenOrientation="portrait"
    android:configChanges="orientation"
    android:windowSoftInputMode="adjustPan" >
</activity>

希望它会对你有所帮助。

答案 3 :(得分:0)

activity_change_address_from_address_button.xml中,您试图在实际加载Button save; TextView currentAddress; EditText addressNew; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_change_address_from_address_button); save =(Button)findViewById(R.id.saveBtn); currentAddress =(TextView)findViewById(R.id.CurrAddress); addressNew=(EditText)findViewById(R.id.tbNewAddress); // your logic } 之前实例化视图。

试试这个。它会起作用。

$xml_load = simplexml_load_file($Your_xml);
$song = $xml_load->addChild("song");
$song->addChild("title", "song title");
$song->addChild("album", "song album");
$song->addChild("artist", "song artist");
$song->addChild("length", "03:01");

echo $xml_load->saveXML()
相关问题