Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 正在尝试访问选项卡小部件中的数据库_Java_Android - Fatal编程技术网

Java 正在尝试访问选项卡小部件中的数据库

Java 正在尝试访问选项卡小部件中的数据库,java,android,Java,Android,我确信我的代码结构是错误的,但我已经看了很长时间了 我已经设法让一个类能够访问我的数据库并返回数据,但是当我试图将这个类构建到我的选项卡小部件中时,它似乎不起作用 这就是我所说的班级: // Create an Intent to launch an Activity for the tab (to be reused) intent = new Intent().setClass(this, recipelist.class); // Initialize a TabSpec for eac

我确信我的代码结构是错误的,但我已经看了很长时间了

我已经设法让一个类能够访问我的数据库并返回数据,但是当我试图将这个类构建到我的选项卡小部件中时,它似乎不起作用

这就是我所说的班级:

// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, recipelist.class);

// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("recipe").setIndicator("recipe", res.getDrawable(R.drawable.ic_tab_list)).setContent(intent);
tabHost.addTab(spec);
这是带有数据库代码的类:

package fridge.mate;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;

public class recipelist extends Activity {
   TextView txt;
   @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    // Create a crude view - this should really be set via the layout resources  
    // but since its an example saves declaring them in the XML.  
    LinearLayout rootLayout = new LinearLayout(getApplicationContext());  
    txt = new TextView(getApplicationContext());  
    rootLayout.addView(txt);  
    setContentView(rootLayout);  

    // Set the text and call the connect function.  
    txt.setText("Connecting..."); 
  //call the method to run the data retreival
    txt.setText("gfgfgf..."); 
   }
public static final String KEY_121 = "http://www.bankruptcy.co.uk/1.php"; //i use my real ip here
private String getServerData(String returnString) {

       InputStream is = null;

       String result = "";
        //the year data to send
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("name","beans"));

        //http post
        try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(KEY_121);
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();

        }catch(Exception e){
                Log.e("log_tag", "Error in http connection "+e.toString());
        }

        //convert response to string
        try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                }
                is.close();
                result=sb.toString();
        }catch(Exception e){
                Log.e("log_tag", "Error converting result "+e.toString());
        }
        //parse json data
        try{
                JSONArray jArray = new JSONArray(result);
                for(int i=0;i<jArray.length();i++){
                        JSONObject json_data = jArray.getJSONObject(i);
                        Log.i("log_tag","ID: "+json_data.getInt("ID")+
                                ", name: "+json_data.getString("name")+
                                ", servings: "+json_data.getString("servings")+
                                ", discription: "+json_data.getString("discription")
                        );
                        //Get an output to the screen
                        returnString += "\n\t" + jArray.getJSONObject(i); 
                        txt.setText("Connecting..."); 

                }
        }catch(JSONException e){
                Log.e("log_tag", "Error parsing data "+e.toString());
        }
        return returnString; 
    }    
}
package fredge.mate;
导入java.io.BufferedReader;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.os.Bundle;
导入android.util.Log;
导入android.widget.LinearLayout;
导入android.widget.TextView;
公共类recipelist扩展活动{
文本视图;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建一个粗略的视图-这应该通过布局资源进行设置
//但是,因为这是一个示例,所以不需要在XML中声明它们。
LinearLayout rootLayout=新的LinearLayout(getApplicationContext());
txt=新文本视图(getApplicationContext());
rootLayout.addView(txt);
setContentView(rootLayout);
//设置文本并调用connect函数。
txt.setText(“连接…”);
//调用该方法以运行数据检索
txt.setText(“GFGF…”);
}
公共静态最终字符串键_121=”http://www.bankruptcy.co.uk/1.php“;//我在这里使用我的真实ip
私有字符串getServerData(字符串返回字符串){
InputStream=null;
字符串结果=”;
//要发送的年度数据
ArrayList nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“名称”、“bean”);
//http post
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(键号121);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
}捕获(例外e){
e(“Log_标记”,“http连接错误”+e.toString());
}
//将响应转换为字符串
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
}捕获(例外e){
Log.e(“Log_标记”,“错误转换结果”+e.toString());
}
//解析json数据
试一试{
JSONArray jArray=新JSONArray(结果);
对于(int i=0;i几个注释:

  • 您调用了两次
    setContentView()
    。一次就足够了。那么,哪一个是正确的
  • 您正在调用
    new LinearLayout(getApplicationContext())
    。活动是一个上下文,因此您可以调用
    new LinearLayout(this)
    。TextView也是如此
  • 我没有看到任何标签。它们在哪里?我只看到里面的LinearLayout和TextView
  • 您不应该在UI线程(EDT)中执行长时间运行的任务。用于此

  • 看看他们是如何做到的。

    您好,我使用了tab示例来构建选项卡布局,我只是想在我的其他类(每个选项卡调用的类)中使用我的代码来执行一些操作,例如返回json对象。