我遇到了一个问题,即在TextView
内尝试拨打onTap
美元会导致应用崩溃。将其移至onCreate()
然后我无法在onTap()
内设置文本。我如何制作它以便我可以将textview放在onTap
中或者使其成为可以访问oncreate
之外的textview?
package com.de_veloper.idlegame;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity{
TextView dollars;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dollars = (TextView)findViewById(R.id.dollars);
}
//
//
//
//code beginning
//
//
//
public int do_llars;
public int upg_click_cost;
public int upg_click_effect;
public int upg_click_level;
String $do_llars = String.valueOf(do_llars);
String $upg_click_cost = String.valueOf(upg_click_cost);
String $upg_click_level = String.valueOf(upg_click_level);
String $upg_click_effect = String.valueOf(upg_click_effect);
public void onTap(View view){
if (upg_click_level == 0){
upg_click_level = 1;
upg_click_effect = 1;
upg_click_effect = upg_click_level * upg_click_effect;
}
do_llars = do_llars + upg_click_effect;
dollars.setText($do_llars);
}
public void upgradeBuy(){
}
}
XML代码在这里:
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="@string/moneybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16pt"
android:padding="50px"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:clickable="true"
android:onClick="onTap"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/totaldo_llars"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="Total do_llars: "
android:textSize="8pt" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/do_llars"
android:id="@+id/dollars"
android:layout_alignTop="@+id/totaldo_llars"
android:layout_toRightOf="@+id/totaldo_llars"
android:layout_toEndOf="@+id/totaldo_llars"
android:textSize="8pt"
android:textIsSelectable="false"
/>
</RelativeLayout>
答案 0 :(得分:0)
变化 dollars.setText($ do_llars);至 view..setText($ do_llars);
答案 1 :(得分:0)
本声明:
String $do_llars = String.valueOf(do_llars);
在为do_llars
分配任何值之前执行,这可能导致空引用异常。
解决方案:在为do_llars分配值后调用上述语句或尝试Syed Zainul Abedin的答案。
答案 2 :(得分:0)
问题似乎存在于变量中。在为do_llars,upg_click_cost,upg_click_effect,upg_click_level分配任何值之前,您正在设置String变量$ do_llars,$ upg_click_cost,$ upg_click_level,$ upg_click_effect。所以将代码修改为
public int do_llars;
public int upg_click_cost;
public int upg_click_effect;
public int upg_click_level;
String $do_llars = ""; //String.valueOf(do_llars);
String $upg_click_cost = ""; //String.valueOf(upg_click_cost);
String $upg_click_level = ""; //String.valueOf(upg_click_level);
String $upg_click_effect = ""; //String.valueOf(upg_click_effect);
public void onTap(View view){
if (upg_click_level == 0){
upg_click_level = 1;
$upg_click_level = String.valueOf(upg_click_level);
upg_click_effect = 1;
upg_click_effect = upg_click_level * upg_click_effect;
$upg_click_effect = String.valueOf(upg_click_effect);
}
do_llars = do_llars + upg_click_effect;
$do_llars = String.valueOf(do_llars);
dollars.setText($do_llars);
}
将值分配给upg_click_cost
时,您可以用同样的方式初始化$upg_click_cost
。
但是不是分别维护int和字符串变量,而是建议您在哪里显示do_llars, upg_click_cost, upg_click_effect, upg_click_level
使用String.valueOf(do_llars), String.valueOf(upg_click_cost), String.valueOf(upg_click_effect), String.valueOf(upg_click_level)
的值,这样每次更改时都不需要将更新后的值赋给String变量int变量。
答案 3 :(得分:0)
修正了它。这是我现在的代码
package com.de_veloper.idlegame;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity{
TextView dollars;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dollars = (TextView)findViewById(R.id.dollars);
}
//
//
//
//code beginning
//
//
//
public int do_llars;
public int upg_click_cost;
public int upg_click_effect;
public int upg_click_level;
public void onTap(View view){
//da strings
String $do_llars = String.valueOf(do_llars);
String $upg_click_cost = String.valueOf(upg_click_cost);
String $upg_click_level = String.valueOf(upg_click_level);
String $upg_click_effect = String.valueOf(upg_click_effect);
//if he's new it will make him level 1
if (upg_click_level == 0){
upg_click_level = 1;
upg_click_effect = 1;
upg_click_effect = upg_click_level * 1;
upg_click_cost = upg_click_level * 2;
}
do_llars = do_llars + upg_click_effect;
TextView dollars = (TextView)findViewById(R.id.dollars);
dollars.setText($do_llars);
}
public void upgradeBuy(){
if (do_llars >= upg_click_cost){
do_llars = do_llars - upg_click_cost;
upg_click_level = upg_click_level + 1;
}
}
}