这是Logcat的一部分: 它说它由什么造成的 MainActivity.java:41) 我检查了ID,它们的完成方式与教程相同。
.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.appt.shreyabisht.test.MainActivity.onCreate(MainActivity.java:41)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread
这是主要的activity.java 包com.appt.shreyabisht.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ExpandableListView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MainActivity extends Activity implements View.OnClickListener {
HashMap <String, List<String>> Services_category;
List<String> Services_list;
ExpandableListView Exp_list;
services_adapter adapter;
Button btn_sign;
Button btn_sign_up;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_sign = (Button) findViewById(R.id.btn_sign);
btn_sign.setOnClickListener(this);
btn_sign_up = (Button)findViewById(R.id.btn_sign_up);
btn_sign_up.setOnClickListener(this);
Exp_list=(ExpandableListView) findViewById(R.id.exp_list);
Services_category = Dataprovider.getinfo();
Services_list = new ArrayList<String>(Services_category.keySet());
adapter = new services_adapter(this, Services_category, Services_list);
Exp_list.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void btclick() {
startActivity(new Intent("com.appt.shreyabisht.second"));
}
private void btsignclick() {
startActivity(new Intent("com.appt.shreyabisht.third"));
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_sign:
btclick();
break;
case R.id.btn_sign_up:
btsignclick();
break;
}
}
}
的Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.appt.shreyabisht.test" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.appt.shreyabisht.test.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.appt.shreyabisht.test.second"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.appt.shreyabisht.second" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.appt.shreyabisht.test.third"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.appt.shreyabisht.third" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.appt.shreyabisht.test.after_signin"
android:label="@string/title_activity_after_signin" >
<intent-filter>
<action android:name="com.appt.shreyabisht.test.after_signin" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
after_signup.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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.appt.shreyabisht.test.after_signin"
android:background="@drawable/download">
<TextView android:text="@string/welcome_header" android:layout_width="wrap_content"
android:textColor="#FFFFDD76"
android:textStyle="bold"
android:textSize="20dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/textView5" />
<ExpandableListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/exp_list"
android:indicatorLeft="?android:attr/expandableListPreferredItemIndicatorLeft"
android:layout_alignParentBottom="true"
android:divider="#A4C739"
android:dividerHeight="0.5dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="@+id/textView5" />
父文件(expand_1.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:id="@+id/parent_id"
android:textColor="#A4C739"/>
子文件(child.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/child"
android:layout_width="match_parent"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_height="match_parent" />
services_adapter.java
package com.appt.shreyabisht.test;
import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.HashMap;
import java.util.List;
/ ** *由SHREYA BISHT于2014年11月19日创建。 * / public class services_adapter扩展了BaseExpandableListAdapter {
private Context ctx;
private HashMap<String, List<String>> Services_category;
private List<String> Services_list;
public services_adapter(Context ctx, HashMap<String, List<String>> Services_category, List<String> Services_list)
{
this.ctx = ctx;
this.Services_category = Services_category;
this.Services_list = Services_list;
}
@Override
public void registerDataSetObserver(DataSetObserver dataSetObserver) {
}
@Override
public void unregisterDataSetObserver(DataSetObserver dataSetObserver) {
}
@Override
public int getGroupCount() {
return Services_list.size();
}
@Override
public int getChildrenCount(int arg0) {
return Services_category.get(Services_list.get(arg0)).size();
}
@Override
public Object getGroup(int i) {
return Services_list.get(i);
}
@Override
public Object getChild(int parent, int child) {
return Services_category.get(Services_list.get(parent)).get(child);
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int parent, int child) {
return child;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int parent , boolean isExpanded, View convertview, ViewGroup parentview) {
String group_title = (String) getGroup(parent);
if(convertview == null)
{
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertview = inflater.inflate(R.layout.expand_1, parentview, false);
}
TextView parent_textview = (TextView) convertview.findViewById(R.id.parent_id);
parent_textview.setTypeface(null, Typeface.BOLD);
parent_textview.setText(group_title);
return convertview;
}
@Override
public View getChildView(int parent, int child, boolean lastchild, View convertview, ViewGroup parentview) {
String child_title = (String) getChild(parent,child);
if(convertview == null)
{
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertview = inflater.inflate(R.layout.expand_child, parentview, false);
}
TextView child_textview = (TextView) convertview.findViewById(R.id.child);
child_textview.setText(child_title);
return convertview;
}
@Override
public boolean isChildSelectable(int i, int i2) {
return false;
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public void onGroupExpanded(int i) {
}
@Override
public void onGroupCollapsed(int i) {
}
@Override
public long getCombinedChildId(long l, long l2) {
return 0;
}
@Override
public long getCombinedGroupId(long l) {
return 0;
}
}
和字符串文件 的strings.xml:
<string name="app_name">test</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="enter_name">Username</string>
<string name="enter_username">Enter</string>
<string name="calling_activity_name">The username and password entered was</string>
<string name="sign_in">Sign In</string>
<string name="user_password">Password</string>
<string name="App_name">StayMax</string>
<string name="user_name">Username:</string>
<string name="details_header">Please provide login details</string>
<string name="hello_blank_fragment">Hello blank fragment</string>
<string name="title_activity_second">second</string>
<string name="title_activity_third">third</string>
<string name="confirm_password">Confirm Password</string>
<string name="enter_location">Enter Location</string>
<string name="sign_up">Sign Up</string>
<string name="sign_up_btn">Sign Up</string>
<string name="title_activity_after_signin">after_signin</string>
<string name="button_login">Log In</string>
<string name="welcome_header">Welcome to Ayaya</string>
建议更改后
logcat:
Process: com.appt.shreyabisht.test, PID: 942
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appt.shreyabisht.test/com.appt.shreyabisht.test.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.appt.shreyabisht.test.MainActivity.onCreate(MainActivity.java:34)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
11-21 06:07:32.739 942-942/com.appt.shreyabisht.test I/Process﹕ Sending signal. PID: 942 SIG: 9
设置时: 的setContentView(R.layout.activity_main);
我得到的错误是:
Caused by: java.lang.NullPointerException
at com.appt.shreyabisht.test.MainActivity.onCreate(MainActivity.java:41)
at android.app.Activity.performCreate(Activity.java:5231)
mainactivity.java:41是:Exp_list.setAdapter(adapter);
设置时:setContentView(R.layout.activity_after_signin); 我得到的错误是:
Caused by: java.lang.NullPointerException
at com.appt.shreyabisht.test.MainActivity.onCreate(MainActivity.java:34)
at android.app.Activity.performCreate(Activity.java:5231)
MainActivity.java:34是:btn_sign.setOnClickListener(this); 这是另一个活动的按钮。
答案 0 :(得分:0)
您正在使用activity_main设置内容视图
setContentView(R.layout.activity_main);
您的activity_main.xml在哪里?你使用正确的xml文件?
我认为您正在引用错误的xml文件,并且您的视图位于另一个xml中。请检查。