“显示”;空";当值来自另一类java android时
我从服务器获取数据,当我使用另一个类中的服务器的值时,它显示null而不是实际值来自服务器…我有两个类activity2.java和notepadv3.java数据来自activity2.java中的服务器,当我使用该数据时,它显示null…我将粘贴这两个类…请解决我的问题为什么显示空的问题“显示”;空";当值来自另一类java android时,android,Android,我从服务器获取数据,当我使用另一个类中的服务器的值时,它显示null而不是实际值来自服务器…我有两个类activity2.java和notepadv3.java数据来自activity2.java中的服务器,当我使用该数据时,它显示null…我将粘贴这两个类…请解决我的问题为什么显示空的问题 package cabs.h; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamRe
package cabs.h;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.AdapterContextMenuInfo;
public class Notepadv3 extends ListActivity {
private static final int ACTIVITY_CREATE=0;
private static final int ACTIVITY_EDIT=1;
private static final int INSERT_ID = Menu.FIRST;
private static final int DELETE_ID = Menu.FIRST + 1;
private NotesDbAdapter mDbHelper;
private Long mRowId;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.listonruntime);
mDbHelper = new NotesDbAdapter(this);
mDbHelper.open();
fillData();
registerForContextMenu(getListView());
}
private void fillData() {
Cursor notesCursor = mDbHelper.fetchAllNotes();
startManagingCursor(notesCursor);
String s=NotesDbAdapter.KEY_TITLE;
Log.i("saurabh trivedi log cat,,,,,,,,,",s);
final String PREFS_NAME1 = "PrefSettings";
// Create an array to specify the fields we want to display in the list (only TITLE)
String[] from = new String[]{NotesDbAdapter.KEY_BODY,NotesDbAdapter.KEY_TITLE};
// String[] too = new String[]{NotesDbAdapter.KEY_TITLE};
// and an array of the fields we want to bind those fields to (in this case just text1)
int[] to = new int[]{R.id.toptext,R.id.middletext};
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.row, notesCursor, from, to);
setListAdapter(notes);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, DELETE_ID, 0, R.string.menu_delete);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case DELETE_ID:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
mDbHelper.deleteNote(info.id);
fillData();
return true;
}
return super.onContextItemSelected(item);
}
@Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
saveState();
outState.putSerializable(NotesDbAdapter.KEY_ROWID, mRowId);
}
@Override
protected void onPause() {
super.onPause();
saveState();
}
@Override
protected void onResume() {
super.onResume();
}
**
和Activity2.java类代码为
package cabs.h;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class Activity2 extends Activity {
public static String resp1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
try {
String tURL="http://qrrency.com/mobile/j2me/cab/CabRequestStatus.php?requestid=130";
URL url = new URL("http://qrrency.com/mobile/j2me/cab/CabRequestStatus.php?requestid=130");
Log.i("Line----saurabh trivedi-----", tURL);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
int m=0;
StringBuffer buffer=new StringBuffer();
String str1 = " ";
while ((m=in.read())!=-1)
{
**buffer.append((char)m);
str1=str1+(char)m;
cabbookingapplication.resp =str1;
resp1=cabbookingapplication.resp;
Log.i("Line----saurabh trivedi---,,,--",resp1);**
}
in.close();
} catch (MalformedURLException e)
{
} catch (IOException e)
{
}
Log.i("Line----saurabh trivedi---,,,--",resp1);
}
}
我认为如果不需要UI,就不应该从Activity扩展Activity2.java。如果您像在Notepadv3.java中那样使用“new”关键字实例化Activity2.java,则不会执行Activity2的onCreate方法。onCreate不是构造函数。这就是静态变量“resp1”为空的原因,因为它从未设置过 从Activity2中删除“扩展活动”,并将代码从onCreate移到返回数据的静态函数中,您可以在Notepadv3.java中调用该函数
class Activity2 {
public static String getData() {
String data;
// you code for retrieving data
return data;
}
}
在Notepadv3类调用中
String sss = Activity2.getData();
获取您的数据。活动是Android的一个UI类,可以从中扩展。决不能用“new”关键字实例化它的子类。系统正在为您创建一个实例,如果您启动了一个意图,那么系统将在活动的生命周期中调用onCreate
String sss = Activity2.getData();