做一晚上了,有没有大佬帮帮我,万分感谢
报错E/AndroidRuntime: FATAL EXCEPTION: main
Process: main_activity.diary, PID: 11800
java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.widget.TextView.setText(java.lang.CharSequence)’ on a null object reference
at main_activity.diary.Memo 1. g e t V i e w ( M e m o . j a v a : 87 ) a t a n d r o i d . w i d g e t . A b s L i s t V i e w . o b t a i n V i e w ( A b s L i s t V i e w . j a v a : 2360 ) a t a n d r o i d . w i d g e t . L i s t V i e w . m e a s u r e H e i g h t O f C h i l d r e n ( L i s t V i e w . j a v a : 1326 ) a t a n d r o i d . w i d g e t . L i s t V i e w . o n M e a s u r e ( L i s t V i e w . j a v a : 1233 ) a t a n d r o i d . v i e w . V i e w . m e a s u r e ( V i e w . j a v a : 19731 ) a t a n d r o i d . w i d g e t . R e l a t i v e L a y o u t . m e a s u r e C h i l d H o r i z o n t a l ( R e l a t i v e L a y o u t . j a v a : 715 ) a t a n d r o i d . w i d g e t . R e l a t i v e L a y o u t . o n M e a s u r e ( R e l a t i v e L a y o u t . j a v a : 461 ) a t a n d r o i d . v i e w . V i e w . m e a s u r e ( V i e w . j a v a : 19731 ) a t a n d r o i d . v i e w . V i e w G r o u p . m e a s u r e C h i l d W i t h M a r g i n s ( V i e w G r o u p . j a v a : 6120 ) a t a n d r o i d . w i d g e t . F r a m e L a y o u t . o n M e a s u r e ( F r a m e L a y o u t . j a v a : 185 ) a t a n d r o i d . s u p p o r t . v 7. w i d g e t . C o n t e n t F r a m e L a y o u t . o n M e a s u r e ( C o n t e n t F r a m e L a y o u t . j a v a : 135 ) a t a n d r o i d . v i e w . V i e w . m e a s u r e ( V i e w . j a v a : 19731 ) a t a n d r o i d . v i e w . V i e w G r o u p . m e a s u r e C h i l d W i t h M a r g i n s ( V i e w G r o u p . j a v a : 6120 ) a t a n d r o i d . s u p p o r t . v 7. w i d g e t . A c t i o n B a r O v e r l a y L a y o u t . o n M e a s u r e ( A c t i o n B a r O v e r l a y L a y o u t . j a v a : 389 ) a t a n d r o i d . v i e w . V i e w . m e a s u r e ( V i e w . j a v a : 19731 ) a t a n d r o i d . v i e w . V i e w G r o u p . m e a s u r e C h i l d W i t h M a r g i n s ( V i e w G r o u p . j a v a : 6120 ) a t a n d r o i d . w i d g e t . F r a m e L a y o u t . o n M e a s u r e ( F r a m e L a y o u t . j a v a : 185 ) a t a n d r o i d . v i e w . V i e w . m e a s u r e ( V i e w . j a v a : 19731 ) a t a n d r o i d . v i e w . V i e w G r o u p . m e a s u r e C h i l d W i t h M a r g i n s ( V i e w G r o u p . j a v a : 6120 ) a t a n d r o i d . w i d g e t . L i n e a r L a y o u t . m e a s u r e C h i l d B e f o r e L a y o u t ( L i n e a r L a y o u t . j a v a : 1464 ) a t a n d r o i d . w i d g e t . L i n e a r L a y o u t . m e a s u r e V e r t i c a l ( L i n e a r L a y o u t . j a v a : 758 ) a t a n d r o i d . w i d g e t . L i n e a r L a y o u t . o n M e a s u r e ( L i n e a r L a y o u t . j a v a : 640 ) a t a n d r o i d . v i e w . V i e w . m e a s u r e ( V i e w . j a v a : 19731 ) a t a n d r o i d . v i e w . V i e w G r o u p . m e a s u r e C h i l d W i t h M a r g i n s ( V i e w G r o u p . j a v a : 6120 ) a t a n d r o i d . w i d g e t . F r a m e L a y o u t . o n M e a s u r e ( F r a m e L a y o u t . j a v a : 185 ) a t c o m . a n d r o i d . i n t e r n a l . p o l i c y . D e c o r V i e w . o n M e a s u r e ( D e c o r V i e w . j a v a : 687 ) a t a n d r o i d . v i e w . V i e w . m e a s u r e ( V i e w . j a v a : 19731 ) a t a n d r o i d . v i e w . V i e w R o o t I m p l . p e r f o r m M e a s u r e ( V i e w R o o t I m p l . j a v a : 2271 ) a t a n d r o i d . v i e w . V i e w R o o t I m p l . m e a s u r e H i e r a r c h y ( V i e w R o o t I m p l . j a v a : 1358 ) a t a n d r o i d . v i e w . V i e w R o o t I m p l . p e r f o r m T r a v e r s a l s ( V i e w R o o t I m p l . j a v a : 1607 ) a t a n d r o i d . v i e w . V i e w R o o t I m p l . d o T r a v e r s a l ( V i e w R o o t I m p l . j a v a : 1246 ) a t a n d r o i d . v i e w . V i e w R o o t I m p l 1.getView(Memo.java:87) at android.widget.AbsListView.obtainView(AbsListView.java:2360) at android.widget.ListView.measureHeightOfChildren(ListView.java:1326) at android.widget.ListView.onMeasure(ListView.java:1233) at android.view.View.measure(View.java:19731) at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461) at android.view.View.measure(View.java:19731) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120) at android.widget.FrameLayout.onMeasure(FrameLayout.java:185) at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135) at android.view.View.measure(View.java:19731) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120) at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:389) at android.view.View.measure(View.java:19731) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120) at android.widget.FrameLayout.onMeasure(FrameLayout.java:185) at android.view.View.measure(View.java:19731) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120) at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464) at android.widget.LinearLayout.measureVertical(LinearLayout.java:758) at android.widget.LinearLayout.onMeasure(LinearLayout.java:640) at android.view.View.measure(View.java:19731) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120) at android.widget.FrameLayout.onMeasure(FrameLayout.java:185) at com.android.internal.policy.DecorView.onMeasure(DecorView.java:687) at android.view.View.measure(View.java:19731) at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2271) at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1358) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1607) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246) at android.view.ViewRootImpl 1.getView(Memo.java:87)atandroid.widget.AbsListView.obtainView(AbsListView.java:2360)atandroid.widget.ListView.measureHeightOfChildren(ListView.java:1326)atandroid.widget.ListView.onMeasure(ListView.java:1233)atandroid.view.View.measure(View.java:19731)atandroid.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)atandroid.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)atandroid.view.View.measure(View.java:19731)atandroid.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)atandroid.widget.FrameLayout.onMeasure(FrameLayout.java:185)atandroid.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)atandroid.view.View.measure(View.java:19731)atandroid.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)atandroid.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:389)atandroid.view.View.measure(View.java:19731)atandroid.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)atandroid.widget.FrameLayout.onMeasure(FrameLayout.java:185)atandroid.view.View.measure(View.java:19731)atandroid.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)atandroid.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)atandroid.widget.LinearLayout.measureVertical(LinearLayout.java:758)atandroid.widget.LinearLayout.onMeasure(LinearLayout.java:640)atandroid.view.View.measure(View.java:19731)atandroid.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)atandroid.widget.FrameLayout.onMeasure(FrameLayout.java:185)atcom.android.internal.policy.DecorView.onMeasure(DecorView.java:687)atandroid.view.View.measure(View.java:19731)atandroid.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2271)atandroid.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1358)atandroid.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1607)atandroid.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246)atandroid.view.ViewRootImplTraversalRunnable.run(ViewRootImpl.java:6301)
at android.view.Choreographer C a l l b a c k R e c o r d . r u n ( C h o r e o g r a p h e r . j a v a : 871 ) a t a n d r o i d . v i e w . C h o r e o g r a p h e r . d o C a l l b a c k s ( C h o r e o g r a p h e r . j a v a : 683 ) a t a n d r o i d . v i e w . C h o r e o g r a p h e r . d o F r a m e ( C h o r e o g r a p h e r . j a v a : 619 ) a t a n d r o i d . v i e w . C h o r e o g r a p h e r CallbackRecord.run(Choreographer.java:871) at android.view.Choreographer.doCallbacks(Choreographer.java:683) at android.view.Choreographer.doFrame(Choreographer.java:619) at android.view.Choreographer CallbackRecord.run(Choreographer.java:871)atandroid.view.Choreographer.doCallbacks(Choreographer.java:683)atandroid.view.Choreographer.doFrame(Choreographer.java:619)atandroid.view.ChoreographerFrameDisplayEventReceiver.run(Choreographer.java:857)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
.java文件:
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class Memo extends AppCompatActivity {
private TextView welcome;
private ImageButton btn_add;
private ListView listView;
private DBHelper dbHelper;
private ArrayList
SQLiteDatabase db;
private int id;
private String name;
private String strid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_add=(ImageButton)findViewById(R.id.btn_add);
listView=(ListView)findViewById(R.id.lv_memoList);
dbHelper=new DBHelper(this);
db=dbHelper.getReadableDatabase();
welcome=(TextView)findViewById(R.id.welcome_word);
final Intent intent=getIntent();
MemoList=new ArrayList<>();
id=Integer.parseInt(intent.getStringExtra(“id”));
String sql_name=“select username from tb_user where user_id=?”;
Cursor c=db.rawQuery(sql_name,new String[]{Integer.toString(id)});
while(c.moveToNext()){
name=c.getString(c.getColumnIndex(“username”));
}
strid=Integer.toString(id);
welcome.setText(name+“欢迎你!”);
String sql=“select * from tb_memo where userID=?”;
Cursor cursor = db.rawQuery(sql,new String[]{Integer.toString(id)});
while(cursor.moveToNext()){
if(id==cursor.getInt(2)){
String title=cursor.getString(cursor.getColumnIndex(“title”));
String createDate=cursor.getString(cursor.getColumnIndex(“createDate”));
int id=cursor.getInt(0);
Memo_info memo=new Memo_info(title,createDate,id);
System.out.println(“title:”+title+“createDate:”+createDate+“id:”+id);
MemoList.add(memo);
}
}
listView.setAdapter(new BaseAdapter() {
@Override
public int getCount() {
return MemoList.size();
}
@Override
public Object getItem(int i) {
return i;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
View view;
if (convertView==null){
view = View.inflate(getBaseContext(),R.layout.list_item_memo,null);
}else{
view = convertView;
}
Memo_info memo=MemoList.get(i);
TextView title=(TextView)view.findViewById(R.id.title);
TextView content=(TextView)view.findViewById(R.id.content);
TextView getsqlId=(TextView)view.findViewById(R.id.getsqlId);
TextView createDate=(TextView)view.findViewById(R.id.noticeDate);
title.setText(memo.getTitle());
content.setText(memo.getContent());
createDate.setText(memo.getCreateDate());
getsqlId.setText(Integer.toString(memo.getId()));
return view;
}
});
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent1=new Intent(Memo.this,SaveMemo.class);
intent1.putExtra("userID",strid);
startActivity(intent1);
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> adapterView, View view, int i, long l) {
TextView view1=(TextView)view.findViewById(R.id.getsqlId);
String memoid=view1.getText().toString();
Intent intent2=new Intent(Memo.this,showmemo.class);
intent2.putExtra("memoid",memoid);
intent2.putExtra("user_id",strid);
startActivity(intent2);
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView> adapterView, View view, int i, long l) {
Button btn_del=(Button)view.findViewById(R.id.btn_del);
btn_del.setVisibility(View.VISIBLE);
TextView view1=(TextView)view.findViewById(R.id.getsqlId);
final String memoid=view1.getText().toString();
btn_del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
db=dbHelper.getWritableDatabase();
db.delete("tb_memo","id=?",new String[]{memoid});
Intent intent3=new Intent(Memo.this,Memo.class);
System.out.println("调用删除方法");
intent3.putExtra("id",strid);
startActivity(intent3);
}
});
return true;
}
});
}
}
.xml文件
这是在做一个记事本,一开始是没问题的,写入数据保存后就闪退了,后面一到跳转到另一个activity时就直接闪退了。这到底怎么回事啊,之前也是报空指针我给解决了,可是这次报的是at main_activity.diary.Memo$1.getView(Memo.java:87)直接给我整懵了,它指示位置代码为title.setText(memo.getTitle()); 布局也没错的感觉啊,现在很无解。 help!