我想在我的主要活动和我的第二个活动之间切换,但每当我这样做时,应用程序都会关闭。 所以在我点击button1“Neues Feld”后,应用程序应切换到第二个活动:feldlayout.class,其布局为:feldlayout.xml!这些活动都在Android_MAnifest中,这也不是问题......
谢谢你的帮助! 现在正在运行!我创建了一个新的View类,将其添加到布局中然后它运行正常!
主要类
package com.example.volleyballapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
public class Main_Layout extends Activity {
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main_layout);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(myhandler);
}
@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__layout, menu);
return true;
}
View.OnClickListener myhandler = new View.OnClickListener(){
public void onClick(View v) {
if(v==b1){
Intent i = new Intent(Main_Layout.this, feldlayout.class);
startActivity(i);
}
}
};}
主要布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background2"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".Main_Layout" >
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="10sp"
android:layout_marginRight="10sp"
android:text=" "
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/farbe" />
<Button
android:id="@+id/button1"
style="@style/ButtonText"
android:layout_below="@id/textView"
android:layout_centerHorizontal="true"
android:background="@drawable/bnt_black"
android:onClick="NeuesFeldClick"
android:text="Neues Feld" />
<TextView
android:layout_width="fill_parent"
android:layout_height="100dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=" "
android:id="@+id/textView2"
android:textColor="@color/farbe"
android:layout_below="@id/button1"/>
<Button
android:id="@+id/button2"
style="@style/ButtonText"
android:layout_below="@id/textView2"
android:layout_centerHorizontal="true"
android:background="@drawable/bnt_black"
android:onClick="LetztesFeldClick"
android:text="Letztes Feld" />
<TextView
android:layout_width="fill_parent"
android:layout_height="100dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=" "
android:id="@+id/textView3"
android:textColor="@color/farbe"
android:layout_below="@id/button2"/>
<Button
android:id="@+id/button3"
style="@style/ButtonText"
android:layout_below="@id/textView3"
android:layout_centerHorizontal="true"
android:background="@drawable/bnt_black"
android:onClick="SavesClick"
android:text="Gespeicherte Felder" />
</RelativeLayout>
第二项活动
package com.example.volleyballapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
public class feldlayout extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.feldlayout);
}
private Paint paint = new Paint();
private Path path = new Path();
int count = 1;
public feldlayout(Context context, AttributeSet attrs) {
super();
paint.setAntiAlias(true);
paint.setStrokeWidth(6f); //Breite des Strichs
paint.setColor(Color.BLACK); //Farbe des Strichs
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
}
protected void onDraw(Canvas canvas) {
if ((count%2 != 0)){
canvas.drawPath(path, paint);}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
count++;
if(count%2 != 1){
path.moveTo(eventX, eventY);
return true;
}
if(count%2 != 0){
path.lineTo(eventX, eventY);
break;
}
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
// nothing to do
break;
default:
return false;
}
// Schedules a repaint.
invalidate();
return true;
}
private void invalidate() {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
我试过了。你的代码工作正常。问题是你在第二个Activity中调用了Constructor
。低于一。
public feldlayout(Context context, AttributeSet attrs) {
super();
paint.setAntiAlias(true);
paint.setStrokeWidth(6f); //Breite des Strichs
paint.setColor(Color.BLACK); //Farbe des Strichs
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
}
请删除此构造函数,然后重试。
注意强>
只需创建一个单独的类并在那里调用constructor
。