I am trying to save a link from webview. From my webview class, I am saving the page url as below.
public void AddUrl(String page_url){
SaveUrlActivity urlactivity = new SaveUrlActivity();
urlactivity.saveurl(page_url);
}
My SaveUrlActivity class is as below:
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);
}
}
Whenever I run my program, I am getting the following error.
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)
Can anyone help me figure out where I am making the mistake?
You're using reference to ListView lv
before it has been initialized, and you can't do that without first starting the SaveUrlActivity, only after it's setContentView(R.layout.activity_savedurl);
you can initialize the lv
.
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);
}
It would be easier if you make a static var in your SaveUrlActivity
class and assign a value to it before you start the Activity, and the make call to the saveurl()
from inside of that class when you have all your views initialized.
Something like:
public void AddUrl(String page_url){
SaveUrlActivity.page_url = page_url;
Intent i = new Intent(this, SaveUrlActivity.class);
startActivity(i);
}
And in your 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);
}
}
It's accurate if you're planning to start this activity, if you need to update data in already started one, you better use the Handler