I read a lot of here but nothing would help me.

As the title says i'm programming a simple android application with two activities. The first includes a button. By clicking this the second activity should be activated. But nothing happens.

My Manifest:

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.schnitzeljagd.UiActivity"
        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.schnitzeljagd.showArchivements"
        android:label="@string/app_name" >
    </activity>
</application>

</manifest>

My first activity is:

package com.example.schnitzeljagd;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class UiActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ui_activitiy);
    Button next = (Button) findViewById(R.id.button2);
    final Intent intent = new Intent(this, showArchivements.class);
    next.setOnClickListener(new View.OnClickListener() {


        public void onClick(View v) {
    startActivity(intent);  }
    });
    }

@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;
   }


}

And my second:

package com.example.schnitzeljagd;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class showArchivements extends Activity{


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



    }

}

Has anyone some ideas what might be wrong here?

Sorry for my creepy english and thanks for your answers!

有帮助吗?

解决方案

You're inflating the uiactivity again in the showArchivements.

Try this:

public class showArchivements extends Activity{

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

其他提示

The problem here is that you are using the same layout for both Activities. Actually second Activity is opened normally, but you don't see any difference because the layout is the same. So just create another layout for the second Activity and use it at the setContentView call.

Change R.layout.ui_activitiy to other in showArchivements activity.

Put this as your Click pulse in the activity where your button is? In your main now.

"name of the activity where you are in now".this, "name of the activity you want to open".class

    public class OnClick implements OnClickListener {   

    @Override
    public void onClick(View v) {
        startActivity(new Intent(**main_activity.this, second_activity.class**));
    }
}   

Maybe you can put one textView in your onCreate in the second activity, then you know for sure that something need to pop up after clicking ;-)

And put the onClick part (shown above..) after your onCreateOptionsMenu. And delete the onClick part in your onCreate.

And your first and second activity needs another layout .xml

Try this, I hope it work! And when it work, let it know! Mark this post as solved! :-)

Goodluck,

Try this...

Thus demo application tutorial show you how to interact with activity, when a button is clicked, navigate from current screen (current activity) to another screen (another activity).

UiActivity.java

package com.example.schnitzeljagd;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class UiActivity extends Activity {

 private Button next;

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

   next = (Button) findViewById(R.id.button2);

   next.setOnClickListener(new View.OnClickListener() {

      public void onClick(View v) {

       Intent intent = new Intent(this, showArchivements.class);
       startActivity(intent); 
       }
    });
   }

  @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;
  }
}

showArchivements.java

package com.example.schnitzeljagd;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

 public class showArchivements extends Activity{


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

  }

 }

Manifest.xml

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

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
   <activity
       android:name="com.example.schnitzeljagd.UiActivity"
       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.schnitzeljagd.showArchivements"
           android:label="@string/app_name" >
       </activity>
  </application>

 </manifest>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top