我正在尝试从webview保存链接。从我的webview类中,我将页面URL保存如下。
public void AddUrl(String page_url){
SaveUrlActivity urlactivity = new SaveUrlActivity();
urlactivity.saveurl(page_url);
}
我的SaveUrlActivity类如下:
public class SaveUrlActivity extends Activity {
public String url;
public ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_savedurl);
lv = (ListView) findViewById(R.id.list);
saveurl(url);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent in = new Intent(SaveUrlActivity.this, DisPlayWebPageActivity.class);
in.putExtra("page_url", url);
startActivity(in);
}
});
}
public void saveurl(String url1){
url = url1;
final List<RowItem> ri = new ArrayList<RowItem>();
RowItem item = new RowItem(url);
ri.add(item);
SavedUrlAdapter adapter = new SavedUrlAdapter(SaveUrlActivity.this, ri);
lv.setAdapter(adapter);
}
}
每当我运行程序时,我都会收到以下错误。
FATAL EXCEPTION: main
Process: com.example.smarthelp, PID: 16284
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.smarthelp.SaveUrlActivity.saveurl(SaveUrlActivity.java:46)
at com.example.smarthelp.DisPlayWebPageActivity.AddUrl(DisPlayWebPageActivity.java:76)
at com.example.smarthelp.DisPlayWebPageActivity.onOptionsItemSelected(DisPlayWebPageActivity.java:66)
任何人都可以帮我弄明白我犯错误的地方吗?
答案 0 :(得分:1)
要了解的几件事情:
activity_savedurl
布局文件实际上包含带有list
ID的视图。如果没有,则lv
将为null
。url
传递给saveurl()
,但您从未为其指定过值。答案 1 :(得分:1)
您在初始化之前使用了对ListView lv
的引用,如果没有首先启动SaveUrlActivity,只有在setContentView(R.layout.activity_savedurl);
lv
之后才能初始化{{1} }。
public void saveurl(String url1){
url = url1;
final List<RowItem> ri = new ArrayList<RowItem>();
RowItem item = new RowItem(url);
ri.add(item);
SavedUrlAdapter adapter = new SavedUrlAdapter(SaveUrlActivity.this, ri);
lv.setAdapter(adapter);
}
如果在SaveUrlActivity
类中创建静态var并在启动Activity之前为其赋值,并且在该类内部对saveurl()
进行make调用时会更容易你已经初始化了所有的观点。
类似的东西:
public void AddUrl(String page_url){
SaveUrlActivity.page_url = page_url;
Intent i = new Intent(this, SaveUrlActivity.class);
startActivity(i);
}
在你的SaveUrlActivity.java中:
public class SaveUrlActivity extends Activity {
public String url;
public ListView lv;
static page_url;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_savedurl);
lv = (ListView) findViewById(R.id.list);
saveurl();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent in = new Intent(SaveUrlActivity.this, DisPlayWebPageActivity.class);
in.putExtra("page_url", url);
startActivity(in);
}
});
}
public void saveurl(){
url = page_url;
final List<RowItem> ri = new ArrayList<RowItem>();
RowItem item = new RowItem(url);
ri.add(item);
SavedUrlAdapter adapter = new SavedUrlAdapter(SaveUrlActivity.this, ri);
lv.setAdapter(adapter);
}
}
如果您计划开始此活动,这是准确的,如果您需要更新已启动的数据,最好使用Handler