所以我给自己做了一个家庭作业:创建一个类进行计算并将该变量返回给要显示的MainActivity
我认为这很简单,但由于某种原因,我的TextView始终为null。当我尝试在新文件中重新创建错误时,我没有通过TextView获得错误 - 我可以将其设置得很好。
我已经在代码运行时记录了值,因此似乎变量正确地传递了数据。但是当我试图将任何值推入我的textView时,我得到了错误。
这是:
package com.kserrattan.clickerdemo001;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MainActivity extends ActionBarActivity {
String countString;
TextView displayCount;
View parent;
public void playerTapped(View view) {
MathCalculations mathCalculations = new MathCalculations();
//parent = (View) view.getRootView();
mathCalculations.addCount();
}
public void getCount(int count) {
Log.i("addCount", Integer.toString(count));
countString = (Integer.toString(count));
displayString();
}
public void displayString() {
displayCount.setText("HIE");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayCount = (TextView) findViewById(R.id.countText);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
二等:
package com.kserrattan.clickerdemo001;
import android.util.Log;
/**
* Created by Kris on 05/10/2016.
*/
public class MathCalculations {
int tapCounter;
public void addCount() {
Log.i("addCount", "HERE");
tapCounter += 1;
MainActivity mainActivity = new MainActivity();
mainActivity.getCount(tapCounter);
}
}
我只能提供部分错误,因为由于某些未知原因,错误的其余部分未正确格式化!希望这一点就够了......:
--------- beginning of crash
10-05 20:00:51.640 2375-2375/com.kserrattan.clickerdemo001 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.kserrattan.clickerdemo001, PID: 2375
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4007)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4002)
答案 0 :(得分:0)
您需要活动上下文来调用其方法。 在onCreate方法中创建上下文值:
public class MainActivity extends ActionBarActivity {
String countString;
TextView displayCount;
View parent;
Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
displayCount = (TextView) findViewById(R.id.countText);
}
然后在调用addCount()时传递此上下文:
public void playerTapped(View view) {
MathCalculations mathCalculations = new MathCalculations();
//parent = (View) view.getRootView();
mathCalculations.addCount(mContext);
}
修改addCount()以使用上下文:
public void addCount(Context mContext) {
Log.i("addCount", "HERE");
tapCounter += 1;
mContext.getCount(tapCounter);
}
答案 1 :(得分:0)
在TextView
MainActivity
设为静态变量
public class MainActivity extends ActionBarActivity {
String countString;
public static TextView displayCount;
View parent;
public void playerTapped(View view) {
MathCalculations mathCalculations = new MathCalculations();
//parent = (View) view.getRootView();
mathCalculations.addCount();
}
public static void getCount(int count) {
Log.i("addCount", Integer.toString(count));
countString = (Integer.toString(count));
displayString();
}
public static void displayString() {
displayCount.setText("HIE");
}
//...
}
静态变量只会在关闭应用程序后收集垃圾