我尝试从其他活动中调用此活动,但是,强制退出应用程序。 这是我的代码。
public class Records extends Activity {
// Array of strings storing country names
String[] types = new String[] {
"Tutorial Room",
"Lecture Hall",
"Tutorial Room",
"Tutorial Room"
};
// Array of integers points to images stored in /res/drawable-ldpi/
int[] icon = new int[]{
R.drawable.tutorialroom,
R.drawable.lecturehall,
R.drawable.tutorialroom,
R.drawable.tutorialroom
};
// Array of strings to store currencies
String[] info = new String[]{
"Booking1",
"Booking2",
"Booking3",
"Booking4"
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Each row in the list stores country name, currency and flag
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<4;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", "Name : " + types[i]);
hm.put("cur","Type : " + info[i]);
hm.put("flag", Integer.toString(icon[i]) );
aList.add(hm);
}
// Keys used in Hashmap
String[] from = { "flag","txt","cur" };
// Ids of views in listview_layout
int[] to = { R.id.flag,R.id.txt,R.id.cur};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to);
// Getting a reference to listview of main.xml layout file
ListView listView = ( ListView ) findViewById(R.id.listview);
// Setting the adapter to the listView
listView.setAdapter(adapter);
}
08-23 12:19:50.449:E / AndroidRuntime(7642):致命异常:主要 08-23 12:19:50.449:E / AndroidRuntime(7642):处理:my.edu.utar.sofebos,PID:7642 08-23 12:19:50.449:E / AndroidRuntime(7642):java.lang.RuntimeException:无法启动活动ComponentInfo {my.edu.utar.sofebos / my.edu.utar.sofebos.Records}:java.lang 。空指针异常 08-23 12:19:50.449:E / AndroidRuntime(7642):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 08-23 12:19:50.449:E / AndroidRuntime(7642):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 08-23 12:19:50.449:E / AndroidRuntime(7642):在android.app.ActivityThread.access $ 800(ActivityThread.java:135) 08-23 12:19:50.449:E / AndroidRuntime(7642):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196) 08-23 12:19:50.449:E / AndroidRuntime(7642):在android.os.Handler.dispatchMessage(Handler.java:102) 08-23 12:19:50.449:E / AndroidRuntime(7642):在android.os.Looper.loop(Looper.java:136) 08-23 12:19:50.449:E / AndroidRuntime(7642):在android.app.ActivityThread.main(ActivityThread.java:5017) 08-23 12:19:50.449:E / AndroidRuntime(7642):at java.lang.reflect.Method.invokeNative(Native Method) 08-23 12:19:50.449:E / AndroidRuntime(7642):at java.lang.reflect.Method.invoke(Method.java:515) 08-23 12:19:50.449:E / AndroidRuntime(7642):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 08-23 12:19:50.449:E / AndroidRuntime(7642):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 08-23 12:19:50.449:E / AndroidRuntime(7642):at dalvik.system.NativeStart.main(Native Method) 08-23 12:19:50.449:E / AndroidRuntime(7642):引起:java.lang.NullPointerException 08-23 12:19:50.449:E / AndroidRuntime(7642):at my.edu.utar.sofebos.Records.onCreate(Records.java:71) 08-23 12:19:50.449:E / AndroidRuntime(7642):在android.app.Activity.performCreate(Activity.java:5231) 08-23 12:19:50.449:E / AndroidRuntime(7642):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 08-23 12:19:50.449:E / AndroidRuntime(7642):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 08-23 12:19:50.449:E / AndroidRuntime(7642):... 11 more
答案 0 :(得分:0)
您是否试图调用您想要的意图?
以下是制作手机意图的代码:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + phone));
startActivity(callIntent);
在清单文件中添加如下权限:
<uses-permission android:name="android.permission.CALL_PHONE" />
答案 1 :(得分:0)
由于您在以下行中获得java.lang.NullPointerException
:
ListView listView = ( ListView ) findViewById(R.id.listview);
listView.setAdapter(adapter);
main.xml
是否为listview
。确保
它存在。setContentView()