Neues Fenster hinzufügen

  • 5 Antworten
  • Letztes Antwortdatum
T

tomycat

Ambitioniertes Mitglied
0
hallo,
ich möchte gerne auf einen Button tippen und dann soll sich ein neue Fenster öffnen.
Mein Handy läuft auf Andorid 4.2.2 und mein Studio ist Eclipse.


klicke ich auf say hello, dann wird die App beendet.

die
Code:
public class MainActivity extends Activity
  implements OnClickListener {
  private Button hiButton;
  private Button helloButton;
  private Button button1;
  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

  hiButton = (Button) findViewById(R.id.hi_button);
  hiButton.setOnClickListener(this);

  helloButton = (Button) findViewById(R.id.hello_button);
  helloButton.setOnClickListener(this);
  }

  public void onClick(View v) {
  EditText nameField = (EditText) findViewById(R.id.name_field);
  String name = nameField.getText().toString();
  if (name.length() == 0) {
  new AlertDialog.Builder(this).setMessage(
  R.string.error_name_missing).setNeutralButton(
  R.string.error_ok,
  null).show();
  return;
  }

  if (v == hiButton || v == helloButton) {
  int resourceId = v == hiButton ? R.string.hi_greeting
  : R.string.hello_greeting;

  String greeting = getResources().getString(resourceId, name);
  Toast.makeText(this, greeting, Toast.LENGTH_LONG).show();

  TextView greetingField = (TextView) findViewById(R.id.greeting_field);
  greetingField.setText(greeting);
   
  //  neu und geht net !!!!
       Intent intent = new Intent(this, Activity_2.class);  
       this.startActivity(intent);
  }
   
  }

}


das Layout
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
<TextView  
  android:id="@+id/greeting_field"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello"
  />
<TextView  
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="20dp"
  android:text="@string/enter_your_name"
  />

<EditText
  android:id="@+id/name_field"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  />

  <LinearLayout
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  >
  <Button
  android:id="@+id/hi_button"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/hi_button"
  android:layout_weight="1"
  />
  <Button
  android:id="@+id/hello_button"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/hello_button"
  android:layout_weight="1"
  />
     <Button
  android:id="@+id/button1"
  android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:text="@string/geheZuActivity2"
     />
  </LinearLayout>
   
</LinearLayout>


DAS 2 Fenster !!!!


Code:
public class setup extends Activity implements OnClickListener {

  private Button rechnerButton;
  private Button activity1Button;
   
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.setup);
   
  activity1Button = (Button) this.findViewById(R.id.button1);
  activity1Button.setOnClickListener(this);
   
  }

   
  public void onClick(View v) {
   
  if(v == rechnerButton) {
  Intent intent = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_APP_CALCULATOR);
  PackageManager pm = getPackageManager();
  ComponentName cn  = intent.resolveActivity(pm);
   
  if(cn != null) {
  startActivity(intent);
  }
  else {
  Toast t = Toast.makeText(this, "Kein Taschenrechner vorhanden!", Toast.LENGTH_SHORT);
  t.show();  
  }


  }
  else if(v == activity1Button) {
  Intent intent = new Intent(this, MainActivity.class);
  startActivity(intent);
  }
  }
}

DAS Layout
Code:
<?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" >
   
</LinearLayout>
 
01-26 21:11:13.934: D/libEGL(6240): loaded /system/lib/egl/libGLESv2_VIVANTE.so
01-26 21:11:13.964: D/OpenGLRenderer(6240): Enabling debug mode 0
01-26 21:11:19.384: W/IInputConnectionWrapper(6240): getCursorCapsMode on inactive InputConnection
01-26 21:11:36.728: D/AndroidRuntime(6240): Shutting down VM
01-26 21:11:36.728: W/dalvikvm(6240): threadid=1: thread exiting with uncaught exception (group=0x41916930)
01-26 21:11:36.738: E/AndroidRuntime(6240): FATAL EXCEPTION: main
01-26 21:11:36.738: E/AndroidRuntime(6240): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.kinup/com.example.kinup.Activity_2}; have you declared this activity in your AndroidManifest.xml?
01-26 21:11:36.738: E/AndroidRuntime(6240): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1618)
01-26 21:11:36.738: E/AndroidRuntime(6240): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
01-26 21:11:36.738: E/AndroidRuntime(6240): at android.app.Activity.startActivityForResult(Activity.java:3375)
01-26 21:11:36.738: E/AndroidRuntime(6240): at android.app.Activity.startActivityForResult(Activity.java:3336)
01-26 21:11:36.738: E/AndroidRuntime(6240): at android.app.Activity.startActivity(Activity.java:3571)
01-26 21:11:36.738: E/AndroidRuntime(6240): at android.app.Activity.startActivity(Activity.java:3539)
01-26 21:11:36.738: E/AndroidRuntime(6240): at com.example.kinup.MainActivity.onClick(MainActivity.java:67)
01-26 21:11:36.738: E/AndroidRuntime(6240): at android.view.View.performClick(View.java:4204)
01-26 21:11:36.738: E/AndroidRuntime(6240): at android.view.View$PerformClick.run(View.java:17394)
01-26 21:11:36.738: E/AndroidRuntime(6240): at android.os.Handler.handleCallback(Handler.java:725)
01-26 21:11:36.738: E/AndroidRuntime(6240): at android.os.Handler.dispatchMessage(Handler.java:92)
01-26 21:11:36.738: E/AndroidRuntime(6240): at android.os.Looper.loop(Looper.java:137)
01-26 21:11:36.738: E/AndroidRuntime(6240): at android.app.ActivityThread.main(ActivityThread.java:5106)
01-26 21:11:36.738: E/AndroidRuntime(6240): at java.lang.reflect.Method.invokeNative(Native Method)
01-26 21:11:36.738: E/AndroidRuntime(6240): at java.lang.reflect.Method.invoke(Method.java:511)
01-26 21:11:36.738: E/AndroidRuntime(6240): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
01-26 21:11:36.738: E/AndroidRuntime(6240): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:588)
01-26 21:11:36.738: E/AndroidRuntime(6240): at dalvik.system.NativeStart.main(Native Method)
 
Hast du die activity in der AndroidManifest.XML registriert ?

Die musst du vorher registrieren.
ActivityNotFoundException sagt aus das es nicht so ist.

Registriere die Activity und teste es erneuert :)
 
Der Punkt von meinem Vorredner, plus der Name deiner zweiten Activity ist "setup" und nicht "Activity_2" außerdem ...

"Interessanter" Code, den du da zusammengeschustert hast:
Code:
if (v == hiButton || v == helloButton) {
  int resourceId = v == hiButton ? R.string.hi_greeting
  : R.string.hello_greeting;
 
Hast du das Problem gelöst bekommen ?
 
danke an alle, es lag an der Manifist Datei.
 
Zurück
Oben Unten