Android GUI does not show up

gedr

I am currently making an app for android and I have a problem where the UI on a new activity that I start from the main one does not show up. I have no idea what the problem is.

Here is my second activity's layout xml file:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/TableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.tabcards.android.Search" >

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" >

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
         >

        <TableLayout
            android:id="@+id/tableScrollView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:stretchColumns="yes"
            android:padding="5dp" 
            android:background="@color/gray">
        </TableLayout>
    </ScrollView>
</TableRow>

Here is my acitiviy's code:

public class Search extends ActionBarActivity {
TableLayout tableScrollView;
String[] JSONExceptions = { "type", "em", "user_id", "id", "profilepic", "bg"};
String value;
JSONObject jObject;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        value = extras.getString("id");
    }
    System.out.println(value);
    tableScrollView = (TableLayout) findViewById(R.id.tableScrollView);
    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {

                jObject = getJson("http://www.tabcards.com/req/androidapi/L2o30H8JlFMtFYHW3KLxkts20ztc5Be6Z6m6v315/json/"
                            + value);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    thread.start();
    try {
        thread.join();
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                createUI(jObject);
            } catch (JSONException e) {
                e.printStackTrace();
            }               
        }

    });

    System.out.println("complete");

}

private void createUI(JSONObject jObject) throws JSONException {

    Iterator<?> keys = jObject.keys();
    int absIndex = 0;
    while( keys.hasNext() ){
        String key = (String)keys.next();
        if(!contains2(JSONExceptions , jObject.get(key))){
            String value = jObject.getString(key);
            System.out.println("level 1");
            if(value!="") {
                insertElement(key + " : " + value, absIndex++);

            }
        }
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.search, menu);
    return true;
}

private void insertElement(String data, int i) {
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View newRow = inflater.inflate(R.layout.row, null, false);
    newRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

    TextView dataTextView = (TextView) newRow
            .findViewById(R.id.rowTextView);
    dataTextView.setText(data);
    System.out.println(dataTextView.getText().toString());
    tableScrollView.addView(newRow, i);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

private InputStream downloadUrl(String urlString) throws IOException {
    URL url = new URL(urlString);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setReadTimeout(10000 /* milliseconds */);
    conn.setConnectTimeout(15000 /* milliseconds */);
    conn.setRequestMethod("GET");
    conn.setDoInput(true);
    // Starts the query
    conn.connect();
    return conn.getInputStream();
}

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

public static JSONObject getJson(String url){

    InputStream is = null;
    String result = "";
    JSONObject jsonObject = null;

    // HTTP
    try {           
        HttpClient httpclient = new DefaultHttpClient(); // for port 80 requests!
        HttpPost httppost = new HttpPost(url);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    } catch(Exception e) {
        return null;
    }

    // Read response to string
    try {           
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
            System.out.println(line);
        }
        is.close();
        result = sb.toString().replace("[", "");                
    } catch(Exception e) {
        return null;
    }

    // Convert string to object
    try {
        jsonObject = new JSONObject(result.replace("]", ""));            
    } catch(JSONException e) {
        return null;
    }

    return jsonObject;

}

This is how I am creating the activity:

Intent i = new Intent(getApplicationContext(), Search.class);
i.putExtra("id",searchEditText.getText().toString());
startActivity(i);

Tell me if you need any more info.

Rod_Algonquin

problem:

thread.join();

That problem is dreadlock you are waiting for thread to be done executing, which will put your UI thread to the Blocking state like Thread.Sleep() thus UI thread is waiting for your request to be done executing before it can display the layout in the screen.

from documentation:

Like sleep, join responds to an interrupt by exiting with an InterruptedException.

solution:

Use only one thread which will still wait for the request(createUI) and executes your createUI method after.

Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        try {

            jObject = getJson("http://www.tabcards.com/req/androidapi/L2o30H8JlFMtFYHW3KLxkts20ztc5Be6Z6m6v315/json/"
                        + value);
            createUI(jObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});
thread.start();

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Why does my corner-widget not show up in QTabWidget?

분류에서Dev

Kendo UI batch edit grid DropDownList does not show up

분류에서Dev

Android TodoApp with ListView - list don't show up

분류에서Dev

Why won't my Android SDK Manager show up when invoked from Eclipse?

분류에서Dev

GUI doesn't show the command at the frame

분류에서Dev

Popping up the folder when clicking "Show in Folder"

분류에서Dev

Popping up the folder when clicking "Show in Folder"

분류에서Dev

QSystemTrayIcon doesn't always show up

분류에서Dev

JWPlayer onComplete show a pop-up

분류에서Dev

Why Won't the BG Colors Show Up?

분류에서Dev

show multiple modal pop up in angularjs

분류에서Dev

How to get padding to show up to the left of the contents?

분류에서Dev

Android wear not show notification

분류에서Dev

Android Show Hide LinearLayout

분류에서Dev

Why does QuickCheck give up?

분류에서Dev

the canvas onDraw does not show enything

분류에서Dev

Nested Fragment does not show properly

분류에서Dev

'htop' does not show the correct CPU%, but 'top' does

분류에서Dev

Run application on local machine and show GUI on remote display

분류에서Dev

Setting up a server for iOS/Android

분류에서Dev

Show more in Horizontal ScrollView in android

분류에서Dev

Android : Show dialog on onResume event

분류에서Dev

Show difference in days in Java/ Android

분류에서Dev

Windows 10 does not wake up from LAN

분류에서Dev

How does ReversedWildcardFilterFactory speed up wildcard searches?

분류에서Dev

Hello world alert does not pop-up

분류에서Dev

Google Sheets Query Image to show up from Query result

분류에서Dev

Data appears when printed but doesn't show up in dataframe

분류에서Dev

How to show custom pop up when detect browser close event?

Related 관련 기사

뜨겁다태그

보관