它完全在模拟器中工作,我甚至在bluestack上检查它,但是当我在移动设备中使用它时,当我按下按钮1时它应该加载新的布局时崩溃,而当它检查互联网连接时它按钮4工作正常。
这是我的MainActivity.java
package com.example.firstapp;
import java.net.URL;
import java.net.URLConnection;
import android.net.ConnectivityManager;
import android.os.*;
import android.app.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity {
private Button button1, button2,button4,check_button1,canda, candb;
private EditText vid,pin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadmain();
}
//Load Main Layout
public void loadmain()
{
setContentView(R.layout.activity_main);
addListenerOnbutton1();
addListenerOnbutton4();
addListenerOnbutton2();
}
//Load check_voter layout
public void loadcheck_voters()
{
setContentView(R.layout.check_voters);
vid = (EditText)findViewById(R.id.editText1);
pin = (EditText)findViewById(R.id.editText2);
addListenerOncheck_button1();
}
public void loadclist()
{
setContentView(R.layout.clist);
addListenerOncanda();
addListenerOncandb();
}
public void validate()
{
String vid1 = vid.getText().toString();
String pin1 = pin.getText().toString();
new ValidateVoter(this).execute(vid1,pin1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void addListenerOnbutton1()
{
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
loadcheck_voters();
}
});
}
public void addListenerOnbutton4()
{
button4 = (Button) findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
isInternetOn();
}
});
}
public void addListenerOnbutton2()
{
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
//
}
});
}
public void addListenerOncheck_button1()
{
check_button1 = (Button) findViewById(R.id.check_button1);
check_button1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
validate();
loadclist();
}
});
}
public boolean isConnectedToServer(String url, int timeout) {
try{
URL myUrl = new URL(url);
URLConnection connection = myUrl.openConnection();
connection.setConnectTimeout(timeout);
connection.connect();
return true;
} catch (Exception e) {
return false;
}
}
public void addListenerOncanda()
{
canda = (Button) findViewById(R.id.canda);
canda.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
voteA();
}
});
}
public void addListenerOncandb()
{
candb = (Button) findViewById(R.id.candb);
candb.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
voteB();
}
});
}
public void voteA()
{
String cand="Candidate A";
new IncVoteA().execute(cand);
loadmain();
Toast.makeText(this, "You Voted for Candidate A", Toast.LENGTH_LONG).show();
}
public void voteB()
{
String cand="Candidate B";
new IncVoteB().execute(cand);
loadmain();
Toast.makeText(this, "You Voted for Candidate B", Toast.LENGTH_LONG).show();
}
public final boolean isInternetOn() {
ConnectivityManager connec = (ConnectivityManager)getSystemService(getBaseContext().CONNECTIVITY_SERVICE);
String disp=new String();
if ( connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTED ||
connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.CONNECTING ||
connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTING ||
connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.CONNECTED )
{
new CheckConn(this).execute();
return true;
}
else if ( connec.getNetworkInfo(0).getState() == android.net.NetworkInfo.State.DISCONNECTED || connec.getNetworkInfo(1).getState() == android.net.NetworkInfo.State.DISCONNECTED )
{
Toast.makeText(this,"Not Connected", Toast.LENGTH_LONG).show();
return false;
}
return false;
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.firstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.firstapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
logcat的
02-22 20:20:54.580: E/AndroidRuntime(5387): FATAL EXCEPTION: main
02-22 20:20:54.580: E/AndroidRuntime(5387): android.content.res.Resources$NotFoundException: Resource ID #0x7f030001
02-22 20:20:54.580: E/AndroidRuntime(5387): at android.content.res.Resources.getValue(Resources.java:1884)
02-22 20:20:54.580: E/AndroidRuntime(5387): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2990)
02-22 20:20:54.580: E/AndroidRuntime(5387): at android.content.res.Resources.getLayout(Resources.java:1723)
02-22 20:20:54.580: E/AndroidRuntime(5387): at android.view.LayoutInflater.inflate(LayoutInflater.java:395)
02-22 20:20:54.580: E/AndroidRuntime(5387): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
02-22 20:20:54.580: E/AndroidRuntime(5387): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:318)
02-22 20:20:54.580: E/AndroidRuntime(5387): at android.app.Activity.setContentView(Activity.java:1924)
02-22 20:20:54.580: E/AndroidRuntime(5387): at com.example.firstapp.MainActivity.loadcheck_voters(MainActivity.java:43)
02-22 20:20:54.580: E/AndroidRuntime(5387): at com.example.firstapp.MainActivity$1.onClick(MainActivity.java:79)
02-22 20:20:54.580: E/AndroidRuntime(5387): at android.view.View.performClick(View.java:4261)
02-22 20:20:54.580: E/AndroidRuntime(5387): at android.view.View$PerformClick.run(View.java:17420)
02-22 20:20:54.580: E/AndroidRuntime(5387): at android.os.Handler.handleCallback(Handler.java:615)
02-22 20:20:54.580: E/AndroidRuntime(5387): at android.os.Handler.dispatchMessage(Handler.java:92)
02-22 20:20:54.580: E/AndroidRuntime(5387): at android.os.Looper.loop(Looper.java:177)
02-22 20:20:54.580: E/AndroidRuntime(5387): at android.app.ActivityThread.main(ActivityThread.java:4944)
02-22 20:20:54.580: E/AndroidRuntime(5387): at java.lang.reflect.Method.invokeNative(Native Method)
02-22 20:20:54.580: E/AndroidRuntime(5387): at java.lang.reflect.Method.invoke(Method.java:511)
02-22 20:20:54.580: E/AndroidRuntime(5387): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
02-22 20:20:54.580: E/AndroidRuntime(5387): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
02-22 20:20:54.580: E/AndroidRuntime(5387): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
无论如何,感谢大家......用eclipse连接我的手机得到了堆栈跟踪。检查ResourceNotFoundEception
是R.java
文件中的哪个资源,当我在layout-qwerty
文件夹中查找时,在该按钮上加载的布局位于某个layout
文件夹中。更正了它以及它在移动设备上运行的所有内容..