在这里,我尝试在按钮单击事件中从 MainActivity 打开一个名为 LevelActivity 的活动,但是当我尝试打开我的LevelActivity时,它使我离开了应用程序。 XML 和 JAVA 文件如下。
我的LevelActivity.java
public class LevelActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level);
Button btnEasy = (Button) findViewById(R.id.btnEasy);
Button btnHard = (Button) findViewById(R.id.btnHard);
Button btnNormal = (Button) findViewById(R.id.btnNormal);
btnEasy.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("varSeconds", seconds);
myIntent.putExtra("varLive", Live);
startActivity(myIntent);
}
});
btnHard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int seconds = 60;
int Lives = 5;
Intent myIntent = new Intent(LevelActivity.this, GameActivity.class);
myIntent.putExtra("varSeconds", seconds);
myIntent.putExtra("varLive", Lives);
startActivity(myIntent);
}
});
btnNormal.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("varSeconds", seconds);
myIntent.putExtra("varLive", Live);
startActivity(myIntent);
}
});
}}
ActivityMain.java的代码
这是我的主要活动,如我之前所说,我想从主要活动导航到关卡活动
public class MainActivity extends AppCompatActivity {
ImageButton btnMathPlay, btnMathShare, btnMathRate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnMathPlay = (ImageButton) findViewById(R.id.btnMathPlay);
btnMathShare= (ImageButton) findViewById(R.id.btnMathShare);
btnMathRate = (ImageButton) findViewById(R.id.btnMathRate);
btnMathPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*
* Example of Explicit Intent
* When you click Play Button on the screen
* Game Activity will be started
*/
Intent i = new Intent(MainActivity.this,LevelActivity.class);
startActivity(i);
}
});
btnMathShare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*
* Example of Implict Intent
* When you click Share Button on the screen
* Android will find the activities that allow to share messages.
*/
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Just Maths - Fun way to learn Maths. http://www.play.google.com");
startActivity(intent);
}
});
btnMathRate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*
* Simple Toast Message To Display Message For Short Duration
* Link that to your app landing page.
*/
Toast.makeText(MainActivity.this,"You can open your Google Play landing page",Toast.LENGTH_LONG).show();
}
});
}}
这是我用于LevelActivity的XML文件
<?xml version="1.0" encoding="utf-8"?>
<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="@color/colorAccent"
tools:context=".LevelActivity"
tools:layout_editor_absoluteY="25dp">
<Button
android:id="@+id/btnEasy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="134dp"
android:text="Easy" />
<Button
android:id="@+id/btnNormal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnEasy"
android:layout_alignStart="@+id/btnEasy"
android:layout_alignLeft="@+id/btnEasy"
android:layout_marginStart="0dp"
android:layout_marginLeft="@+id/btnEasy"
android:layout_marginTop="35dp"
android:text="Normal" />
<Button
android:id="@+id/btnHard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnNormal"
android:layout_alignStart="@+id/btnEasy"
android:layout_alignLeft="@+id/btnEasy"
android:layout_alignParentBottom="true"
android:layout_marginTop="36dp"
android:layout_marginBottom="220dp"
android:text="Hard" />
</RelativeLayout>
请帮助我解决此问题,如果您还需要其他类似cat log的文件,请提及!
这是我在调试控制台上找到的内容 希望如此对您有帮助
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)
答案 0 :(得分:2)
这是错误的地方:
android:layout_marginLeft="@+id/btnEasy"
边距应该是数字,而不是视图。
如果您打算将其放置在该视图旁边,请使用:
android:layout_toLeftOf="@id/btnEasy"
同样,每次引用现有视图时,也不应包含+符号。仅当您在其ID属性上命名新视图时,这种情况才会发生。
答案 1 :(得分:-1)
最后,在造成了很多混乱之后,我再次重写了所有代码,并找出一些错误并使用此代码对LevelActivity进行了更改
var orders = from o in suborder.OrderLineItemList
where
o.Product == "Shoes" ||
o.BundleComponentList.Any(c => c.Product == "Shoes")
select o;
bool isShoesOrder = orders.Any();