在 MyAdapter.getView 获取 Nullpointer,如何修复?
Getting Nullpointer at MyAdapter.getView,how to fix?
我想在导航抽屉中添加图片,我把它放在 drawable 文件夹中,大小为 48*48,我的字符串数组字符串数组在 string.xml,
我遵循了一个教程,但最后我所做的更改导致我的应用程序崩溃,我无法弄清楚问题是什么以及如何解决?
这是例外情况
01-23 19:32:41.277: E/AndroidRuntime(16889): java.lang.NullPointerException
01-23 19:32:41.277: E/AndroidRuntime(16889): at com.pKLabs.RgpvDigest.MyAdapter.getView(MainActivity.java:339)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.widget.AbsListView.obtainView(AbsListView.java:2733)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.widget.ListView.makeAndAddView(ListView.java:1811)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.widget.ListView.fillDown(ListView.java:697)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.widget.ListView.fillFromTop(ListView.java:763)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.widget.ListView.layoutChildren(ListView.java:1641)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.widget.AbsListView.onLayout(AbsListView.java:2546)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.View.layout(View.java:15664)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.ViewGroup.layout(ViewGroup.java:4869)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:914)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.View.layout(View.java:15664)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.ViewGroup.layout(ViewGroup.java:4869)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.View.layout(View.java:15664)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.ViewGroup.layout(ViewGroup.java:4869)
01-23 19:32:41.277: E/AndroidRuntime(16889): at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:429)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.View.layout(View.java:15664)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.ViewGroup.layout(ViewGroup.java:4869)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.View.layout(View.java:15664)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.ViewGroup.layout(ViewGroup.java:4869)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2246)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1968)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1200)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6401)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.Choreographer.doCallbacks(Choreographer.java:603)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.Choreographer.doFrame(Choreographer.java:573)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.os.Handler.handleCallback(Handler.java:733)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.os.Handler.dispatchMessage(Handler.java:95)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.os.Looper.loop(Looper.java:157)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.app.ActivityThread.main(ActivityThread.java:5335)
01-23 19:32:41.277: E/AndroidRuntime(16889): at java.lang.reflect.Method.invokeNative(Native Method)
01-23 19:32:41.277: E/AndroidRuntime(16889): at java.lang.reflect.Method.invoke(Method.java:515)
01-23 19:32:41.277: E/AndroidRuntime(16889): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
01-23 19:32:41.277: E/AndroidRuntime(16889): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
01-23 19:32:41.277: E/AndroidRuntime(16889): at dalvik.system.NativeStart.main(Native Method)
MainActivity
public class MainActivity extends ActionBarActivity implements
OnItemClickListener {
private DrawerLayout drawerLayout;
private ListView listView;
// private String[] navdra;
private ActionBarDrawerToggle drawerListner;
private MyAdapter myAdapter;
Intent shareintent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// navdra = getResources().getStringArray(R.array.navdra);
listView = (ListView) findViewById(R.id.drawerlist);
myAdapter = new MyAdapter(this);
// listView.setAdapter(new ArrayAdapter<String>(this,
// android.R.layout.simple_expandable_list_item_1, navdra));
listView.setAdapter(myAdapter);
listView.setOnItemClickListener(this);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerListner = new ActionBarDrawerToggle(this, drawerLayout,
R.drawable.ic_drawer, R.string.dopen, R.string.dclose);
/*
* @Override public void onDrawerOpened(View drawerView) {
* Toast.makeText(MainActivity.this, "Open", Toast.LENGTH_SHORT)
* .show(); }
*
* @Override public void onDrawerClosed(View drawerView) {
* Toast.makeText(MainActivity.this, "Close", Toast.LENGTH_SHORT)
* .show(); } }
*/
drawerLayout.setDrawerListener(drawerListner);
getSupportActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
Button csv = (Button) findViewById(R.id.cs);
csv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), Semester.class);
startActivityForResult(intent, 0);
}
});
Button cal = (Button) findViewById(R.id.cal);
cal.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), Calculator.class);
startActivityForResult(intent, 0);
}
});
Button iq = (Button) findViewById(R.id.interviewquestion);
iq.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(),
InterviewQuestion.class);
startActivityForResult(intent, 0);
}
});
Button cg = (Button) findViewById(R.id.program);
cg.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(v.getContext(), Programs.class);
startActivityForResult(intent, 0);
}
});
Button abt = (Button) findViewById(R.id.about);
abt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "PKlabs!!!",
Toast.LENGTH_SHORT).show();
// Intent intent = new Intent
// (v.getContext(),InterviewQuestion.class);
// startActivityForResult(intent,0);
}
});
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
drawerListner.onConfigurationChanged(newConfig);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onPostCreate(savedInstanceState);
drawerListner.syncState();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.add("share"); // your desired title here
item.setIcon(R.drawable.share_icon); // your desired icon here
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
shareintent = new Intent(android.content.Intent.ACTION_SEND);
shareintent.setType("text/plain");
String shareBody = "Here is the share content body";
shareintent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"Subject Here");
shareintent.putExtra(Intent.EXTRA_TEXT, "Hi there");
startActivity(Intent.createChooser(shareintent, "Share via"));
return true;
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if (drawerListner.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if (position == 0) {
Uri uri = Uri
.parse("https://2ce74af0d8de8783b91cdd315eeba0340a4b9277.googledrive.com/host/0B2sDUzNeIK0KODB0ZDRMVjFPaFE/Jobs.html");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
} else if (position == 1) {
Intent intent = new Intent(this, Careerguid.class);
startActivityForResult(intent, 0);
} else if (position == 2) {
Toast.makeText(this, "Coming Soon!!!", Toast.LENGTH_LONG).show();
}
// }
else if (position == 3) {
Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
else if (position == 4) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
final PackageManager pm = getPackageManager();
final List<ResolveInfo> matches = pm.queryIntentActivities(intent,
0);
ResolveInfo best = null;
for (final ResolveInfo info : matches)
if (info.activityInfo.packageName.endsWith(".gm")
|| info.activityInfo.name.toLowerCase().contains(
"gmail"))
best = info;
if (best != null)
intent.setClassName(best.activityInfo.packageName,
best.activityInfo.name);
intent.putExtra(Intent.EXTRA_EMAIL,
new String[] { "laboratorypk@gmail.com" });
intent.putExtra(Intent.EXTRA_SUBJECT, "Query/Feedback");
intent.putExtra(Intent.EXTRA_TEXT, "");
startActivity(Intent.createChooser(intent, ""));
}
if (position == 5) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
final PackageManager pm = getPackageManager();
final List<ResolveInfo> matches = pm.queryIntentActivities(intent,
0);
ResolveInfo best = null;
for (final ResolveInfo info : matches)
if (info.activityInfo.packageName.endsWith(".gm")
|| info.activityInfo.name.toLowerCase().contains(
"gmail"))
best = info;
if (best != null)
intent.setClassName(best.activityInfo.packageName,
best.activityInfo.name);
intent.putExtra(Intent.EXTRA_EMAIL,
new String[] { "laboratorypk@gmail.com" });
intent.putExtra(Intent.EXTRA_SUBJECT, "PC Games");
intent.putExtra(Intent.EXTRA_TEXT,
"Hey! I need PC Games pls share the List.");
startActivity(Intent.createChooser(intent, ""));
}
drawerLayout.closeDrawers();
selectItem(position);
}
public void selectItem(int position) {
// TODO Auto-generated method stub
listView.setItemChecked(position, true);
}
public void setTitle(String title) {
getSupportActionBar().setTitle(title);
}
private Toast toast;
private long lastBackPressTime = 0;
@Override
public void onBackPressed() {
if (this.lastBackPressTime < System.currentTimeMillis() - 4000) {
toast = Toast.makeText(this, "Press back again to exit",
Toast.LENGTH_LONG);
toast.show();
this.lastBackPressTime = System.currentTimeMillis();
drawerLayout.closeDrawers();
} else {
if (toast != null) {
toast.cancel();
}
super.onBackPressed();
}
}
}
class MyAdapter extends BaseAdapter {
private Context context;
String[] navlist;
int[] images = { R.drawable.job, R.drawable.career, R.drawable.project,
R.drawable.facebook, R.drawable.mail, R.drawable.games };
public MyAdapter(Context context) {
this.context = context;
navlist = context.getResources().getStringArray(R.array.navdra);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return navlist.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return navlist[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.customrow, parent, false);
}
else {
row = convertView;
}
TextView titleTextView = (TextView) row.findViewById(R.id.textView11);
ImageView titleImageView = (ImageView) row
.findViewById(R.id.imageView1);
titleTextView.setText(navlist[position]);
titleImageView.setImageResource(images[position]);
return row;
}
}
customrow.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"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
/>
<TextView
android:layout_margin="8dp"
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
/>
</LinearLayout>
row 为 null,将 inflate 的 return 值赋给 row 变量。
row = inflater.inflate(R.layout.customrow, parent, false);
只需复制粘贴即可。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// navdra = getResources().getStringArray(R.array.navdra);
listView = (ListView) findViewById(R.id.drawerlist);
myAdapter = new MyAdapter(this);
// listView.setAdapter(new ArrayAdapter<String>(this,
// android.R.layout.simple_expandable_list_item_1, navdra));
listView.setAdapter(myAdapter);
listView.setOnItemClickListener(this);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerListner = new ActionBarDrawerToggle(this, drawerLayout,
R.drawable.ic_drawer, R.string.dopen, R.string.dclose);
/*
* @Override public void onDrawerOpened(View drawerView) {
* Toast.makeText(MainActivity.this, "Open", Toast.LENGTH_SHORT)
* .show(); }
*
* @Override public void onDrawerClosed(View drawerView) {
* Toast.makeText(MainActivity.this, "Close", Toast.LENGTH_SHORT)
* .show(); } }
*/
drawerLayout.setDrawerListener(drawerListner);
getSupportActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
Button csv = (Button) findViewById(R.id.cs);
csv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), Semester.class);
startActivityForResult(intent, 0);
}
});
Button cal = (Button) findViewById(R.id.cal);
cal.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), Calculator.class);
startActivityForResult(intent, 0);
}
});
Button iq = (Button) findViewById(R.id.interviewquestion);
iq.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(),
InterviewQuestion.class);
startActivityForResult(intent, 0);
}
});
Button cg = (Button) findViewById(R.id.program);
cg.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(v.getContext(), Programs.class);
startActivityForResult(intent, 0);
}
});
Button abt = (Button) findViewById(R.id.about);
abt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "PKlabs!!!",
Toast.LENGTH_SHORT).show();
// Intent intent = new Intent
// (v.getContext(),InterviewQuestion.class);
// startActivityForResult(intent,0);
}
});
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
drawerListner.onConfigurationChanged(newConfig);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onPostCreate(savedInstanceState);
drawerListner.syncState();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.add("share"); // your desired title here
item.setIcon(R.drawable.share_icon); // your desired icon here
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
shareintent = new Intent(android.content.Intent.ACTION_SEND);
shareintent.setType("text/plain");
String shareBody = "Here is the share content body";
shareintent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"Subject Here");
shareintent.putExtra(Intent.EXTRA_TEXT, "Hi there");
startActivity(Intent.createChooser(shareintent, "Share via"));
return true;
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if (drawerListner.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if (position == 0) {
Uri uri = Uri
.parse("https://2ce74af0d8de8783b91cdd315eeba0340a4b9277.googledrive.com/host/0B2sDUzNeIK0KODB0ZDRMVjFPaFE/Jobs.html");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
} else if (position == 1) {
Intent intent = new Intent(this, Careerguid.class);
startActivityForResult(intent, 0);
} else if (position == 2) {
Toast.makeText(this, "Coming Soon!!!", Toast.LENGTH_LONG).show();
}
// }
else if (position == 3) {
Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
else if (position == 4) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
final PackageManager pm = getPackageManager();
final List<ResolveInfo> matches = pm.queryIntentActivities(intent,
0);
ResolveInfo best = null;
for (final ResolveInfo info : matches)
if (info.activityInfo.packageName.endsWith(".gm")
|| info.activityInfo.name.toLowerCase().contains(
"gmail"))
best = info;
if (best != null)
intent.setClassName(best.activityInfo.packageName,
best.activityInfo.name);
intent.putExtra(Intent.EXTRA_EMAIL,
new String[] { "laboratorypk@gmail.com" });
intent.putExtra(Intent.EXTRA_SUBJECT, "Query/Feedback");
intent.putExtra(Intent.EXTRA_TEXT, "");
startActivity(Intent.createChooser(intent, ""));
}
if (position == 5) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
final PackageManager pm = getPackageManager();
final List<ResolveInfo> matches = pm.queryIntentActivities(intent,
0);
ResolveInfo best = null;
for (final ResolveInfo info : matches)
if (info.activityInfo.packageName.endsWith(".gm")
|| info.activityInfo.name.toLowerCase().contains(
"gmail"))
best = info;
if (best != null)
intent.setClassName(best.activityInfo.packageName,
best.activityInfo.name);
intent.putExtra(Intent.EXTRA_EMAIL,
new String[] { "laboratorypk@gmail.com" });
intent.putExtra(Intent.EXTRA_SUBJECT, "PC Games");
intent.putExtra(Intent.EXTRA_TEXT,
"Hey! I need PC Games pls share the List.");
startActivity(Intent.createChooser(intent, ""));
}
drawerLayout.closeDrawers();
selectItem(position);
}
public void selectItem(int position) {
// TODO Auto-generated method stub
listView.setItemChecked(position, true);
}
public void setTitle(String title) {
getSupportActionBar().setTitle(title);
}
private Toast toast;
private long lastBackPressTime = 0;
@Override
public void onBackPressed() {
if (this.lastBackPressTime < System.currentTimeMillis() - 4000) {
toast = Toast.makeText(this, "Press back again to exit",
Toast.LENGTH_LONG);
toast.show();
this.lastBackPressTime = System.currentTimeMillis();
drawerLayout.closeDrawers();
} else {
if (toast != null) {
toast.cancel();
}
super.onBackPressed();
}
}
}
class MyAdapter extends BaseAdapter {
private Context context;
String[] navlist;
int[] images = { R.drawable.job, R.drawable.career, R.drawable.project,
R.drawable.facebook, R.drawable.mail, R.drawable.games };
public MyAdapter(Context context) {
this.context = context;
navlist = context.getResources().getStringArray(R.array.navdra);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return navlist.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return navlist[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.customrow, parent, false);
}
else {
row = convertView;
}
TextView titleTextView = (TextView) row.findViewById(R.id.textView11);
ImageView titleImageView = (ImageView) row
.findViewById(R.id.imageView1);
titleTextView.setText(navlist[position]);
titleImageView.setImageResource(images[position]);
return row;
}
}
我想在导航抽屉中添加图片,我把它放在 drawable 文件夹中,大小为 48*48,我的字符串数组字符串数组在 string.xml,
我遵循了一个教程,但最后我所做的更改导致我的应用程序崩溃,我无法弄清楚问题是什么以及如何解决?
这是例外情况
01-23 19:32:41.277: E/AndroidRuntime(16889): java.lang.NullPointerException
01-23 19:32:41.277: E/AndroidRuntime(16889): at com.pKLabs.RgpvDigest.MyAdapter.getView(MainActivity.java:339)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.widget.AbsListView.obtainView(AbsListView.java:2733)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.widget.ListView.makeAndAddView(ListView.java:1811)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.widget.ListView.fillDown(ListView.java:697)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.widget.ListView.fillFromTop(ListView.java:763)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.widget.ListView.layoutChildren(ListView.java:1641)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.widget.AbsListView.onLayout(AbsListView.java:2546)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.View.layout(View.java:15664)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.ViewGroup.layout(ViewGroup.java:4869)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:914)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.View.layout(View.java:15664)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.ViewGroup.layout(ViewGroup.java:4869)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.View.layout(View.java:15664)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.ViewGroup.layout(ViewGroup.java:4869)
01-23 19:32:41.277: E/AndroidRuntime(16889): at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:429)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.View.layout(View.java:15664)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.ViewGroup.layout(ViewGroup.java:4869)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.View.layout(View.java:15664)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.ViewGroup.layout(ViewGroup.java:4869)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2246)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1968)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1200)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6401)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.Choreographer.doCallbacks(Choreographer.java:603)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.Choreographer.doFrame(Choreographer.java:573)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.os.Handler.handleCallback(Handler.java:733)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.os.Handler.dispatchMessage(Handler.java:95)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.os.Looper.loop(Looper.java:157)
01-23 19:32:41.277: E/AndroidRuntime(16889): at android.app.ActivityThread.main(ActivityThread.java:5335)
01-23 19:32:41.277: E/AndroidRuntime(16889): at java.lang.reflect.Method.invokeNative(Native Method)
01-23 19:32:41.277: E/AndroidRuntime(16889): at java.lang.reflect.Method.invoke(Method.java:515)
01-23 19:32:41.277: E/AndroidRuntime(16889): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
01-23 19:32:41.277: E/AndroidRuntime(16889): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
01-23 19:32:41.277: E/AndroidRuntime(16889): at dalvik.system.NativeStart.main(Native Method)
MainActivity
public class MainActivity extends ActionBarActivity implements
OnItemClickListener {
private DrawerLayout drawerLayout;
private ListView listView;
// private String[] navdra;
private ActionBarDrawerToggle drawerListner;
private MyAdapter myAdapter;
Intent shareintent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// navdra = getResources().getStringArray(R.array.navdra);
listView = (ListView) findViewById(R.id.drawerlist);
myAdapter = new MyAdapter(this);
// listView.setAdapter(new ArrayAdapter<String>(this,
// android.R.layout.simple_expandable_list_item_1, navdra));
listView.setAdapter(myAdapter);
listView.setOnItemClickListener(this);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerListner = new ActionBarDrawerToggle(this, drawerLayout,
R.drawable.ic_drawer, R.string.dopen, R.string.dclose);
/*
* @Override public void onDrawerOpened(View drawerView) {
* Toast.makeText(MainActivity.this, "Open", Toast.LENGTH_SHORT)
* .show(); }
*
* @Override public void onDrawerClosed(View drawerView) {
* Toast.makeText(MainActivity.this, "Close", Toast.LENGTH_SHORT)
* .show(); } }
*/
drawerLayout.setDrawerListener(drawerListner);
getSupportActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
Button csv = (Button) findViewById(R.id.cs);
csv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), Semester.class);
startActivityForResult(intent, 0);
}
});
Button cal = (Button) findViewById(R.id.cal);
cal.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), Calculator.class);
startActivityForResult(intent, 0);
}
});
Button iq = (Button) findViewById(R.id.interviewquestion);
iq.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(),
InterviewQuestion.class);
startActivityForResult(intent, 0);
}
});
Button cg = (Button) findViewById(R.id.program);
cg.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(v.getContext(), Programs.class);
startActivityForResult(intent, 0);
}
});
Button abt = (Button) findViewById(R.id.about);
abt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "PKlabs!!!",
Toast.LENGTH_SHORT).show();
// Intent intent = new Intent
// (v.getContext(),InterviewQuestion.class);
// startActivityForResult(intent,0);
}
});
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
drawerListner.onConfigurationChanged(newConfig);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onPostCreate(savedInstanceState);
drawerListner.syncState();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.add("share"); // your desired title here
item.setIcon(R.drawable.share_icon); // your desired icon here
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
shareintent = new Intent(android.content.Intent.ACTION_SEND);
shareintent.setType("text/plain");
String shareBody = "Here is the share content body";
shareintent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"Subject Here");
shareintent.putExtra(Intent.EXTRA_TEXT, "Hi there");
startActivity(Intent.createChooser(shareintent, "Share via"));
return true;
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if (drawerListner.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if (position == 0) {
Uri uri = Uri
.parse("https://2ce74af0d8de8783b91cdd315eeba0340a4b9277.googledrive.com/host/0B2sDUzNeIK0KODB0ZDRMVjFPaFE/Jobs.html");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
} else if (position == 1) {
Intent intent = new Intent(this, Careerguid.class);
startActivityForResult(intent, 0);
} else if (position == 2) {
Toast.makeText(this, "Coming Soon!!!", Toast.LENGTH_LONG).show();
}
// }
else if (position == 3) {
Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
else if (position == 4) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
final PackageManager pm = getPackageManager();
final List<ResolveInfo> matches = pm.queryIntentActivities(intent,
0);
ResolveInfo best = null;
for (final ResolveInfo info : matches)
if (info.activityInfo.packageName.endsWith(".gm")
|| info.activityInfo.name.toLowerCase().contains(
"gmail"))
best = info;
if (best != null)
intent.setClassName(best.activityInfo.packageName,
best.activityInfo.name);
intent.putExtra(Intent.EXTRA_EMAIL,
new String[] { "laboratorypk@gmail.com" });
intent.putExtra(Intent.EXTRA_SUBJECT, "Query/Feedback");
intent.putExtra(Intent.EXTRA_TEXT, "");
startActivity(Intent.createChooser(intent, ""));
}
if (position == 5) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
final PackageManager pm = getPackageManager();
final List<ResolveInfo> matches = pm.queryIntentActivities(intent,
0);
ResolveInfo best = null;
for (final ResolveInfo info : matches)
if (info.activityInfo.packageName.endsWith(".gm")
|| info.activityInfo.name.toLowerCase().contains(
"gmail"))
best = info;
if (best != null)
intent.setClassName(best.activityInfo.packageName,
best.activityInfo.name);
intent.putExtra(Intent.EXTRA_EMAIL,
new String[] { "laboratorypk@gmail.com" });
intent.putExtra(Intent.EXTRA_SUBJECT, "PC Games");
intent.putExtra(Intent.EXTRA_TEXT,
"Hey! I need PC Games pls share the List.");
startActivity(Intent.createChooser(intent, ""));
}
drawerLayout.closeDrawers();
selectItem(position);
}
public void selectItem(int position) {
// TODO Auto-generated method stub
listView.setItemChecked(position, true);
}
public void setTitle(String title) {
getSupportActionBar().setTitle(title);
}
private Toast toast;
private long lastBackPressTime = 0;
@Override
public void onBackPressed() {
if (this.lastBackPressTime < System.currentTimeMillis() - 4000) {
toast = Toast.makeText(this, "Press back again to exit",
Toast.LENGTH_LONG);
toast.show();
this.lastBackPressTime = System.currentTimeMillis();
drawerLayout.closeDrawers();
} else {
if (toast != null) {
toast.cancel();
}
super.onBackPressed();
}
}
}
class MyAdapter extends BaseAdapter {
private Context context;
String[] navlist;
int[] images = { R.drawable.job, R.drawable.career, R.drawable.project,
R.drawable.facebook, R.drawable.mail, R.drawable.games };
public MyAdapter(Context context) {
this.context = context;
navlist = context.getResources().getStringArray(R.array.navdra);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return navlist.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return navlist[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.customrow, parent, false);
}
else {
row = convertView;
}
TextView titleTextView = (TextView) row.findViewById(R.id.textView11);
ImageView titleImageView = (ImageView) row
.findViewById(R.id.imageView1);
titleTextView.setText(navlist[position]);
titleImageView.setImageResource(images[position]);
return row;
}
}
customrow.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"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
/>
<TextView
android:layout_margin="8dp"
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:gravity="center"
/>
</LinearLayout>
row 为 null,将 inflate 的 return 值赋给 row 变量。
row = inflater.inflate(R.layout.customrow, parent, false);
只需复制粘贴即可。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// navdra = getResources().getStringArray(R.array.navdra);
listView = (ListView) findViewById(R.id.drawerlist);
myAdapter = new MyAdapter(this);
// listView.setAdapter(new ArrayAdapter<String>(this,
// android.R.layout.simple_expandable_list_item_1, navdra));
listView.setAdapter(myAdapter);
listView.setOnItemClickListener(this);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerListner = new ActionBarDrawerToggle(this, drawerLayout,
R.drawable.ic_drawer, R.string.dopen, R.string.dclose);
/*
* @Override public void onDrawerOpened(View drawerView) {
* Toast.makeText(MainActivity.this, "Open", Toast.LENGTH_SHORT)
* .show(); }
*
* @Override public void onDrawerClosed(View drawerView) {
* Toast.makeText(MainActivity.this, "Close", Toast.LENGTH_SHORT)
* .show(); } }
*/
drawerLayout.setDrawerListener(drawerListner);
getSupportActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
Button csv = (Button) findViewById(R.id.cs);
csv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), Semester.class);
startActivityForResult(intent, 0);
}
});
Button cal = (Button) findViewById(R.id.cal);
cal.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), Calculator.class);
startActivityForResult(intent, 0);
}
});
Button iq = (Button) findViewById(R.id.interviewquestion);
iq.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(),
InterviewQuestion.class);
startActivityForResult(intent, 0);
}
});
Button cg = (Button) findViewById(R.id.program);
cg.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(v.getContext(), Programs.class);
startActivityForResult(intent, 0);
}
});
Button abt = (Button) findViewById(R.id.about);
abt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "PKlabs!!!",
Toast.LENGTH_SHORT).show();
// Intent intent = new Intent
// (v.getContext(),InterviewQuestion.class);
// startActivityForResult(intent,0);
}
});
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
drawerListner.onConfigurationChanged(newConfig);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onPostCreate(savedInstanceState);
drawerListner.syncState();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem item = menu.add("share"); // your desired title here
item.setIcon(R.drawable.share_icon); // your desired icon here
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
shareintent = new Intent(android.content.Intent.ACTION_SEND);
shareintent.setType("text/plain");
String shareBody = "Here is the share content body";
shareintent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"Subject Here");
shareintent.putExtra(Intent.EXTRA_TEXT, "Hi there");
startActivity(Intent.createChooser(shareintent, "Share via"));
return true;
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if (drawerListner.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if (position == 0) {
Uri uri = Uri
.parse("https://2ce74af0d8de8783b91cdd315eeba0340a4b9277.googledrive.com/host/0B2sDUzNeIK0KODB0ZDRMVjFPaFE/Jobs.html");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
} else if (position == 1) {
Intent intent = new Intent(this, Careerguid.class);
startActivityForResult(intent, 0);
} else if (position == 2) {
Toast.makeText(this, "Coming Soon!!!", Toast.LENGTH_LONG).show();
}
// }
else if (position == 3) {
Uri uri = Uri.parse("http://www.google.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
else if (position == 4) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
final PackageManager pm = getPackageManager();
final List<ResolveInfo> matches = pm.queryIntentActivities(intent,
0);
ResolveInfo best = null;
for (final ResolveInfo info : matches)
if (info.activityInfo.packageName.endsWith(".gm")
|| info.activityInfo.name.toLowerCase().contains(
"gmail"))
best = info;
if (best != null)
intent.setClassName(best.activityInfo.packageName,
best.activityInfo.name);
intent.putExtra(Intent.EXTRA_EMAIL,
new String[] { "laboratorypk@gmail.com" });
intent.putExtra(Intent.EXTRA_SUBJECT, "Query/Feedback");
intent.putExtra(Intent.EXTRA_TEXT, "");
startActivity(Intent.createChooser(intent, ""));
}
if (position == 5) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
final PackageManager pm = getPackageManager();
final List<ResolveInfo> matches = pm.queryIntentActivities(intent,
0);
ResolveInfo best = null;
for (final ResolveInfo info : matches)
if (info.activityInfo.packageName.endsWith(".gm")
|| info.activityInfo.name.toLowerCase().contains(
"gmail"))
best = info;
if (best != null)
intent.setClassName(best.activityInfo.packageName,
best.activityInfo.name);
intent.putExtra(Intent.EXTRA_EMAIL,
new String[] { "laboratorypk@gmail.com" });
intent.putExtra(Intent.EXTRA_SUBJECT, "PC Games");
intent.putExtra(Intent.EXTRA_TEXT,
"Hey! I need PC Games pls share the List.");
startActivity(Intent.createChooser(intent, ""));
}
drawerLayout.closeDrawers();
selectItem(position);
}
public void selectItem(int position) {
// TODO Auto-generated method stub
listView.setItemChecked(position, true);
}
public void setTitle(String title) {
getSupportActionBar().setTitle(title);
}
private Toast toast;
private long lastBackPressTime = 0;
@Override
public void onBackPressed() {
if (this.lastBackPressTime < System.currentTimeMillis() - 4000) {
toast = Toast.makeText(this, "Press back again to exit",
Toast.LENGTH_LONG);
toast.show();
this.lastBackPressTime = System.currentTimeMillis();
drawerLayout.closeDrawers();
} else {
if (toast != null) {
toast.cancel();
}
super.onBackPressed();
}
}
}
class MyAdapter extends BaseAdapter {
private Context context;
String[] navlist;
int[] images = { R.drawable.job, R.drawable.career, R.drawable.project,
R.drawable.facebook, R.drawable.mail, R.drawable.games };
public MyAdapter(Context context) {
this.context = context;
navlist = context.getResources().getStringArray(R.array.navdra);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return navlist.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return navlist[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.customrow, parent, false);
}
else {
row = convertView;
}
TextView titleTextView = (TextView) row.findViewById(R.id.textView11);
ImageView titleImageView = (ImageView) row
.findViewById(R.id.imageView1);
titleTextView.setText(navlist[position]);
titleImageView.setImageResource(images[position]);
return row;
}
}