我是Android开发新手并尝试构建我的第一个应用程序。我正在尝试构建一个简单的计数器,允许用户将总数增加+1,-1,+ 5,-5,起始值为20.当我尝试运行我的应用程序时,它总是立即崩溃我完全坚持如何解决它。
这是我的代码:
package com.example.mtglifecounter;
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.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
int Total=20;
Button Plus1, Min1, Plus5, Min5;
EditText Display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Total = 20;
Plus1 = (Button) findViewById(R.id.btnPlus1);
Min1 = (Button) findViewById(R.id.btnMin1);
Plus5 = (Button) findViewById(R.id.btnPlus5);
Min5 = (Button) findViewById(R.id.btnmin5);
Display = (EditText) findViewById(R.id.tvTotal);
Display.setText(Total);
Plus1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Adds 1 to the counter
Total = Total + 1;
Display.setText(Total);
}
});
Min1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Subtract 1 from counter
Total = Total - 1;
Display.setText(Total);
}
});
Plus5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Adds 1 to the counter
Total = Total + 5;
Display.setText(Total);
}
});
Min5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Subtract 1 from counter
Total = Total - 5;
Display.setText(Total);
}
});
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;
}
}
}
我还有一个我保存的logcat:
04-11 11:56:27.433: D/AndroidRuntime(1459): Shutting down VM
04-11 11:56:27.433: W/dalvikvm(1459): threadid=1: thread exiting with uncaught exception (group=0xb1aabba8)
04-11 11:56:27.453: E/AndroidRuntime(1459): FATAL EXCEPTION: main
04-11 11:56:27.453: E/AndroidRuntime(1459): Process: com.example.mtglifecounter, PID: 1459
04-11 11:56:27.453: E/AndroidRuntime(1459): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mtglifecounter/com.example.mtglifecounter.MainActivity}: java.lang.NullPointerException
04-11 11:56:27.453: E/AndroidRuntime(1459): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-11 11:56:27.453: E/AndroidRuntime(1459): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-11 11:56:27.453: E/AndroidRuntime(1459): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-11 11:56:27.453: E/AndroidRuntime(1459): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-11 11:56:27.453: E/AndroidRuntime(1459): at android.os.Handler.dispatchMessage(Handler.java:102)
04-11 11:56:27.453: E/AndroidRuntime(1459): at android.os.Looper.loop(Looper.java:136)
04-11 11:56:27.453: E/AndroidRuntime(1459): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-11 11:56:27.453: E/AndroidRuntime(1459): at java.lang.reflect.Method.invokeNative(Native Method)
04-11 11:56:27.453: E/AndroidRuntime(1459): at java.lang.reflect.Method.invoke(Method.java:515)
04-11 11:56:27.453: E/AndroidRuntime(1459): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-11 11:56:27.453: E/AndroidRuntime(1459): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-11 11:56:27.453: E/AndroidRuntime(1459): at dalvik.system.NativeStart.main(Native Method)
04-11 11:56:27.453: E/AndroidRuntime(1459): Caused by: java.lang.NullPointerException
04-11 11:56:27.453: E/AndroidRuntime(1459): at com.example.mtglifecounter.MainActivity.onCreate(MainActivity.java:34)
04-11 11:56:27.453: E/AndroidRuntime(1459): at android.app.Activity.performCreate(Activity.java:5231)
04-11 11:56:27.453: E/AndroidRuntime(1459): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-11 11:56:27.453: E/AndroidRuntime(1459): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-11 11:56:27.453: E/AndroidRuntime(1459): ... 11 more
04-11 11:56:33.373: I/Process(1459): Sending signal. PID: 1459 SIG: 9
我非常感谢你能给我的任何帮助,因为这是我第一次尝试使用Android。
由于
这是activity_Main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mtglifecounter.MainActivity"
tools:ignore="MergeRootFrame" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mtglifecounter.MainActivity$PlaceholderFragment" >
<EditText
android:id="@+id/etTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/LifeTotal"
android:ems="10"
android:inputType="number"
android:text="20" />
<Button
android:id="@+id/btnmin5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btnPlus5"
android:layout_alignBottom="@+id/btnPlus5"
android:layout_alignLeft="@+id/btnMin1"
android:onClick="On_Clicked"
android:text="-5" />
<Button
android:id="@+id/btnMin1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btnPlus1"
android:layout_alignBottom="@+id/btnPlus1"
android:layout_alignLeft="@+id/LifeTotal"
android:layout_marginLeft="37dp"
android:onClick="On_Clicked"
android:text="-1" />
<Button
android:id="@+id/btnPlus5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/tvTotal"
android:layout_below="@+id/btnPlus1"
android:layout_marginRight="60dp"
android:layout_marginTop="25dp"
android:onClick="On_Clicked"
android:text="+5" />
<Button
android:id="@+id/btnPlus1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btnPlus5"
android:layout_below="@+id/LifeTotal"
android:layout_marginTop="44dp"
android:onClick="On_Clicked"
android:text="+1" />
<TextView
android:id="@+id/LifeTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/tvTotal"
android:layout_alignParentLeft="true"
android:layout_marginLeft="21dp"
android:text="Life Total:"
android:textSize="@dimen/abc_action_bar_title_text_size" />
答案 0 :(得分:1)
根据您的代码和布局,问题在于 EditText ID。
您正尝试使用按钮 ID
找到 EditText 视图根据您的布局更改此行
Display = (EditText) findViewById(R.id.tvTotal);
到
Display = (EditText) findViewById(R.id.etTotal);
另外,另一个问题是 setText 到 EditText
Display.setText(Total);
总计是 int 。它应该是字符串或应该是有效的字符串资源ID
如果您要设置整数,则会将其视为资源ID
这就是setText方法的作用......
public final void setText(int resid) {
setText(getContext().getResources().getText(resid));
}
或者,更改int以传递正确的字符串资源ID 或将int转换为字符串,如下所示
Display.setText(Integer.toString(Total));
答案 1 :(得分:0)
您应该使用调试器。在onCreate方法中逐步执行每一行,直到看到错误的值 - 可能是空指针。您没有说明如何构建应用程序,但使用Eclipse或Android Studio允许逐行调试步进。使用此功能,您可以快速轻松地找到您的错误。
https://developer.android.com/tools/debugging/debugging-projects.html
答案 2 :(得分:0)
确保所有这些都存在于您的布局activity_mail.xml
Plus1 = (Button) findViewById(R.id.btnPlus1);
Min1 = (Button) findViewById(R.id.btnMin1);
Plus5 = (Button) findViewById(R.id.btnPlus5);
Min5 = (Button) findViewById(R.id.btnmin5);
Display = (EditText) findViewById(R.id.tvTotal);
我的意思是,检查是否有Button
ID为btnPlus1
,然后检查是否有Button
ID btnMin1
,依此类推。
由于异常是NullPointerException
,问题可能是您错误输入了ID并且其中一个(或多个)正在返回null
。