在这个论坛上,我知道这是三个问题,但没有一个能解决我的问题。他们没有帮助我。
我正在尝试学习android系统,我编写了一些代码,但是它在任何时候都无法在手机上运行。每当它说“已经停止工作”
我的代码:
Java
package com.example.murathan.oyun;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button javauyu, javasavas, javayemek;
TextView javaeylem, javaistatistik;
Karakter k;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
k = new Karakter();
javauyu = (Button)findViewById(R.id.bt_uyu);
javasavas = (Button)findViewById(R.id.bt_savas);
javayemek = (Button)findViewById(R.id.bt_yemek);
javaeylem = (TextView)findViewById(R.id.tv_eylem);
javaistatistik = (TextView)findViewById(R.id.tv_istatistik);
javauyu.setOnClickListener(this);
javayemek.setOnClickListener(this);
javasavas.setOnClickListener(this);
k.kilo = 50;
k.action = 10;
k.attackD = 80;
javaistatistik.setText(k.toString());
}
@Override
public void onClick(View v) {
if (v.getId() == javauyu.getId())
{
javaeylem.setText(k.komutuyu());
}
else if (v.getId() == javasavas.getId())
{
javaeylem.setText(k.komutsavas());
}
else if (v.getId() == javayemek.getId())
{
javaeylem.setText(k.komutye());
}
javaistatistik.setText(k.toString());
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<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"
android:gravity="center"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
<TextView
android:id="@+id/tv_istatistik"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@string/size"
android:gravity="center"
android:padding="@string/pad"
android:text="@string/istatistik"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
>
<Button
android:id="@+id/bt_uyu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@string/size"
android:padding="@string/pad"
android:text="@string/b_uyu"
/>
<Button
android:id="@+id/bt_yemek"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@string/size"
android:padding="@string/pad"
android:text="@string/b_yemek"
/>
<Button
android:id="@+id/bt_savas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@string/size"
android:padding="@string/pad"
android:text="@string/b_savas"
/>
</LinearLayout>
<TextView
android:id="@+id/tv_eylem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@string/size"
android:padding="@string/pad"
android:text="@string/eylemyap"
/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
和崩溃(LogCat)
08-18 21:17:45.816 22618-22618/com.example.murathan.oyun E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.murathan.oyun, PID: 22618
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.murathan.oyun/com.example.murathan.oyun.MainActivity}: android.view.InflateException: Binary XML file line #17: Binary XML file line #17: Error inflating class TextView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
Caused by: android.view.InflateException: Binary XML file line #17: Binary XML file line #17: Error inflating class TextView
Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class TextView
Caused by: java.lang.UnsupportedOperationException: Can't convert value at index 14 to dimension: type=0x3
at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java)
at android.view.View.<init>(View.java)
at android.widget.TextView.<init>(TextView.java)
at android.widget.TextView.<init>(TextView.java)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:87)
at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:83)
at android.support.v7.app.AppCompatViewInflater.createTextView(AppCompatViewInflater.java:177)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:102)
at android.support.v7.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1266)
at android.support.v7.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1316)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
at android.view.LayoutInflater.rInflate(LayoutInflater.java)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java)
at android.view.LayoutInflater.rInflate(LayoutInflater.java)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at android.view.LayoutInflater.inflate(LayoutInflater.java)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.murathan.oyun.MainActivity.onCreate(MainActivity.java:16)
at android.app.Activity.performCreate(Activity.java)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
请帮我解决我的问题,自2天以来我一直在研究,但我无法解决。
如果有任何写作问题,对不起,我不是英语,所以我写得不好。
答案 0 :(得分:4)
这是你的问题
android:padding="@string/pad"
填充和文本大小需要获得10dp之类的尺寸,而不是字符串
答案 1 :(得分:0)
在您的xml中,您已设置
android:textSize="@string/size"
android:padding="@string/pad"
代替设定
@string/size
,您必须使用@dimen/size
check here