我在Eclipse中运行应用程序时遇到问题。当我运行我的应用程序时,它立即崩溃并说Unfortunately (App name) has stopped.
该应用程序是一个简单的2屏幕票价应用程序。第一页只有一个textview,imageview和按钮。第二页有文本视图,微调器,编辑文本,按钮和另一个textview。它几乎结合了Android Bootcamp书中的第2章和第3章。
这是logcat信息:
02-15 14:31:28.061: D/dalvikvm(536): Not late-enabling CheckJNI (already on)
02-15 14:31:28.851: D/dalvikvm(536): GC_FOR_ALLOC freed 67K, 3% free 9936K/10179K, paused 51ms
02-15 14:31:28.851: I/dalvikvm-heap(536): Grow heap (frag case) to 10.257MB for 494176-byte allocation
02-15 14:31:28.941: D/dalvikvm(536): GC_CONCURRENT freed 1K, 3% free 10417K/10695K, paused 7ms+3ms
02-15 14:31:28.971: D/AndroidRuntime(536): Shutting down VM
02-15 14:31:28.971: W/dalvikvm(536): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
02-15 14:31:28.981: E/AndroidRuntime(536): FATAL EXCEPTION: main
02-15 14:31:28.981: E/AndroidRuntime(536): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.androidbootcamp.racetickets/net.androidbootcamp.racetickets.Main}: java.lang.NullPointerException
02-15 14:31:28.981: E/AndroidRuntime(536): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
02-15 14:31:28.981: E/AndroidRuntime(536): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
02-15 14:31:28.981: E/AndroidRuntime(536): at android.app.ActivityThread.access$600(ActivityThread.java:122)
02-15 14:31:28.981: E/AndroidRuntime(536): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
02-15 14:31:28.981: E/AndroidRuntime(536): at android.os.Handler.dispatchMessage(Handler.java:99)
02-15 14:31:28.981: E/AndroidRuntime(536): at android.os.Looper.loop(Looper.java:137)
02-15 14:31:28.981: E/AndroidRuntime(536): at android.app.ActivityThread.main(ActivityThread.java:4340)
02-15 14:31:28.981: E/AndroidRuntime(536): at java.lang.reflect.Method.invokeNative(Native Method)
02-15 14:31:28.981: E/AndroidRuntime(536): at java.lang.reflect.Method.invoke(Method.java:511)
02-15 14:31:28.981: E/AndroidRuntime(536): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-15 14:31:28.981: E/AndroidRuntime(536): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-15 14:31:28.981: E/AndroidRuntime(536): at dalvik.system.NativeStart.main(Native Method)
02-15 14:31:28.981: E/AndroidRuntime(536): Caused by: java.lang.NullPointerException
02-15 14:31:28.981: E/AndroidRuntime(536): at net.androidbootcamp.racetickets.Main.onCreate(Main.java:31)
02-15 14:31:28.981: E/AndroidRuntime(536): at android.app.Activity.performCreate(Activity.java:4465)
02-15 14:31:28.981: E/AndroidRuntime(536): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-15 14:31:28.981: E/AndroidRuntime(536): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
02-15 14:31:28.981: E/AndroidRuntime(536): ... 11 more
main.xml中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Gateway Raceway"
android:textSize="38sp" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="68dp"
android:src="@drawable/gateway" />
<Button
android:id="@+id/btnTickets"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp"
android:text="Find Tickets"
android:textSize="32sp" />
</RelativeLayout>
tickets.xml(第2页)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Gateway Tickets"
android:textSize="38sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"
android:text="Which Tickets do you want?"
android:textSize="20sp" />
<Spinner
android:id="@+id/txtRacing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView2"
android:layout_marginTop="40dp"
android:entries="@array/Type" />
<Button
android:id="@+id/btnBuy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="59dp"
android:text="Buy Tickets"
android:textSize="25sp" />
<TextView
android:id="@+id/txtPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btnBuy"
android:layout_centerHorizontal="true"
android:layout_marginBottom="36dp"
android:text="results" />
<EditText
android:id="@+id/txtNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtRacing"
android:layout_centerHorizontal="true"
android:layout_marginTop="23dp"
android:ems="10"
android:hint="How many tickets?"
android:inputType="number" >
<requestFocus />
</EditText>
</RelativeLayout>
Main.java
package net.androidbootcamp.racetickets;
import java.text.DecimalFormat;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class Main extends Activity {
double costPerTicket=15.00;
int numberOfTickets;
double totalCost;
String groupChoice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText tickets=(EditText) findViewById(R.id.txtNumber);
final Spinner group = (Spinner) findViewById(R.id.txtRacing);
Button cost = (Button) findViewById(R.id.btnBuy);
final TextView result = ((TextView) findViewById(R.id.txtPrice));
cost.setOnClickListener(new OnClickListener () {
@Override
public void onClick(View v) {
//TODO Auto-generated method stub
numberOfTickets = Integer.parseInt(tickets.getText().toString());
totalCost = costPerTicket + numberOfTickets;
DecimalFormat currency = new DecimalFormat ("###,###.##");
groupChoice = group.getSelectedItem().toString();
result.setText("Total Cost for" + groupChoice + " is " + currency.format(totalCost));
}
});
Button b=(Button)findViewById(R.id.btnTickets);
b.setOnClickListener(new OnClickListener () {
public void onClick(View v) {
//TODO Auto-generated method stub
startActivity(new Intent(Main.this, Tickets.class));
}
});
}
@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;
}
}
Tickets.java
package net.androidbootcamp.racetickets;
import android.app.Activity;
import android.os.Bundle;
public class Tickets extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tickets);
}
}
任何想法?
答案 0 :(得分:0)
这一行
Button cost = (Button) findViewById(R.id.btnBuy);
你在main.java类中调用的将无法正常工作。因为您的按钮是在tickets.xml中声明的
我认为你必须替换
Button cost = (Button) findViewById(R.id.btnBuy);
让它运作
Button cost = (Button) findViewById(R.id.btnTickets);