2016-03-29 8 views
1

私は自己アンドロイド学習者です。以下のコードはエラーを示しています。エラーを修正するのを手伝ってください。ここでは、sに格納されているバイナリ文字列を対応する整数値に変換したいと考えています。どのように私はアンドロイドスタジオで整数にバイナリ文字列を変換できますか?

import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    String s = "110"; 
    int num = Integer.parseInt(s); 
    TextView textView = new TextView(this); 
    textView.setText(num); 
    setContentView(textView); 
} 

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

@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(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
} 

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="com.example.addition.AdditionActivity" > 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="New Text" 
    android:id="@+id/textView" 
    android:layout_centerVertical="true" /> 
</RelativeLayout> 

ここでは私のlogcatです:2は、お使いのベース(または基数)である

03-29 16:39:42.608 5722-5722/com.example.user.rec E/AndroidRuntime: FATAL EXCEPTION: main 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.rec/com.example.user.rec.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x6 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
     at android.app.ActivityThread.access$600(ActivityThread.java:141) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
     at android.os.Handler.dispatchMessage(Handler.java:99) 
     at android.os.Looper.loop(Looper.java:137) 
     at android.app.ActivityThread.main(ActivityThread.java:5041) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:511) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
     at dalvik.system.NativeStart.main(Native Method) 
    Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x6 
     at android.content.res.Resources.getText(Resources.java:230) 
     at android.widget.TextView.setText(TextView.java:3776) 
     at com.example.user.rec.MainActivity.onCreate(MainActivity.java:22) 
     at android.app.Activity.performCreate(Activity.java:5104) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
     at android.app.ActivityThread.access$600(ActivityThread.java:141) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
     at android.os.Handler.dispatchMessage(Handler.java:99) 
     at android.os.Looper.loop(Looper.java:137) 
     at android.app.ActivityThread.main(ActivityThread.java:5041) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:511) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
     at dalvik.system.NativeStart.main(Native Method) 
03-29 16:39:52.567 5722-5722/com.example.user.rec I/Process: Sending signal. PID: 5722 SIG: 9 

答えて

4
int num = Integer.parseInt("110", 2); 

。公式ドキュメントには、 linkがあります。

+0

エラーが表示されます。 Plzはコード全体を通過し、私を助ける –

+0

また、私は "ComponentInfo {com.example.user.rec/com.example.user.rec.MainActivity}アクティビティを開始できませんエラー:" android.content.res.Resources $ NotFoundException:文字列リソースID#0x6 ".plzこれを修正する方法を教えてください –

+0

logcatの出力とAndroidManifest.xmlで投稿を更新できますか? –

関連する問題