Java 按下按钮后,活动将关闭
在这里,我试图在按钮单击事件上从MainActivity打开一个名为LevelActivity的活动,但当我试图打开我的LevelActivity时,它会将我抛出应用程序之外。下面给出了XML和JAVA文件 My LevelActivity.java ActivityMain.java的代码 这是我的主要活动,正如我前面所说的,我希望从主要活动导航到级别活动 这是我的LevelActivity的XML文件 请帮助我解决这个问题,如果你想要任何其他文件,如cat日志,请提及 下面是我在调试控制台上发现的内容 希望这会有所帮助Java 按下按钮后,活动将关闭,java,android,android-activity,Java,Android,Android Activity,在这里,我试图在按钮单击事件上从MainActivity打开一个名为LevelActivity的活动,但当我试图打开我的LevelActivity时,它会将我抛出应用程序之外。下面给出了XML和JAVA文件 My LevelActivity.java ActivityMain.java的代码 这是我的主要活动,正如我前面所说的,我希望从主要活动导航到级别活动 这是我的LevelActivity的XML文件 请帮助我解决这个问题,如果你想要任何其他文件,如cat日志,请提及 下面是我在调试控制台上
E/AndroidRuntime: FATAL EXCEPTION: main
Process: cheezycode.com.justmaths, PID: 1634
java.lang.RuntimeException: Unable to start activity ComponentInfo{cheezycode.com.justmaths/cheezycode.com.justmaths.LevelActivity}: android.view.InflateException: Binary XML file line #19: Can't convert to dimension: type=0x12
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2487)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2547)
at android.app.ActivityThread.access$1100(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5604)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
Caused by: android.view.InflateException: Binary XML file line #19: Can't convert to dimension: type=0x12
at android.view.LayoutInflater.inflate(LayoutInflater.java:543)
at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
at cheezycode.com.justmaths.LevelActivity.onCreate(LevelActivity.java:14)
at android.app.Activity.performCreate(Activity.java:6358)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2440)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2547)
at android.app.ActivityThread.access$1100(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5604)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x12
at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:668)
at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:7086)
at android.widget.RelativeLayout$LayoutParams.<init>(RelativeLayout.java:1241)
at android.widget.RelativeLayout.generateLayoutParams(RelativeLayout.java:1087)
at
android.widget.RelativeLayout.generateLayoutParams(RelativeLayout.java:84)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:841)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:802)
at android.view.LayoutInflater.inflate(LayoutInflater.java:519)
这就是错误的地方:
android:layout_marginLeft="@+id/btnEasy"
边距应该是数字,而不是视图
如果要将其放置在该视图旁边,请使用:
android:layout_toLeftOf="@id/btnEasy"
此外,每次引用现有视图时,不应包含+符号。只有当您在id属性上命名一个新视图时,才会出现这种情况。最后,在弄得如此混乱之后,我只需重新编写所有代码,并找出一些错误,然后使用此代码在LevelActivity中进行更改
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class LevelActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level);
Button btn1 = (Button) findViewById(R.id.button1);
Button btn2 = (Button) findViewById(R.id.button2);
Button btn3 = (Button) findViewById(R.id.button3);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int seconds = 120;
// int Live = 10;
Intent myIntent = new Intent(LevelActivity.this, GameActivity.class);
myIntent.putExtra("secondA", seconds);
startActivity(myIntent);
// myIntent.putExtra("varLive", Live);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int seconds = 60;
// int Live = 10;
Intent myIntent = new Intent(LevelActivity.this, GameActivity.class);
myIntent.putExtra("secondA", seconds);
startActivity(myIntent);
// myIntent.putExtra("varLive", Live);
}
});
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int seconds = 40;
// int Live = 10;
Intent myIntent = new Intent(LevelActivity.this, GameActivity.class);
myIntent.putExtra("secondA", seconds);
startActivity(myIntent);
// myIntent.putExtra("varLive", Live);
}
});
}
}
如果我们能看到您的日志会有帮助吗?您是否在清单中声明了LevelActivity?是的,我在清单中声明了LevelActivity请不要让人们猜测,添加您的日志,以便我们提供更多信息answers@NikosHidalgo好的,我会的!查看我更新的帖子,你需要在不同的地方更改你的代码
android:layout_marginLeft="@+id/btnEasy"
android:layout_toLeftOf="@id/btnEasy"
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class LevelActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level);
Button btn1 = (Button) findViewById(R.id.button1);
Button btn2 = (Button) findViewById(R.id.button2);
Button btn3 = (Button) findViewById(R.id.button3);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int seconds = 120;
// int Live = 10;
Intent myIntent = new Intent(LevelActivity.this, GameActivity.class);
myIntent.putExtra("secondA", seconds);
startActivity(myIntent);
// myIntent.putExtra("varLive", Live);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int seconds = 60;
// int Live = 10;
Intent myIntent = new Intent(LevelActivity.this, GameActivity.class);
myIntent.putExtra("secondA", seconds);
startActivity(myIntent);
// myIntent.putExtra("varLive", Live);
}
});
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int seconds = 40;
// int Live = 10;
Intent myIntent = new Intent(LevelActivity.this, GameActivity.class);
myIntent.putExtra("secondA", seconds);
startActivity(myIntent);
// myIntent.putExtra("varLive", Live);
}
});
}
}