我一直在尝试运行我的客户端/服务器android程序。但每当我在Android上运行客户端部分时它会给出错误,不幸的是你的应用程序已停止运行。我试过读logcat但是还没能解决问题。下面是我的代码...我已经尝试了3天。需要项目帮助
logcat的
12-06 21:05:19.948: D/AndroidRuntime(2136): Shutting down VM
12-06 21:05:19.948: W/dalvikvm(2136): threadid=1: thread exiting with uncaught exception (group=0xa4cf8b20)
12-06 21:05:19.980: E/AndroidRuntime(2136): FATAL EXCEPTION: main
12-06 21:05:19.980: E/AndroidRuntime(2136): Process: com.example.simpleclientapp, PID: 2136
12-06 21:05:19.980: E/AndroidRuntime(2136): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.simpleclientapp/com.example.simpleclientapp.New_main_activity}: android.view.InflateException: Binary XML file line #1: Error inflating class linearlayout
12-06 21:05:19.980: E/AndroidRuntime(2136): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
12-06 21:05:19.980: E/AndroidRuntime(2136): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
12-06 21:05:19.980: E/AndroidRuntime(2136): at android.app.ActivityThread.access$800(ActivityThread.java:135)
12-06 21:05:19.980: E/AndroidRuntime(2136): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
12-06 21:05:19.980: E/AndroidRuntime(2136): at android.os.Handler.dispatchMessage(Handler.java:102)
12-06 21:05:19.980: E/AndroidRuntime(2136): at android.os.Looper.loop(Looper.java:136)
12-06 21:05:19.980: E/AndroidRuntime(2136): at android.app.ActivityThread.main(ActivityThread.java:5001)
12-06 21:05:19.980: E/AndroidRuntime(2136): at java.lang.reflect.Method.invokeNative(Native Method)
12-06 21:05:19.980: E/AndroidRuntime(2136): at java.lang.reflect.Method.invoke(Method.java:515)
12-06 21:05:19.980: E/AndroidRuntime(2136): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-06 21:05:19.980: E/AndroidRuntime(2136): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-06 21:05:19.980: E/AndroidRuntime(2136): at dalvik.system.NativeStart.main(Native Method)
12-06 21:05:19.980: E/AndroidRuntime(2136): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class linearlayout
12-06 21:05:19.980: E/AndroidRuntime(2136): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:707)
12-06 21:05:19.980: E/AndroidRuntime(2136): at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
12-06 21:05:19.980: E/AndroidRuntime(2136): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
12-06 21:05:19.980: E/AndroidRuntime(2136): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
12-06 21:05:19.980: E/AndroidRuntime(2136): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
12-06 21:05:19.980: E/AndroidRuntime(2136): at android.app.Activity.setContentView(Activity.java:1929)
12-06 21:05:19.980: E/AndroidRuntime(2136): at com.example.simpleclientapp.New_main_activity.onCreate(New_main_activity.java:33)
12-06 21:05:19.980: E/AndroidRuntime(2136): at android.app.Activity.performCreate(Activity.java:5231)
12-06 21:05:19.980: E/AndroidRuntime(2136): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-06 21:05:19.980: E/AndroidRuntime(2136): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
12-06 21:05:19.980: E/AndroidRuntime(2136): ... 11 more
12-06 21:05:19.980: E/AndroidRuntime(2136): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.linearlayout" on path: DexPathList[[zip file "/data/app/com.example.simpleclientapp-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.simpleclientapp-2, /system/lib]]
12-06 21:05:19.980: E/AndroidRuntime(2136): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
12-06 21:05:19.980: E/AndroidRuntime(2136): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
12-06 21:05:19.980: E/AndroidRuntime(2136): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
12-06 21:05:19.980: E/AndroidRuntime(2136): at android.view.LayoutInflater.createView(LayoutInflater.java:559)
12-06 21:05:19.980: E/AndroidRuntime(2136): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:652)
12-06 21:05:19.980: E/AndroidRuntime(2136): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
12-06 21:05:19.980: E/AndroidRuntime(2136): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
12-06 21:05:19.980: E/AndroidRuntime(2136): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
12-06 21:05:19.980: E/AndroidRuntime(2136): ... 20 more`
activity_new_main_activity.xml
<linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
<textview android:id="@+id/textView1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge">
<button android:id="@+id/button1" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Send">
</button></textview></linearlayout>
的manifest.xml
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".New_main_activity"
android:label="@string/title_activity_new_main_activity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
new_main_activity.java
package com.example.simpleclientapp;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.lang.*;
import java.lang.annotation.*;
import java.lang.ref.*;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class New_main_activity extends Activity
{
private Socket client;
private FileInputStream fileInputStream;
private BufferedInputStream bufferedInputStream;
private OutputStream outputStream;
private Button button;
private TextView text;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_main_activity);
button = (Button) findViewById(R.id.button1); //reference to the send button
text = (TextView) findViewById(R.id.textView1); //reference to the text view
//Button press event listener
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
File file = new File("/mnt/shared/sharedwithemulatot/numbers.txt"); //create file instance
try
{
client = new Socket("127.0.0.1", 6443);
byte[] mybytearray = new byte[(int) file.length()]; //create a byte array to file
fileInputStream = new FileInputStream(file);
bufferedInputStream = new BufferedInputStream(fileInputStream);
bufferedInputStream.read(mybytearray, 0, mybytearray.length); //read the file
outputStream = client.getOutputStream();
outputStream.write(mybytearray, 0, mybytearray.length); //write file to the output stream byte by byte
outputStream.flush();
bufferedInputStream.close();
outputStream.close();
client.close();
text.setText("File Sent");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
答案 0 :(得分:6)
在布局文件中,您似乎拥有<linearlayout>
标记。请改用<LinearLayout>
标记。案例在这里很重要。
更新:原始问题有布局文件,但由于格式化,我错过了它们。正如Squonk指出的那样,所有元素都必须处于适当的情况。