我一直在为Android测试一个简单的计算器应用程序但是当我使用按钮OnClick监听器时它会继续崩溃。我尝试使用android:OnClick =“Add”并且它正常工作,但前提是我把所有的txtA =(EditText)都设为findViewById(R.id.txtA);在“添加”子程序中。我真的想使用OnClick监听器,所以任何帮助将不胜感激。这是我的代码:
package com.example.calculadora;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Build;
public class Main extends ActionBarActivity {
private EditText txtA;
private EditText txtB;
private TextView lblSuma;
private Button btnsumar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
txtA = (EditText) findViewById(R.id.txtA);
txtB = (EditText) findViewById(R.id.txtB);
lblSuma = (TextView) findViewById(R.id.lblSuma);
btnsumar = (Button) findViewById(R.id.btnSuma);
btnsumar.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
//float A = Float.parseFloat(txtA.getText().toString());
//float B = Float.parseFloat(txtB.getText().toString());
//float Suma = A + B;
//lblSuma.setText(""+Suma);
}
});
}
@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;
}
}
}
这是LogCat:
04-13 10:48:17.966: W/ApplicationPackageManager(26281): getCSCPackageItemText()
04-13 10:48:18.021: D/AndroidRuntime(26281): Shutting down VM
04-13 10:48:18.021: W/dalvikvm(26281): threadid=1: thread exiting with uncaught exception (group=0x4180ac08)
04-13 10:48:18.021: E/AndroidRuntime(26281): FATAL EXCEPTION: main
04-13 10:48:18.021: E/AndroidRuntime(26281): Process: com.example.calculadora, PID: 26281
04-13 10:48:18.021: E/AndroidRuntime(26281): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calculadora/com.example.calculadora.Main}: java.lang.NullPointerException
04-13 10:48:18.021: E/AndroidRuntime(26281): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
04-13 10:48:18.021: E/AndroidRuntime(26281): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
04-13 10:48:18.021: E/AndroidRuntime(26281): at android.app.ActivityThread.access$900(ActivityThread.java:161)
04-13 10:48:18.021: E/AndroidRuntime(26281): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
04-13 10:48:18.021: E/AndroidRuntime(26281): at android.os.Handler.dispatchMessage(Handler.java:102)
04-13 10:48:18.021: E/AndroidRuntime(26281): at android.os.Looper.loop(Looper.java:157)
04-13 10:48:18.021: E/AndroidRuntime(26281): at android.app.ActivityThread.main(ActivityThread.java:5356)
04-13 10:48:18.021: E/AndroidRuntime(26281): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 10:48:18.021: E/AndroidRuntime(26281): at java.lang.reflect.Method.invoke(Method.java:515)
04-13 10:48:18.021: E/AndroidRuntime(26281): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
04-13 10:48:18.021: E/AndroidRuntime(26281): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
04-13 10:48:18.021: E/AndroidRuntime(26281): at dalvik.system.NativeStart.main(Native Method)
04-13 10:48:18.021: E/AndroidRuntime(26281): Caused by: java.lang.NullPointerException
04-13 10:48:18.021: E/AndroidRuntime(26281): at com.example.calculadora.Main.onCreate(Main.java:39)
04-13 10:48:18.021: E/AndroidRuntime(26281): at android.app.Activity.performCreate(Activity.java:5426)
04-13 10:48:18.021: E/AndroidRuntime(26281): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
04-13 10:48:18.021: E/AndroidRuntime(26281): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
04-13 10:48:18.021: E/AndroidRuntime(26281): ... 11 more
答案 0 :(得分:3)
试试这个..
我猜Button
EditText
TextView
属于fragment_main.xml
,请尝试如下。
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);
txtA = (EditText) rootView.findViewById(R.id.txtA);
txtB = (EditText) rootView.findViewById(R.id.txtB);
lblSuma = (TextView) rootView.findViewById(R.id.lblSuma);
btnsumar = (Button) rootView.findViewById(R.id.btnSuma);
btnsumar.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
}
});
return rootView;
}
}
答案 1 :(得分:0)
问题在于您的onclick代码。将其更改为以下内容:
btnsumar .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});