Guten Tag ich bin es mal wieder,
ich habe nun ein Problem. Ich möchte gerne eine neue Activity aufrufen per ButtonClick, das Problem welches ich jedoch habe ist, dass mir ADT/Eclipse keinerlei Fehler ausspuckt. Die App jedoch abschmiert sobald ich auf den Button drücke.
AndroidManifest.xml
Code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.terratroll.bgcchanger"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.terratroll.bgcchanger.MainActivity"
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.terratroll.bgcchanger.SeekActivity"
android:label="@string/title_activity_seek" >
</activity>
</application>
</manifest>
Alles anzeigen
MainActivity.java
Java
package com.terratroll.bgcchanger;
import java.util.Random;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Intent intent = new Intent(this, SeekActivity.class);
Button red = (Button) findViewById(R.id.button1);
Button green = (Button) findViewById(R.id.button2);
Button blue = (Button) findViewById(R.id.button3);
Button yellow = (Button) findViewById(R.id.button4);
Button orange = (Button) findViewById(R.id.button5);
Button purple = (Button) findViewById(R.id.button6);
Button black = (Button) findViewById(R.id.button7);
Button white = (Button) findViewById(R.id.button8);
Button random = (Button) findViewById(R.id.button9);
Button seek = (Button) findViewById(R.id.button10);
final RelativeLayout layout = (RelativeLayout) findViewById(R.id.container);
red.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
layout.setBackgroundColor(Color.RED);
}
});
green.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
layout.setBackgroundColor(Color.GREEN);
}
});
blue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
layout.setBackgroundColor(Color.BLUE);
}
});
yellow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
layout.setBackgroundColor(Color.YELLOW);
}
});
orange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
layout.setBackgroundColor(Color.rgb(255,140,0));
}
});
purple.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
layout.setBackgroundColor(Color.rgb(138,43,226));
}
});
black.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
layout.setBackgroundColor(Color.BLACK);
}
});
white.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
layout.setBackgroundColor(Color.WHITE);
}
});
random.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
int min = 100;
int max = 255;
Random r = new Random();
Random g = new Random();
Random b = new Random();
final int r1 = r.nextInt(max - min + 1) + min;
final int g1 = g.nextInt(max - min + 1) + min;
final int b1 = b.nextInt(max - min + 1) + min;
layout.setBackgroundColor(Color.rgb(r1,g1,b1));
return false;
}
});
seek.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(intent);
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
Alles anzeigen
Wenn mehr benötigt wird bitte dazu schreiben.
Mfg Joshi