所以我给了自己一个家庭作业:创建一个类做了计算,并且这个变量返回要显示这个TextView怎么可能是空的?
在MainActivity我认为这将是简单的,但由于某种原因,我的TextView总是空。当我尝试在新文件中重新创建错误时,我没有看到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)
你不应该自己创建一个Activity。删除此代码:'MainActivity mainActivity = new MainActivity();' – nandsito
删除后我该怎么办?我想明白为什么我不想做我所做的事......但你的简短回答并不会让我理解。 –