簡體   English   中英

我要在MainActivity中放入哪一行代碼才能使用新的Class?

[英]What line of code do I put in my MainActivity to be able to use the new Class?

Android編程新手,需要一些幫助。 創建我的第一個應用程序。

我創建了一個新類,現在嘗試使用該類,請問我,不知道要在MainActivity.java添加什么以使用新類,這是MainActivity ,也是該類。

我要在MainActivity放入哪一行代碼才能使用新的Class? 非常感謝你。

public class MainActivity extends Activity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Determine whether the current user is an anonymous user
    if (ParseAnonymousUtils.isLinked(ParseUser.getCurrentUser())) {
        // If user is anonymous, send the user to LoginSignupActivity.class
        Intent intent = new Intent(MainActivity.this,
                LoginSignupActivity.class);
        startActivity(intent);
        finish();
    } else {
        // If current user is NOT anonymous user
        // Get current user data from Parse.com
        ParseUser currentUser = ParseUser.getCurrentUser();
        if (currentUser != null) {
            // Send logged in users to Welcome.class
            Intent intent = new Intent(MainActivity.this, Welcome.class);
            startActivity(intent);
            finish();
        } else {
            // Send user to LoginSignupActivity.class
            Intent intent = new Intent(MainActivity.this,
                    LoginSignupActivity.class);
            startActivity(intent);
            finish();
        }           
    }

}    
}

這是課程:

public class RaceCar extends Activity {

TextView tvHttp;

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

    tvHttp = (TextView) findViewById(R.id.httptest);

    GetData obj = new GetData();
    obj.execute("http://racecar.com");

}

public class GetData extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {
    BufferedReader reader = null;
    String data = null;

    try {
        HttpClient client = new DefaultHttpClient();
        URI uri = new URI(params[0]);
        HttpGet get = new HttpGet(uri);
        HttpResponse response = client.execute(get);
        InputStream stream = response.getEntity().getContent();
        reader = new BufferedReader (new InputStreamReader (stream));           
        StringBuffer buffer = new StringBuffer("");
        String line = "";
        String newLine = System.getProperty("line.separator");
        while ((line = reader.readLine()) !=null) {
            buffer.append(line + newLine);
        }
        reader.close();

        data = buffer.toString();
        return data;

    } catch (URISyntaxException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    finally {
        if (reader !=null) {
            try {
                reader.close();
            } catch (Exception e) {
        }
    }
}
    return null;
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
    tvHttp.setText(result);
    }
}
}

如果你想開RaceCar從Activity類MainActivity你必須有打開它Intent像你已經做了Welcome.classLoginSignupActivity.class

例如:

Intent intent = new Intent(MainActivity.this, RaceCar.class);
startActivity(intent);

您還必須在“應用程序”部分的AndroidManifest.xml文件中包括此RaceCar.class活動,如下所示:

<activity
    android:name="com.example.app.RaceCar"
    android:label="RaceCar">
       <!-- Make sure this is the full path to your Activity class -->
</activity>

它看起來應該與您在其中定義的MainActivity相似。

您可以在此處找到有關“活動”的進一步閱讀(非常值得一讀)。

使用Intent調用Activity

Intent intent = new Intent(MainActivity.this, RaceCar.class);
startActivity(intent);

manifest.xml注冊新的Activity

<activity android:name="packageName.RaceCar"    
    android:label="@string/app_name">
</activity>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM