我在我的应用中创建了两个活动。 当我点击MainActivity中的一个按钮时,它显示致命异常错误。
MainActivity.java
package com.example.ravi.testingstudio;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
public class MainActivity extends AppCompatActivity {
Button button;
AdView adView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
adView = (AdView)findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
adView.loadAd(adRequest);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
}
`
activity_main.xml文件
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity">
<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Second Activity"
android:textSize="25sp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="0dp"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Go to First Activity"
android:gravity="center"
android:layout_marginBottom="@id/txt"/>
</android.support.constraint.ConstraintLayout>
SecondActivity.java文件
package com.example.ravi.testingstudio;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
`public class SecondActivity extends AppCompatActivity {`
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
btn= (Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(SecondActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
}
second_main.xml文件
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity">
<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Second Activity"
android:textSize="25sp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="0dp"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Go to First Activity"
android:gravity="center"
android:layout_marginBottom="@id/txt"/>
</android.support.constraint.ConstraintLayout>
错误消息显示 enter code here
`E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ravi.testingstudio, PID: 4469
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ravi.testingstudio/com.example.ravi.testingstudio.SecondActivity}: android.view.InflateException: Binary XML file line #21: Can't convert value at index 6 to dimension: type=0x12
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: android.view.InflateException: Binary XML file line #21:
Can't convert value at index 6 to dimension: type=0x12
Caused by: java.lang.UnsupportedOperationException: Can't convert value at index 6 to dimension: type=0x12
at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:720)
at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:7271)
at android.support.constraint.ConstraintLayout$LayoutParams.<init>(ConstraintLayout.java:2590)
at android.support.constraint.ConstraintLayout.generateLayoutParams(ConstraintLayout.java:1913)
at android.support.constraint.ConstraintLayout.generateLayoutParams(ConstraintLayout.java:475)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:860)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.ravi.testingstudio.SecondActivity.onCreate(SecondActivity.java:14)
at android.app.Activity.performCreate(Activity.java:6664)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at
and com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
enter code here
`