我不熟悉Android编程,但我对Java和C ++非常熟悉。 以下是我的应用程序的Java代码,它适用于吐司,但当我尝试更改文本时,它会导致"您的应用程序已停止工作"。
代码
public class MainActivity extends ActionBarActivity {
int counter;
Button sum, sub;
TextView display;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
display = (TextView) findViewById(R.id.show);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
public void addButtonClicked(View v) {
counter++;
tToast("Your Total Is : " + counter);
display.setText("Your Total Is : " + counter);
}
public void subButtonClicked(View v) {
counter--;
tToast("Your Total Is : " + counter);
display.setText("Your Total Is : " + counter);
}
private void tToast(String s) {
Context context = getApplicationContext();
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, s, duration);
toast.show();
}
}
StackTrace:
07-05 05:22:42.888: E/AndroidRuntime(1650): FATAL EXCEPTION: main
07-05 05:22:42.888: E/AndroidRuntime(1650): Process: com.example.counter, PID: 1650
07-05 05:22:42.888: E/AndroidRuntime(1650): java.lang.IllegalStateException: Could not execute method of the activity
07-05 05:22:42.888: E/AndroidRuntime(1650): at android.view.View$1.onClick(View.java:3823)
07-05 05:22:42.888: E/AndroidRuntime(1650): at android.view.View.performClick(View.java:4438)
07-05 05:22:42.888: E/AndroidRuntime(1650): at android.view.View$PerformClick.run(View.java:18422)
07-05 05:22:42.888: E/AndroidRuntime(1650): at android.os.Handler.handleCallback(Handler.java:733)
07-05 05:22:42.888: E/AndroidRuntime(1650): at android.os.Handler.dispatchMessage(Handler.java:95)
07-05 05:22:42.888: E/AndroidRuntime(1650): at android.os.Looper.loop(Looper.java:136)
07-05 05:22:42.888: E/AndroidRuntime(1650): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-05 05:22:42.888: E/AndroidRuntime(1650): at java.lang.reflect.Method.invokeNative(Native Method)
07-05 05:22:42.888: E/AndroidRuntime(1650): at java.lang.reflect.Method.invoke(Method.java:515)
07-05 05:22:42.888: E/AndroidRuntime(1650): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-05 05:22:42.888: E/AndroidRuntime(1650): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-05 05:22:42.888: E/AndroidRuntime(1650): at dalvik.system.NativeStart.main(Native Method)
07-05 05:22:42.888: E/AndroidRuntime(1650): Caused by: java.lang.reflect.InvocationTargetException
07-05 05:22:42.888: E/AndroidRuntime(1650): at java.lang.reflect.Method.invokeNative(Native Method)
07-05 05:22:42.888: E/AndroidRuntime(1650): at java.lang.reflect.Method.invoke(Method.java:515)
07-05 05:22:42.888: E/AndroidRuntime(1650): at android.view.View$1.onClick(View.java:3818)
07-05 05:22:42.888: E/AndroidRuntime(1650): ... 11 more
07-05 05:22:42.888: E/AndroidRuntime(1650): Caused by: java.lang.NullPointerException
07-05 05:22:42.888: E/AndroidRuntime(1650): at com.example.counter.MainActivity.addButtonClicked(MainActivity.java:104)
07-05 05:22:42.888: E/AndroidRuntime(1650): ... 14 more
答案 0 :(得分:1)
这是因为findViewById()
在activity_main
布局中搜索,而TextView
位于片段的布局fragment_main
中。
在片段的onCreateView()
方法中移动该段代码:
//...
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
display = (TextView)rootView.findViewById(R.id.show);
请注意,现在您可以通过rootView view
访问它:
display = (TextView)rootView.findViewById(R.id.show);
否则你会再次NullPointerException
。