我在模拟器中运行Android应用时遇到问题:每次尝试时都会出现同样的错误:
12-12 08:53:33.958: E/cutils-trace(5320): Error opening trace file: No such file or directory (2)
这是我的LoginPage.java代码:
编辑:我稍微更改了我的.java代码,但我继续发现上述错误...我确定有一些我不完全理解的东西,但我无法看到它是什么。LoginPage.java
package com.example.shop;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import com.example.shop.R;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class LoginPage extends Activity implements OnClickListener, OnItemSelectedListener {
Context context;
private TextView textView;
EditText username;
EditText password;
String userid;
boolean succeed;
Boolean isInternetPresent;
ConnectionChecker cc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_page);
context=this;
userid = "";
username = (EditText)findViewById(R.id.edittext_login_username);
password = (EditText)findViewById(R.id.edittext_login_password);
textView = (TextView)findViewById(R.id.textView);
cc = new ConnectionChecker(getApplicationContext());
isInternetPresent = false;
//login_button();
Button enter = (Button) findViewById(R.id.enter);
enter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(arg0.getId() == R.id.enter)
{
Toast.makeText(
context,
"Login correcte",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context, Tenda.class);
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "http://www.v2msoft.com/clientes/lasalle/curs-android/login.php" });
startActivity(intent);
finish();
}
if (username.getText().toString().equals("luis") && password.getText().toString().equals("seguro")){
Toast.makeText(getApplicationContext(), "Redirecting...",
Toast.LENGTH_SHORT).show();
succeed = true;
userid = "luis";
}
else{
Toast.makeText(getApplicationContext(), "Wrong info",
Toast.LENGTH_SHORT).show();
succeed = false;
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login_page, menu);
return true;
}
public void login ()
{
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String response = "";
for (String url : urls) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return response;
}
@Override
protected void onPostExecute(String result) {
textView.setText(result);
}
}
public void login_button()
{
}
public void IC (View v)
{
isInternetPresent = cc.isConnectingToInternet();
// check for Internet status
if (isInternetPresent) {
// Internet Connection is Present
// make HTTP requests
showAlertDialog(LoginPage.this, "Internet Connection",
"You have internet connection", true);
} else {
// Internet connection is not present
// Ask user to connect to Internet
showAlertDialog(LoginPage.this, "No Internet Connection",
"You don't have internet connection.", false);
}
}
@SuppressWarnings("deprecation")
public void showAlertDialog(Context context, String title, String message, Boolean status) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
// Setting Dialog Title
alertDialog.setTitle(title);
// Setting Dialog Message
alertDialog.setMessage(message);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
// Showing Alert Message
alertDialog.show();
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
而且,我的.xml文件:
activity_login_page.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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=".LoginPage" >
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></TextView>
<EditText
android:id="@+id/edittext_login_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="username"
android:inputType="text"/>
<EditText
android:id="@+id/edittext_login_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="password"
android:password="true"
android:layout_below="@+id/edittext_login_username"
/>
<Button
android:id="@+id/enter"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="right"
android:text="login"
android:layout_below="@+id/edittext_login_password" />
</RelativeLayout>
任何人都可以向我指出我的错误在哪里?
答案 0 :(得分:2)
首先将此onCreate()方法移动。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_page);
context=this;
EditText username = (EditText)findViewById(R.id.edittext_login_username);
EditText password = (EditText)findViewById(R.id.edittext_login_password);
login_button();
}
因为您无法在onCreate()方法之前初始化您的UI元素。
此外,您刚刚宣布了
private TextView textView;
但没有初始化它意味着没有找到它的ID。所以也这样做。
答案 1 :(得分:1)
先做这个
public class LoginPage extends Activity implements OnClickListener, OnItemSelectedListener {
Context context;
private TextView textView;
EditText username ;
EditText password ;
String userid;
boolean succeed;
ConnectionChecker cc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_page);
context=this;
username = (EditText)findViewById(R.id.edittext_login_username);
password = (EditText)findViewById(R.id.edittext_login_password);
String userid = "";
Boolean isInternetPresent = false;
ConnectionChecker cc = new ConnectionChecker(getApplicationContext());
login_button();
}
}